I have two simple queries that execute within expected time when they run alone. The first query:
SELECT OBJECTID AS OID FROM PST_35053_SNAPPED;
Takes less than a second and returns about 3000 rows. The second query:
SELECT DISTINCT PST.OBJECTID as OID FROM PST_35053_SNAPPED
PST INNER JOIN POWNERS_35053 POW
ON geometry::STGeomFromText('POINT(' + convert(varchar(16),POW.x) + ' ' +
convert(varchar(17),POW.y) + ')', 2100).STWithin(PST.Shape)=1;
Takes a second returning about 2500 rows.
When combining them with EXCEPT
to retrieve polygon IDs
without points falling in them (about 500 rows), the resulting query takes more than two minutes to execute (about 122 seconds):
SELECT OBJECTID AS OID FROM PST_35053_SNAPPED
EXCEPT
SELECT DISTINCT PST.OBJECTID as OID FROM PST_35053_SNAPPED
PST INNER JOIN POWNERS_35053 POW
ON geometry::STGeomFromText('POINT(' + convert(varchar(16),POW.x) + ' ' +
convert(varchar(17),POW.y) + ')', 2100).STWithin(PST.Shape)=1
Is there something I am missing or doing wrong? I am using SQL Server 2012 SP3
It's hard to say without query execution plan. However, if I understand correctly, the following query should get the same result, and would be faster.