Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Inner Join vs. Natural Join, speed-wise?

I'm having a discussion with a colleague of mine, and we're stuck on the point of whether an inner join is faster than a natural join simply due to the fact that the ON clause is specified in an inner join, and thus the SQL engine need not compare table structures to determine common columns.

Please share your thoughts/answers.

like image 921
Julian H. Lam Avatar asked Jan 30 '11 06:01

Julian H. Lam


2 Answers

It is going to depend on the DBMS, and to some extent on the tables joined, and the difference is generally not going to be measurable. With an INNER JOIN, there will be two copies of the common columns; with a NATURAL JOIN, there will be just one copy of the common columns.

If the intermediate results have to be written to disk or sorted, there will be more data to be managed, and there is slightly more data to be returned to the client over the client-server connection, so an INNER JOIN might take slightly longer than an NATURAL JOIN. If the common columns are big enough - long characters fields, for example - this size difference could be significant.

The difference in time to prepare the two types of join is completely negligible. Both have to read the information for all the columns in both tables involved in the join, and the processing of that data is substantially similar.

During execution, the copying of the data to form the intermediate results might be slightly more complex for a NATURAL JOIN - because you don't simply take a copy of each row - but you don't necessarily take a full copy of each row for an INNER JOIN either, so the difference is again negligible. And, as before, there is slightly less data to be copied in aggregate for the NATURAL JOIN, so there is a still a small advantage to it.

So, overall, a NATURAL JOIN might be slightly quicker than an INNER JOIN, but the difference is still likely to be negligible.

like image 129
Jonathan Leffler Avatar answered Nov 15 '22 19:11

Jonathan Leffler


I can't imagine it being so expensive to find common columns that you could support that hypothesis.

like image 22
Ciaran Keating Avatar answered Nov 15 '22 20:11

Ciaran Keating