Postgresql why is INNER JOIN so much slower than WHERE











up vote
0
down vote

favorite












I have 2 tables where I copy file name from one table to another in an update operation. Using INNER JOIN makes the query run in 22 seconds when there are just ~4000 rows. Using a WHERE clause allows it to run it in about 200 milliseconds. How and why is this happening, does the INNER JOIN result in additional looping?



Example 1 using INNER JOIN - Takes 22 seconds when table a has about 4k records.



UPDATE table_a SET file_name = tmp.file_name FROM
(
SELECT b.customer_id, b.file_name, b.file_id FROM table_b AS b WHERE b.status = 'A'
) tmp
INNER JOIN table_a AS a
ON tmp.customer_id=a.customer_id AND tmp.file_id=a.file_id;


Example 2 using WHERE runs in about 200 ms.



UPDATE table_a AS a SET file_name = tmp.file_name FROM
(
SELECT b.customer_id, b.file_name, b.file_id FROM table_b AS b WHERE b.status = 'A'
) tmp
WHERE tmp.customer_id=a.customer_id AND tmp.file_id=a.file_id;









share|improve this question




















  • 1




    I think the first query is missing information that connects the first reference to table_a to the second one. Because of that, I think it updates waaay more data, (all rows, multiple times). So, what is your goal anyway with the first query? Why would you want to write it like that?
    – GolezTrol
    Nov 19 at 19:22












  • Yes, that seems to be it. I was just wondering how the INNER JOIN ended up being a cross join and it looks like it is because the 2 table a references are different.
    – Abe
    Nov 19 at 19:47















up vote
0
down vote

favorite












I have 2 tables where I copy file name from one table to another in an update operation. Using INNER JOIN makes the query run in 22 seconds when there are just ~4000 rows. Using a WHERE clause allows it to run it in about 200 milliseconds. How and why is this happening, does the INNER JOIN result in additional looping?



Example 1 using INNER JOIN - Takes 22 seconds when table a has about 4k records.



UPDATE table_a SET file_name = tmp.file_name FROM
(
SELECT b.customer_id, b.file_name, b.file_id FROM table_b AS b WHERE b.status = 'A'
) tmp
INNER JOIN table_a AS a
ON tmp.customer_id=a.customer_id AND tmp.file_id=a.file_id;


Example 2 using WHERE runs in about 200 ms.



UPDATE table_a AS a SET file_name = tmp.file_name FROM
(
SELECT b.customer_id, b.file_name, b.file_id FROM table_b AS b WHERE b.status = 'A'
) tmp
WHERE tmp.customer_id=a.customer_id AND tmp.file_id=a.file_id;









share|improve this question




















  • 1




    I think the first query is missing information that connects the first reference to table_a to the second one. Because of that, I think it updates waaay more data, (all rows, multiple times). So, what is your goal anyway with the first query? Why would you want to write it like that?
    – GolezTrol
    Nov 19 at 19:22












  • Yes, that seems to be it. I was just wondering how the INNER JOIN ended up being a cross join and it looks like it is because the 2 table a references are different.
    – Abe
    Nov 19 at 19:47













up vote
0
down vote

favorite









up vote
0
down vote

favorite











I have 2 tables where I copy file name from one table to another in an update operation. Using INNER JOIN makes the query run in 22 seconds when there are just ~4000 rows. Using a WHERE clause allows it to run it in about 200 milliseconds. How and why is this happening, does the INNER JOIN result in additional looping?



Example 1 using INNER JOIN - Takes 22 seconds when table a has about 4k records.



UPDATE table_a SET file_name = tmp.file_name FROM
(
SELECT b.customer_id, b.file_name, b.file_id FROM table_b AS b WHERE b.status = 'A'
) tmp
INNER JOIN table_a AS a
ON tmp.customer_id=a.customer_id AND tmp.file_id=a.file_id;


Example 2 using WHERE runs in about 200 ms.



UPDATE table_a AS a SET file_name = tmp.file_name FROM
(
SELECT b.customer_id, b.file_name, b.file_id FROM table_b AS b WHERE b.status = 'A'
) tmp
WHERE tmp.customer_id=a.customer_id AND tmp.file_id=a.file_id;









share|improve this question















I have 2 tables where I copy file name from one table to another in an update operation. Using INNER JOIN makes the query run in 22 seconds when there are just ~4000 rows. Using a WHERE clause allows it to run it in about 200 milliseconds. How and why is this happening, does the INNER JOIN result in additional looping?



Example 1 using INNER JOIN - Takes 22 seconds when table a has about 4k records.



UPDATE table_a SET file_name = tmp.file_name FROM
(
SELECT b.customer_id, b.file_name, b.file_id FROM table_b AS b WHERE b.status = 'A'
) tmp
INNER JOIN table_a AS a
ON tmp.customer_id=a.customer_id AND tmp.file_id=a.file_id;


Example 2 using WHERE runs in about 200 ms.



UPDATE table_a AS a SET file_name = tmp.file_name FROM
(
SELECT b.customer_id, b.file_name, b.file_id FROM table_b AS b WHERE b.status = 'A'
) tmp
WHERE tmp.customer_id=a.customer_id AND tmp.file_id=a.file_id;






sql postgresql inner-join






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 19 at 19:51

























asked Nov 19 at 19:13









Abe

4,09873565




4,09873565








  • 1




    I think the first query is missing information that connects the first reference to table_a to the second one. Because of that, I think it updates waaay more data, (all rows, multiple times). So, what is your goal anyway with the first query? Why would you want to write it like that?
    – GolezTrol
    Nov 19 at 19:22












  • Yes, that seems to be it. I was just wondering how the INNER JOIN ended up being a cross join and it looks like it is because the 2 table a references are different.
    – Abe
    Nov 19 at 19:47














  • 1




    I think the first query is missing information that connects the first reference to table_a to the second one. Because of that, I think it updates waaay more data, (all rows, multiple times). So, what is your goal anyway with the first query? Why would you want to write it like that?
    – GolezTrol
    Nov 19 at 19:22












  • Yes, that seems to be it. I was just wondering how the INNER JOIN ended up being a cross join and it looks like it is because the 2 table a references are different.
    – Abe
    Nov 19 at 19:47








1




1




I think the first query is missing information that connects the first reference to table_a to the second one. Because of that, I think it updates waaay more data, (all rows, multiple times). So, what is your goal anyway with the first query? Why would you want to write it like that?
– GolezTrol
Nov 19 at 19:22






I think the first query is missing information that connects the first reference to table_a to the second one. Because of that, I think it updates waaay more data, (all rows, multiple times). So, what is your goal anyway with the first query? Why would you want to write it like that?
– GolezTrol
Nov 19 at 19:22














Yes, that seems to be it. I was just wondering how the INNER JOIN ended up being a cross join and it looks like it is because the 2 table a references are different.
– Abe
Nov 19 at 19:47




Yes, that seems to be it. I was just wondering how the INNER JOIN ended up being a cross join and it looks like it is because the 2 table a references are different.
– Abe
Nov 19 at 19:47












1 Answer
1






active

oldest

votes

















up vote
2
down vote



accepted










The queries are doing totally different things. The first is updating every row in table_a with the expression. I am guessing that there are even multiple updates on the same row.



The two table_as in the first version are two different references to the table. The effect is a cross join because you have no conditions combining them.



The second method is the correct syntax for what you want to do in Postgres.






share|improve this answer





















  • Out of curiosity, how can the equivalent of the second query be written in terms of INNER JOIN?
    – Abe
    Nov 19 at 19:49






  • 1




    @Abe . . . You need join conditions (in the where clause between table_a and a) -- perhaps on the primary key.
    – Gordon Linoff
    Nov 19 at 19:55











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
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53381176%2fpostgresql-why-is-inner-join-so-much-slower-than-where%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








up vote
2
down vote



accepted










The queries are doing totally different things. The first is updating every row in table_a with the expression. I am guessing that there are even multiple updates on the same row.



The two table_as in the first version are two different references to the table. The effect is a cross join because you have no conditions combining them.



The second method is the correct syntax for what you want to do in Postgres.






share|improve this answer





















  • Out of curiosity, how can the equivalent of the second query be written in terms of INNER JOIN?
    – Abe
    Nov 19 at 19:49






  • 1




    @Abe . . . You need join conditions (in the where clause between table_a and a) -- perhaps on the primary key.
    – Gordon Linoff
    Nov 19 at 19:55















up vote
2
down vote



accepted










The queries are doing totally different things. The first is updating every row in table_a with the expression. I am guessing that there are even multiple updates on the same row.



The two table_as in the first version are two different references to the table. The effect is a cross join because you have no conditions combining them.



The second method is the correct syntax for what you want to do in Postgres.






share|improve this answer





















  • Out of curiosity, how can the equivalent of the second query be written in terms of INNER JOIN?
    – Abe
    Nov 19 at 19:49






  • 1




    @Abe . . . You need join conditions (in the where clause between table_a and a) -- perhaps on the primary key.
    – Gordon Linoff
    Nov 19 at 19:55













up vote
2
down vote



accepted







up vote
2
down vote



accepted






The queries are doing totally different things. The first is updating every row in table_a with the expression. I am guessing that there are even multiple updates on the same row.



The two table_as in the first version are two different references to the table. The effect is a cross join because you have no conditions combining them.



The second method is the correct syntax for what you want to do in Postgres.






share|improve this answer












The queries are doing totally different things. The first is updating every row in table_a with the expression. I am guessing that there are even multiple updates on the same row.



The two table_as in the first version are two different references to the table. The effect is a cross join because you have no conditions combining them.



The second method is the correct syntax for what you want to do in Postgres.







share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 19 at 19:15









Gordon Linoff

748k34285391




748k34285391












  • Out of curiosity, how can the equivalent of the second query be written in terms of INNER JOIN?
    – Abe
    Nov 19 at 19:49






  • 1




    @Abe . . . You need join conditions (in the where clause between table_a and a) -- perhaps on the primary key.
    – Gordon Linoff
    Nov 19 at 19:55


















  • Out of curiosity, how can the equivalent of the second query be written in terms of INNER JOIN?
    – Abe
    Nov 19 at 19:49






  • 1




    @Abe . . . You need join conditions (in the where clause between table_a and a) -- perhaps on the primary key.
    – Gordon Linoff
    Nov 19 at 19:55
















Out of curiosity, how can the equivalent of the second query be written in terms of INNER JOIN?
– Abe
Nov 19 at 19:49




Out of curiosity, how can the equivalent of the second query be written in terms of INNER JOIN?
– Abe
Nov 19 at 19:49




1




1




@Abe . . . You need join conditions (in the where clause between table_a and a) -- perhaps on the primary key.
– Gordon Linoff
Nov 19 at 19:55




@Abe . . . You need join conditions (in the where clause between table_a and a) -- perhaps on the primary key.
– Gordon Linoff
Nov 19 at 19:55


















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • 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.





Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


Please pay close attention to the following guidance:


  • 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.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53381176%2fpostgresql-why-is-inner-join-so-much-slower-than-where%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

Wiesbaden

Marschland

Dieringhausen