1

I want to compare two geography column using StContains function but it slow down the query and output is obtained after 30-40 seconds which is a huge performance issue.

I am using below query:

SELECT  DISTINCT
            T.[LocationID], G.[Boundary].STContains(ESP.GeographyValue)
    FROM    [#TempTable] T
    CROSS JOIN [dbo].[GeographyTable] G
    INNER JOIN [dbo].LocationTable ESP ON T.LocationID = ESP.LocationID
    WHERE   G.[ID] = 1

Here both Boundary and GeographyValue are geography data type.

If i remove G.[Boundary].STContains(ESP.GeographyValue) then query is executed in 0 second so the main performance issue is due to StContains.

The result set contains 7000 records which may not be the issue.

Update:

I have added spatial index to both the columns but still the execution is slow. By running a simple query it takes 10 secs.I used below query:

select ES.* from LocationTable ES INNER JOIN GeographyTable G ON ES.GeographyValue.STEquals(G.Boundary) = 1 Where G.Id = 1

I also tried STContains but still no luck.

4
  • 1
    What is the purpose of this query that you need to compare 7000 different locations? Spatial comparisons are complex and resource intensive so can take a while and you are doing 7000 of them! Do you have a spatial index on your Geography columns? Commented Dec 21, 2016 at 11:47
  • I have a list of Geography on UI through which user can select one of then and I want to compare GeographyTable with matching location. LocationTable is present in another database so I cannot add GeographyTable ID column to it. Spatial index is not present in both the table. Can you guide to which table I need to add it and how? Commented Dec 21, 2016 at 12:16
  • Spatial indexes go on your spatial columns... If your users are selecting one Geography via your UI, why are you comparing all 7000? Just compare the one they select. Commented Dec 21, 2016 at 12:20
  • I only have Id of GeographyTable and I want to get records from LocationTable based on it. I don't have relation of GeographyTable and LocationTable. The only common field is Boundary from GeographyTable and GeographyValue from LocationTable. So is there any way to compare both of this? If i use STContain and StEquals then it takes 30 seconds. Commented Dec 21, 2016 at 13:07

1 Answer 1

3

Force your code to use spatial index using below code [dbo].[GeographyTable] G WITH (INDEX(SI_Geofence_Boundary))

Your revised query will look like,

SELECT  DISTINCT
            T.[LocationID], G.[Boundary].STContains(ESP.GeographyValue)
    FROM    [#TempTable] T
    CROSS JOIN [dbo].[GeographyTable] G WITH (INDEX(SI_Geofence_Boundary))
    INNER JOIN [dbo].LocationTable ESP ON T.LocationID = ESP.LocationID
    WHERE   G.[ID] = 1

It would reduce execution time from 30-40 seconds to 2-3 seconds.

For more information review the link

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.