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










share|improve this question









New contributor




Apostolis Kotidis is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.




















  • 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















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










share|improve this question









New contributor




Apostolis Kotidis is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.




















  • 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













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










share|improve this question









New contributor




Apostolis Kotidis is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.











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






share|improve this question









New contributor




Apostolis Kotidis is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.











share|improve this question









New contributor




Apostolis Kotidis is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.









share|improve this question




share|improve this question








edited 2 days ago









Birel

1449




1449






New contributor




Apostolis Kotidis is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.









asked Nov 16 at 14:08









Apostolis Kotidis

11




11




New contributor




Apostolis Kotidis is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.





New contributor





Apostolis Kotidis is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.






Apostolis Kotidis is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.












  • 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












  • 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












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;





share|improve this answer























  • 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


















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





share|improve this answer





















  • 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













Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});






Apostolis Kotidis is a new contributor. Be nice, and check out our Code of Conduct.










 

draft saved


draft discarded


















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

























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;





share|improve this answer























  • 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















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;





share|improve this answer























  • 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













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;





share|improve this answer














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;






share|improve this answer














share|improve this answer



share|improve this answer








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


















  • 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












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





share|improve this answer





















  • 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

















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





share|improve this answer





















  • 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















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





share|improve this answer












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






share|improve this answer












share|improve this answer



share|improve this answer










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




















  • 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












Apostolis Kotidis is a new contributor. Be nice, and check out our Code of Conduct.










 

draft saved


draft discarded


















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.















 


draft saved


draft discarded














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





















































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







Popular posts from this blog

Volksrepublik China

How to test boost logger output in unit testing?

Write to the output between two pipeline