I am creating temporary table during stored procedure execution with the following structure:
[ID] BIGINT
[Point] GEOGRAPHY
the ID
is not unique - there are about 200
records for each ID
.
I need to find a list with distinct IDs
for which there is at least one Point
to Point
distance larger then constant value (for example 200
meters).
So, I am using something like this:
SELECT DISTINCT DS1.[ID]
FROM DataSource DS1
INNER JOIN DataSource DS2
ON DS1.[ID] = DS2.[ID]
WHERE DS1.Point.STDistance(DS2.Point) > 200
For 23 000 points, the query is executed for 4-5
seconds. As I am expecting to have more values, I need to find better solution.
I guess that if there is faster way, I can always create a materialized table and implement additional logic that will calculated this on ID
base.
I have created a spatial index, but the query optimizer is not using it. If I use a hint
like this WITH (INDEX(SPATIAL_idx_test))
I am getting the following error:
Msg 8635, Level 16, State 4, Line 78
The query processor could not produce a query plan for a query with a spatial index hint. Reason: Spatial indexes do not support the comparator supplied in the predicate. Try removing the index hints or removingSET FORCEPLAN
. `