Combining two simple SQL Server queries takes long running time
up vote
0
down vote
favorite
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
sql-server ogc
New contributor
add a comment |
up vote
0
down vote
favorite
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
sql-server ogc
New contributor
How much faster is it without the distinct (which isn't needed)? Also, have you tried exists instead?
– scsimon
Nov 16 at 14:14
brentozar.com/archive/2009/03/getting-help-with-a-slow-query
– scsimon
Nov 16 at 14:17
DISTINCT does not affect duration. I also tried NOT IN and NOT EXISTS with the same long running result
– Apostolis Kotidis
Nov 16 at 14:44
THen please post the execution plan :)
– scsimon
Nov 16 at 14:44
link @EricZ
– Apostolis Kotidis
Nov 16 at 15:02
add a comment |
up vote
0
down vote
favorite
up vote
0
down vote
favorite
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
sql-server ogc
New contributor
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
sql-server ogc
sql-server ogc
New contributor
New contributor
edited 2 days ago
Birel
1449
1449
New contributor
asked Nov 16 at 14:08
Apostolis Kotidis
11
11
New contributor
New contributor
How much faster is it without the distinct (which isn't needed)? Also, have you tried exists instead?
– scsimon
Nov 16 at 14:14
brentozar.com/archive/2009/03/getting-help-with-a-slow-query
– scsimon
Nov 16 at 14:17
DISTINCT does not affect duration. I also tried NOT IN and NOT EXISTS with the same long running result
– Apostolis Kotidis
Nov 16 at 14:44
THen please post the execution plan :)
– scsimon
Nov 16 at 14:44
link @EricZ
– Apostolis Kotidis
Nov 16 at 15:02
add a comment |
How much faster is it without the distinct (which isn't needed)? Also, have you tried exists instead?
– scsimon
Nov 16 at 14:14
brentozar.com/archive/2009/03/getting-help-with-a-slow-query
– scsimon
Nov 16 at 14:17
DISTINCT does not affect duration. I also tried NOT IN and NOT EXISTS with the same long running result
– Apostolis Kotidis
Nov 16 at 14:44
THen please post the execution plan :)
– scsimon
Nov 16 at 14:44
link @EricZ
– Apostolis Kotidis
Nov 16 at 15:02
How much faster is it without the distinct (which isn't needed)? Also, have you tried exists instead?
– scsimon
Nov 16 at 14:14
How much faster is it without the distinct (which isn't needed)? Also, have you tried exists instead?
– scsimon
Nov 16 at 14:14
brentozar.com/archive/2009/03/getting-help-with-a-slow-query
– scsimon
Nov 16 at 14:17
brentozar.com/archive/2009/03/getting-help-with-a-slow-query
– scsimon
Nov 16 at 14:17
DISTINCT does not affect duration. I also tried NOT IN and NOT EXISTS with the same long running result
– Apostolis Kotidis
Nov 16 at 14:44
DISTINCT does not affect duration. I also tried NOT IN and NOT EXISTS with the same long running result
– Apostolis Kotidis
Nov 16 at 14:44
THen please post the execution plan :)
– scsimon
Nov 16 at 14:44
THen please post the execution plan :)
– scsimon
Nov 16 at 14:44
link @EricZ
– Apostolis Kotidis
Nov 16 at 15:02
link @EricZ
– Apostolis Kotidis
Nov 16 at 15:02
add a comment |
2 Answers
2
active
oldest
votes
up vote
1
down vote
Try to insert your script to temp table than combine it. We don't have execution plan we can only show you to execute your query with different angles. :
SELECT DISTINCT PST.OBJECTID as OID INTO #Temp 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;
SELECT OBJECTID AS OID FROM PST_35053_SNAPPED
EXCEPT
SELECT OID FROM #Temp;
or try not exists :
SELECT OBJECTID AS OID
FROM PST_35053_SNAPPED PST1
WHERE NOT EXISTS
(SELECT 1 FROM
(
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
) PST2
WHERE PST1.OBJECTID=PST2.OBJECTID
)
or try WITH :
WITH Cte AS
(
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
)
SELECT OBJECTID AS OID FROM PST_35053_SNAPPED
EXCEPT
SELECT OID FROM Cte;
Thanks Zeki. This executes in 2 seconds with the desirable results, but I would like an explanation for the delay. I am using the queries in software code and I would like to avoid temporary tables.
– Apostolis Kotidis
Nov 16 at 15:19
brentozar.com/pastetheplan/?id=HkrmoIhT7
– Apostolis Kotidis
Nov 16 at 15:23
hi @ApostolisKotidis I checked your execution plan. I can assume only except works slowly when you use multiple join. Also,I think(can't prove) it seems spatial data works slowly with except. I put another query with using NOT EXISTS instead of using temp table (because you don't want to use temp table). Could you check it please.
– Zeki Gumus
Nov 16 at 15:59
I checked it but the result is worse. It takes more than 3 minutes. I also checked NOT IN with the same results. I tried UNION and INTERSECT instead of EXCEPT too. UNION takes less than a second as expected, INTERSECT is very slow taking 1 minute and EXCEPT is terribly slow taking 2 minutes. It does not seem to be a coding problem , but an SQL Server matter. I will try to test it on another edition of SQL Server
– Apostolis Kotidis
2 days ago
1
Hi @ApostolisKotidis , I have added another solution using WITH clause.
– Zeki Gumus
2 days ago
|
show 1 more comment
up vote
0
down vote
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.
SELECT PST.OBJECTID as OID
FROM PST_35053_SNAPPED PST
LEFT JOIN POWNERS_35053 POW
ON geometry::STGeomFromText('POINT(' + convert(varchar(16),POW.x) + ' ' +
convert(varchar(17),POW.y) + ')', 2100).STWithin(PST.Shape)=1
WHERE POW.OBJECTID IS NULL
why would it be faster?
– scsimon
Nov 16 at 14:17
No, it took 193 seconds to run. I also tried variations with NOT EXISTS and NOT IN with nested selections and took also more than 2 minutes time.
– Apostolis Kotidis
Nov 16 at 14:37
please share execution plan
– EricZ
Nov 16 at 14:47
[brentozar.com/pastetheplan/?id=HkrmoIhT7
– Apostolis Kotidis
Nov 16 at 15:03
add a comment |
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
1
down vote
Try to insert your script to temp table than combine it. We don't have execution plan we can only show you to execute your query with different angles. :
SELECT DISTINCT PST.OBJECTID as OID INTO #Temp 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;
SELECT OBJECTID AS OID FROM PST_35053_SNAPPED
EXCEPT
SELECT OID FROM #Temp;
or try not exists :
SELECT OBJECTID AS OID
FROM PST_35053_SNAPPED PST1
WHERE NOT EXISTS
(SELECT 1 FROM
(
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
) PST2
WHERE PST1.OBJECTID=PST2.OBJECTID
)
or try WITH :
WITH Cte AS
(
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
)
SELECT OBJECTID AS OID FROM PST_35053_SNAPPED
EXCEPT
SELECT OID FROM Cte;
Thanks Zeki. This executes in 2 seconds with the desirable results, but I would like an explanation for the delay. I am using the queries in software code and I would like to avoid temporary tables.
– Apostolis Kotidis
Nov 16 at 15:19
brentozar.com/pastetheplan/?id=HkrmoIhT7
– Apostolis Kotidis
Nov 16 at 15:23
hi @ApostolisKotidis I checked your execution plan. I can assume only except works slowly when you use multiple join. Also,I think(can't prove) it seems spatial data works slowly with except. I put another query with using NOT EXISTS instead of using temp table (because you don't want to use temp table). Could you check it please.
– Zeki Gumus
Nov 16 at 15:59
I checked it but the result is worse. It takes more than 3 minutes. I also checked NOT IN with the same results. I tried UNION and INTERSECT instead of EXCEPT too. UNION takes less than a second as expected, INTERSECT is very slow taking 1 minute and EXCEPT is terribly slow taking 2 minutes. It does not seem to be a coding problem , but an SQL Server matter. I will try to test it on another edition of SQL Server
– Apostolis Kotidis
2 days ago
1
Hi @ApostolisKotidis , I have added another solution using WITH clause.
– Zeki Gumus
2 days ago
|
show 1 more comment
up vote
1
down vote
Try to insert your script to temp table than combine it. We don't have execution plan we can only show you to execute your query with different angles. :
SELECT DISTINCT PST.OBJECTID as OID INTO #Temp 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;
SELECT OBJECTID AS OID FROM PST_35053_SNAPPED
EXCEPT
SELECT OID FROM #Temp;
or try not exists :
SELECT OBJECTID AS OID
FROM PST_35053_SNAPPED PST1
WHERE NOT EXISTS
(SELECT 1 FROM
(
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
) PST2
WHERE PST1.OBJECTID=PST2.OBJECTID
)
or try WITH :
WITH Cte AS
(
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
)
SELECT OBJECTID AS OID FROM PST_35053_SNAPPED
EXCEPT
SELECT OID FROM Cte;
Thanks Zeki. This executes in 2 seconds with the desirable results, but I would like an explanation for the delay. I am using the queries in software code and I would like to avoid temporary tables.
– Apostolis Kotidis
Nov 16 at 15:19
brentozar.com/pastetheplan/?id=HkrmoIhT7
– Apostolis Kotidis
Nov 16 at 15:23
hi @ApostolisKotidis I checked your execution plan. I can assume only except works slowly when you use multiple join. Also,I think(can't prove) it seems spatial data works slowly with except. I put another query with using NOT EXISTS instead of using temp table (because you don't want to use temp table). Could you check it please.
– Zeki Gumus
Nov 16 at 15:59
I checked it but the result is worse. It takes more than 3 minutes. I also checked NOT IN with the same results. I tried UNION and INTERSECT instead of EXCEPT too. UNION takes less than a second as expected, INTERSECT is very slow taking 1 minute and EXCEPT is terribly slow taking 2 minutes. It does not seem to be a coding problem , but an SQL Server matter. I will try to test it on another edition of SQL Server
– Apostolis Kotidis
2 days ago
1
Hi @ApostolisKotidis , I have added another solution using WITH clause.
– Zeki Gumus
2 days ago
|
show 1 more comment
up vote
1
down vote
up vote
1
down vote
Try to insert your script to temp table than combine it. We don't have execution plan we can only show you to execute your query with different angles. :
SELECT DISTINCT PST.OBJECTID as OID INTO #Temp 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;
SELECT OBJECTID AS OID FROM PST_35053_SNAPPED
EXCEPT
SELECT OID FROM #Temp;
or try not exists :
SELECT OBJECTID AS OID
FROM PST_35053_SNAPPED PST1
WHERE NOT EXISTS
(SELECT 1 FROM
(
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
) PST2
WHERE PST1.OBJECTID=PST2.OBJECTID
)
or try WITH :
WITH Cte AS
(
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
)
SELECT OBJECTID AS OID FROM PST_35053_SNAPPED
EXCEPT
SELECT OID FROM Cte;
Try to insert your script to temp table than combine it. We don't have execution plan we can only show you to execute your query with different angles. :
SELECT DISTINCT PST.OBJECTID as OID INTO #Temp 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;
SELECT OBJECTID AS OID FROM PST_35053_SNAPPED
EXCEPT
SELECT OID FROM #Temp;
or try not exists :
SELECT OBJECTID AS OID
FROM PST_35053_SNAPPED PST1
WHERE NOT EXISTS
(SELECT 1 FROM
(
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
) PST2
WHERE PST1.OBJECTID=PST2.OBJECTID
)
or try WITH :
WITH Cte AS
(
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
)
SELECT OBJECTID AS OID FROM PST_35053_SNAPPED
EXCEPT
SELECT OID FROM Cte;
edited 2 days ago
answered Nov 16 at 14:56
Zeki Gumus
2186
2186
Thanks Zeki. This executes in 2 seconds with the desirable results, but I would like an explanation for the delay. I am using the queries in software code and I would like to avoid temporary tables.
– Apostolis Kotidis
Nov 16 at 15:19
brentozar.com/pastetheplan/?id=HkrmoIhT7
– Apostolis Kotidis
Nov 16 at 15:23
hi @ApostolisKotidis I checked your execution plan. I can assume only except works slowly when you use multiple join. Also,I think(can't prove) it seems spatial data works slowly with except. I put another query with using NOT EXISTS instead of using temp table (because you don't want to use temp table). Could you check it please.
– Zeki Gumus
Nov 16 at 15:59
I checked it but the result is worse. It takes more than 3 minutes. I also checked NOT IN with the same results. I tried UNION and INTERSECT instead of EXCEPT too. UNION takes less than a second as expected, INTERSECT is very slow taking 1 minute and EXCEPT is terribly slow taking 2 minutes. It does not seem to be a coding problem , but an SQL Server matter. I will try to test it on another edition of SQL Server
– Apostolis Kotidis
2 days ago
1
Hi @ApostolisKotidis , I have added another solution using WITH clause.
– Zeki Gumus
2 days ago
|
show 1 more comment
Thanks Zeki. This executes in 2 seconds with the desirable results, but I would like an explanation for the delay. I am using the queries in software code and I would like to avoid temporary tables.
– Apostolis Kotidis
Nov 16 at 15:19
brentozar.com/pastetheplan/?id=HkrmoIhT7
– Apostolis Kotidis
Nov 16 at 15:23
hi @ApostolisKotidis I checked your execution plan. I can assume only except works slowly when you use multiple join. Also,I think(can't prove) it seems spatial data works slowly with except. I put another query with using NOT EXISTS instead of using temp table (because you don't want to use temp table). Could you check it please.
– Zeki Gumus
Nov 16 at 15:59
I checked it but the result is worse. It takes more than 3 minutes. I also checked NOT IN with the same results. I tried UNION and INTERSECT instead of EXCEPT too. UNION takes less than a second as expected, INTERSECT is very slow taking 1 minute and EXCEPT is terribly slow taking 2 minutes. It does not seem to be a coding problem , but an SQL Server matter. I will try to test it on another edition of SQL Server
– Apostolis Kotidis
2 days ago
1
Hi @ApostolisKotidis , I have added another solution using WITH clause.
– Zeki Gumus
2 days ago
Thanks Zeki. This executes in 2 seconds with the desirable results, but I would like an explanation for the delay. I am using the queries in software code and I would like to avoid temporary tables.
– Apostolis Kotidis
Nov 16 at 15:19
Thanks Zeki. This executes in 2 seconds with the desirable results, but I would like an explanation for the delay. I am using the queries in software code and I would like to avoid temporary tables.
– Apostolis Kotidis
Nov 16 at 15:19
brentozar.com/pastetheplan/?id=HkrmoIhT7
– Apostolis Kotidis
Nov 16 at 15:23
brentozar.com/pastetheplan/?id=HkrmoIhT7
– Apostolis Kotidis
Nov 16 at 15:23
hi @ApostolisKotidis I checked your execution plan. I can assume only except works slowly when you use multiple join. Also,I think(can't prove) it seems spatial data works slowly with except. I put another query with using NOT EXISTS instead of using temp table (because you don't want to use temp table). Could you check it please.
– Zeki Gumus
Nov 16 at 15:59
hi @ApostolisKotidis I checked your execution plan. I can assume only except works slowly when you use multiple join. Also,I think(can't prove) it seems spatial data works slowly with except. I put another query with using NOT EXISTS instead of using temp table (because you don't want to use temp table). Could you check it please.
– Zeki Gumus
Nov 16 at 15:59
I checked it but the result is worse. It takes more than 3 minutes. I also checked NOT IN with the same results. I tried UNION and INTERSECT instead of EXCEPT too. UNION takes less than a second as expected, INTERSECT is very slow taking 1 minute and EXCEPT is terribly slow taking 2 minutes. It does not seem to be a coding problem , but an SQL Server matter. I will try to test it on another edition of SQL Server
– Apostolis Kotidis
2 days ago
I checked it but the result is worse. It takes more than 3 minutes. I also checked NOT IN with the same results. I tried UNION and INTERSECT instead of EXCEPT too. UNION takes less than a second as expected, INTERSECT is very slow taking 1 minute and EXCEPT is terribly slow taking 2 minutes. It does not seem to be a coding problem , but an SQL Server matter. I will try to test it on another edition of SQL Server
– Apostolis Kotidis
2 days ago
1
1
Hi @ApostolisKotidis , I have added another solution using WITH clause.
– Zeki Gumus
2 days ago
Hi @ApostolisKotidis , I have added another solution using WITH clause.
– Zeki Gumus
2 days ago
|
show 1 more comment
up vote
0
down vote
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.
SELECT PST.OBJECTID as OID
FROM PST_35053_SNAPPED PST
LEFT JOIN POWNERS_35053 POW
ON geometry::STGeomFromText('POINT(' + convert(varchar(16),POW.x) + ' ' +
convert(varchar(17),POW.y) + ')', 2100).STWithin(PST.Shape)=1
WHERE POW.OBJECTID IS NULL
why would it be faster?
– scsimon
Nov 16 at 14:17
No, it took 193 seconds to run. I also tried variations with NOT EXISTS and NOT IN with nested selections and took also more than 2 minutes time.
– Apostolis Kotidis
Nov 16 at 14:37
please share execution plan
– EricZ
Nov 16 at 14:47
[brentozar.com/pastetheplan/?id=HkrmoIhT7
– Apostolis Kotidis
Nov 16 at 15:03
add a comment |
up vote
0
down vote
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.
SELECT PST.OBJECTID as OID
FROM PST_35053_SNAPPED PST
LEFT JOIN POWNERS_35053 POW
ON geometry::STGeomFromText('POINT(' + convert(varchar(16),POW.x) + ' ' +
convert(varchar(17),POW.y) + ')', 2100).STWithin(PST.Shape)=1
WHERE POW.OBJECTID IS NULL
why would it be faster?
– scsimon
Nov 16 at 14:17
No, it took 193 seconds to run. I also tried variations with NOT EXISTS and NOT IN with nested selections and took also more than 2 minutes time.
– Apostolis Kotidis
Nov 16 at 14:37
please share execution plan
– EricZ
Nov 16 at 14:47
[brentozar.com/pastetheplan/?id=HkrmoIhT7
– Apostolis Kotidis
Nov 16 at 15:03
add a comment |
up vote
0
down vote
up vote
0
down vote
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.
SELECT PST.OBJECTID as OID
FROM PST_35053_SNAPPED PST
LEFT JOIN POWNERS_35053 POW
ON geometry::STGeomFromText('POINT(' + convert(varchar(16),POW.x) + ' ' +
convert(varchar(17),POW.y) + ')', 2100).STWithin(PST.Shape)=1
WHERE POW.OBJECTID IS NULL
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.
SELECT PST.OBJECTID as OID
FROM PST_35053_SNAPPED PST
LEFT JOIN POWNERS_35053 POW
ON geometry::STGeomFromText('POINT(' + convert(varchar(16),POW.x) + ' ' +
convert(varchar(17),POW.y) + ')', 2100).STWithin(PST.Shape)=1
WHERE POW.OBJECTID IS NULL
answered Nov 16 at 14:16
EricZ
5,2672027
5,2672027
why would it be faster?
– scsimon
Nov 16 at 14:17
No, it took 193 seconds to run. I also tried variations with NOT EXISTS and NOT IN with nested selections and took also more than 2 minutes time.
– Apostolis Kotidis
Nov 16 at 14:37
please share execution plan
– EricZ
Nov 16 at 14:47
[brentozar.com/pastetheplan/?id=HkrmoIhT7
– Apostolis Kotidis
Nov 16 at 15:03
add a comment |
why would it be faster?
– scsimon
Nov 16 at 14:17
No, it took 193 seconds to run. I also tried variations with NOT EXISTS and NOT IN with nested selections and took also more than 2 minutes time.
– Apostolis Kotidis
Nov 16 at 14:37
please share execution plan
– EricZ
Nov 16 at 14:47
[brentozar.com/pastetheplan/?id=HkrmoIhT7
– Apostolis Kotidis
Nov 16 at 15:03
why would it be faster?
– scsimon
Nov 16 at 14:17
why would it be faster?
– scsimon
Nov 16 at 14:17
No, it took 193 seconds to run. I also tried variations with NOT EXISTS and NOT IN with nested selections and took also more than 2 minutes time.
– Apostolis Kotidis
Nov 16 at 14:37
No, it took 193 seconds to run. I also tried variations with NOT EXISTS and NOT IN with nested selections and took also more than 2 minutes time.
– Apostolis Kotidis
Nov 16 at 14:37
please share execution plan
– EricZ
Nov 16 at 14:47
please share execution plan
– EricZ
Nov 16 at 14:47
[brentozar.com/pastetheplan/?id=HkrmoIhT7
– Apostolis Kotidis
Nov 16 at 15:03
[brentozar.com/pastetheplan/?id=HkrmoIhT7
– Apostolis Kotidis
Nov 16 at 15:03
add a comment |
Apostolis Kotidis is a new contributor. Be nice, and check out our Code of Conduct.
Apostolis Kotidis is a new contributor. Be nice, and check out our Code of Conduct.
Apostolis Kotidis is a new contributor. Be nice, and check out our Code of Conduct.
Apostolis Kotidis is a new contributor. Be nice, and check out our Code of Conduct.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53339463%2fcombining-two-simple-sql-server-queries-takes-long-running-time%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
How much faster is it without the distinct (which isn't needed)? Also, have you tried exists instead?
– scsimon
Nov 16 at 14:14
brentozar.com/archive/2009/03/getting-help-with-a-slow-query
– scsimon
Nov 16 at 14:17
DISTINCT does not affect duration. I also tried NOT IN and NOT EXISTS with the same long running result
– Apostolis Kotidis
Nov 16 at 14:44
THen please post the execution plan :)
– scsimon
Nov 16 at 14:44
link @EricZ
– Apostolis Kotidis
Nov 16 at 15:02