Query with conditional JOIN running slow
I have a query the first part of which looks like this -
With CTE AS (
select DISTINCT
A.col1,
A.col2,
.
.
.
from tableA A
JOIN tableB B ON
(
B.col3= 'X' and B.col4=A.col3
OR
B.col3= 'Y' and B.col4=A.col4
OR
.
.
.
.
)
)
Now this query runs for more than 4 minutes and the execution plan showed a table spool which was data size of 600 MB and looked like this -
I changed this to conditional JOIN to UNIONs -
With CTE AS (
select
A.col1,
A.col2,
.
.
.
from tableA A
JOIN tableB B ON
(
B.col3= 'X' and B.col4=A.col3
)
UNION
select
A.col1,
A.col2,
.
.
.
from tableA A
JOIN tableB B ON
(
B.col3= 'Y' and B.col4=A.col4
)
UNION
.
.
.
)
Now the spool with the large number of rows vanished and the run time reduced to 17 seconds. However when I check the IO statistics, the logical reads are higher than the previous slower query. What could the reason be ?
sql-server-2014 join execution-plan statistics union
bumped to the homepage by Community♦ 7 mins ago
This question has answers that may be good or bad; the system has marked it active so that they can be reviewed.
add a comment |
I have a query the first part of which looks like this -
With CTE AS (
select DISTINCT
A.col1,
A.col2,
.
.
.
from tableA A
JOIN tableB B ON
(
B.col3= 'X' and B.col4=A.col3
OR
B.col3= 'Y' and B.col4=A.col4
OR
.
.
.
.
)
)
Now this query runs for more than 4 minutes and the execution plan showed a table spool which was data size of 600 MB and looked like this -
I changed this to conditional JOIN to UNIONs -
With CTE AS (
select
A.col1,
A.col2,
.
.
.
from tableA A
JOIN tableB B ON
(
B.col3= 'X' and B.col4=A.col3
)
UNION
select
A.col1,
A.col2,
.
.
.
from tableA A
JOIN tableB B ON
(
B.col3= 'Y' and B.col4=A.col4
)
UNION
.
.
.
)
Now the spool with the large number of rows vanished and the run time reduced to 17 seconds. However when I check the IO statistics, the logical reads are higher than the previous slower query. What could the reason be ?
sql-server-2014 join execution-plan statistics union
bumped to the homepage by Community♦ 7 mins ago
This question has answers that may be good or bad; the system has marked it active so that they can be reviewed.
add a comment |
I have a query the first part of which looks like this -
With CTE AS (
select DISTINCT
A.col1,
A.col2,
.
.
.
from tableA A
JOIN tableB B ON
(
B.col3= 'X' and B.col4=A.col3
OR
B.col3= 'Y' and B.col4=A.col4
OR
.
.
.
.
)
)
Now this query runs for more than 4 minutes and the execution plan showed a table spool which was data size of 600 MB and looked like this -
I changed this to conditional JOIN to UNIONs -
With CTE AS (
select
A.col1,
A.col2,
.
.
.
from tableA A
JOIN tableB B ON
(
B.col3= 'X' and B.col4=A.col3
)
UNION
select
A.col1,
A.col2,
.
.
.
from tableA A
JOIN tableB B ON
(
B.col3= 'Y' and B.col4=A.col4
)
UNION
.
.
.
)
Now the spool with the large number of rows vanished and the run time reduced to 17 seconds. However when I check the IO statistics, the logical reads are higher than the previous slower query. What could the reason be ?
sql-server-2014 join execution-plan statistics union
I have a query the first part of which looks like this -
With CTE AS (
select DISTINCT
A.col1,
A.col2,
.
.
.
from tableA A
JOIN tableB B ON
(
B.col3= 'X' and B.col4=A.col3
OR
B.col3= 'Y' and B.col4=A.col4
OR
.
.
.
.
)
)
Now this query runs for more than 4 minutes and the execution plan showed a table spool which was data size of 600 MB and looked like this -
I changed this to conditional JOIN to UNIONs -
With CTE AS (
select
A.col1,
A.col2,
.
.
.
from tableA A
JOIN tableB B ON
(
B.col3= 'X' and B.col4=A.col3
)
UNION
select
A.col1,
A.col2,
.
.
.
from tableA A
JOIN tableB B ON
(
B.col3= 'Y' and B.col4=A.col4
)
UNION
.
.
.
)
Now the spool with the large number of rows vanished and the run time reduced to 17 seconds. However when I check the IO statistics, the logical reads are higher than the previous slower query. What could the reason be ?
sql-server-2014 join execution-plan statistics union
sql-server-2014 join execution-plan statistics union
asked Aug 13 '18 at 4:10
virvir
112
112
bumped to the homepage by Community♦ 7 mins ago
This question has answers that may be good or bad; the system has marked it active so that they can be reviewed.
bumped to the homepage by Community♦ 7 mins ago
This question has answers that may be good or bad; the system has marked it active so that they can be reviewed.
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
The reason is that AND
and OR
has the same precedence and the long condition x AND y OR z AND w OR v AND...
without round brackets has a bit different meaning than you think.
Your query can be rewritten that way:
FROM tableA AS A
JOIN tableB AS B ON ( B.col3 = 'X' AND B.col4 = A.col3 )
OR ( B.col3 = 'Y' AND B.col4 = A.col4 )
OR (. . . . .)
I've tried this too and got the same run time and statistics(and plan) as the first query(the one with the spool with 29 million rows). And I think AND has precedence over OR.
– vir
Aug 13 '18 at 4:45
AND
andOR
actually have different precedence. In absence of brackets,AND
has higher priority. YourON
predicate, therefore, would work the same way with or without those brackets. That said, however, specifying the brackets makes the intent clearer, as not everyone can constantly remember the precedence rules.
– Andriy M
Aug 13 '18 at 12:38
add a comment |
Your Answer
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "182"
};
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',
autoActivateHeartbeat: false,
convertImagesToLinks: false,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: null,
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
});
}
});
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%2fdba.stackexchange.com%2fquestions%2f214739%2fquery-with-conditional-join-running-slow%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
The reason is that AND
and OR
has the same precedence and the long condition x AND y OR z AND w OR v AND...
without round brackets has a bit different meaning than you think.
Your query can be rewritten that way:
FROM tableA AS A
JOIN tableB AS B ON ( B.col3 = 'X' AND B.col4 = A.col3 )
OR ( B.col3 = 'Y' AND B.col4 = A.col4 )
OR (. . . . .)
I've tried this too and got the same run time and statistics(and plan) as the first query(the one with the spool with 29 million rows). And I think AND has precedence over OR.
– vir
Aug 13 '18 at 4:45
AND
andOR
actually have different precedence. In absence of brackets,AND
has higher priority. YourON
predicate, therefore, would work the same way with or without those brackets. That said, however, specifying the brackets makes the intent clearer, as not everyone can constantly remember the precedence rules.
– Andriy M
Aug 13 '18 at 12:38
add a comment |
The reason is that AND
and OR
has the same precedence and the long condition x AND y OR z AND w OR v AND...
without round brackets has a bit different meaning than you think.
Your query can be rewritten that way:
FROM tableA AS A
JOIN tableB AS B ON ( B.col3 = 'X' AND B.col4 = A.col3 )
OR ( B.col3 = 'Y' AND B.col4 = A.col4 )
OR (. . . . .)
I've tried this too and got the same run time and statistics(and plan) as the first query(the one with the spool with 29 million rows). And I think AND has precedence over OR.
– vir
Aug 13 '18 at 4:45
AND
andOR
actually have different precedence. In absence of brackets,AND
has higher priority. YourON
predicate, therefore, would work the same way with or without those brackets. That said, however, specifying the brackets makes the intent clearer, as not everyone can constantly remember the precedence rules.
– Andriy M
Aug 13 '18 at 12:38
add a comment |
The reason is that AND
and OR
has the same precedence and the long condition x AND y OR z AND w OR v AND...
without round brackets has a bit different meaning than you think.
Your query can be rewritten that way:
FROM tableA AS A
JOIN tableB AS B ON ( B.col3 = 'X' AND B.col4 = A.col3 )
OR ( B.col3 = 'Y' AND B.col4 = A.col4 )
OR (. . . . .)
The reason is that AND
and OR
has the same precedence and the long condition x AND y OR z AND w OR v AND...
without round brackets has a bit different meaning than you think.
Your query can be rewritten that way:
FROM tableA AS A
JOIN tableB AS B ON ( B.col3 = 'X' AND B.col4 = A.col3 )
OR ( B.col3 = 'Y' AND B.col4 = A.col4 )
OR (. . . . .)
answered Aug 13 '18 at 4:38
KondybasKondybas
2,646912
2,646912
I've tried this too and got the same run time and statistics(and plan) as the first query(the one with the spool with 29 million rows). And I think AND has precedence over OR.
– vir
Aug 13 '18 at 4:45
AND
andOR
actually have different precedence. In absence of brackets,AND
has higher priority. YourON
predicate, therefore, would work the same way with or without those brackets. That said, however, specifying the brackets makes the intent clearer, as not everyone can constantly remember the precedence rules.
– Andriy M
Aug 13 '18 at 12:38
add a comment |
I've tried this too and got the same run time and statistics(and plan) as the first query(the one with the spool with 29 million rows). And I think AND has precedence over OR.
– vir
Aug 13 '18 at 4:45
AND
andOR
actually have different precedence. In absence of brackets,AND
has higher priority. YourON
predicate, therefore, would work the same way with or without those brackets. That said, however, specifying the brackets makes the intent clearer, as not everyone can constantly remember the precedence rules.
– Andriy M
Aug 13 '18 at 12:38
I've tried this too and got the same run time and statistics(and plan) as the first query(the one with the spool with 29 million rows). And I think AND has precedence over OR.
– vir
Aug 13 '18 at 4:45
I've tried this too and got the same run time and statistics(and plan) as the first query(the one with the spool with 29 million rows). And I think AND has precedence over OR.
– vir
Aug 13 '18 at 4:45
AND
and OR
actually have different precedence. In absence of brackets, AND
has higher priority. Your ON
predicate, therefore, would work the same way with or without those brackets. That said, however, specifying the brackets makes the intent clearer, as not everyone can constantly remember the precedence rules.– Andriy M
Aug 13 '18 at 12:38
AND
and OR
actually have different precedence. In absence of brackets, AND
has higher priority. Your ON
predicate, therefore, would work the same way with or without those brackets. That said, however, specifying the brackets makes the intent clearer, as not everyone can constantly remember the precedence rules.– Andriy M
Aug 13 '18 at 12:38
add a comment |
Thanks for contributing an answer to Database Administrators Stack Exchange!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
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%2fdba.stackexchange.com%2fquestions%2f214739%2fquery-with-conditional-join-running-slow%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