In a table with almost 50 million rows, there's a geography column.
I've already created a spatial index on that column and when I tried to execute the below query
SELECT
table_name.column_name.STDistance(geography::STPointFromText (('POINT(latidude longitude)'), 4326)) AS dist
FROM
table_name (nolock)
INNER JOIN
table_name_2 (nolock) ON (some conditions)
INNER JOIN
table_name_3 (nolock) ON (some conditions)
WHERE
geography::STPolyFromText (('POLYGON((latitude longitude))'), 4326).STContains(table_name.column_name) = 1
This query is running for a very long time.
Apart from the spatial index, is there something else I can do to improve the performance of this query?
NOLOCKhint here? Do you "need" to use it? If you "must" use it, why are you using deprecated syntax or not just changing the isolation level of your query, as you're using it against every table? Bad habits : Putting NOLOCK everywhereSTContainsisn't in the list of methods supported by spatial indexes. So why would you expect it to benefit from one?