Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does "Join" order will lead to different query performance [duplicate]

I have 2 tables (table1 table2), table2 is larger than table1.

Script 1:

SELECT t1.A, t2.B
FROM table1 AS t1
JOIN table2 AS t2 ON t1.PK = t2.FK

Script 2:

SELECT t1.A, t2.B
FROM table2 AS t2
JOIN table1 AS t1 ON t1.PK = t2.FK

Does performance script #1 will be better than script #2?

like image 331
Sue Su Avatar asked Mar 17 '16 08:03

Sue Su


People also ask

Does order of join affect query performance?

Even though the join order has no impact on the final result, it still affects performance. The optimizer will therefore evaluate all possible join order permutations and select the best one.

Does the order of joins matter for performance SQL?

Basically, join order DOES matter because if we can join two tables that will reduce the number of rows needed to be processed by subsequent steps, then our performance will improve.

Does join order affect query performance in Oracle?

No. That's basic optimisation for the database; the optimizer will decide what is the best strategy to join the tables, regardless of the order in which they appear in the from clause. Save this answer.

How do you avoid duplicates in join?

Solution. Select column values in a specific order within rows to make rows with duplicate sets of values identical. Then you can use SELECT DISTINCT to remove duplicates. Alternatively, retrieve rows in such a way that near-duplicates are not even selected.


1 Answers

You are using an INNER JOIN so the answer is NO.

This will produce exactly the same amount of data due to the join type, and you are joining by the same relations in both queries, so they are basically identical.

It would have been different if you had used a LEFT JOIN , because in a left join, all the data from the master(left) table is kept, and all the matching data from the details table(right) .

So if you had used left join, and placed the larger table on the left side, then the query would have produced more data and probably would be slower then to put the larger table in the right side.

like image 106
sagi Avatar answered Oct 06 '22 00:10

sagi