MS SQL Server STIntersects OR STIntersects is too slow

582 views Asked by At

I am querying a table with a geography column that is a LINESTRING. My query is basically this:

SELECT id FROM mytable 
WHERE geography::STPolyFromText('POLYGON((...coords for poly A...)),4326).STIntersects(geo) = 1 
OR geography::STPolyFromText('POLYGON((...coords for poly B...)),4326).STIntersects(geo) = 1

This query results in 81 records and takes 16 seconds.

If I break it into 2 separate querys:

SELECT id FROM mytable 
WHERE geography::STPolyFromText('POLYGON((...coords for poly A...)),4326).STIntersects(geo) = 1 

SELECT id FROM mytable 
WHERE geography::STPolyFromText('POLYGON((...coords for poly B...)),4326).STIntersects(geo) = 1 

They return 51 and 31 records (1 in common) respectively and take virtually no time to execute.

Why is the OR so unusably slow? Do I have to resort to multiple queries and post processing to implement something so basic or am I missing something obvious?

0

There are 0 answers