Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MySQL Performance: JOIN ON vs WHERE [duplicate]

Possible Duplicates:
MySQL: Inner join vs Where
Explicit vs implicit SQL joins

Is there any difference performance wise when we run join queries using "JOIN ON" and "WHERE" clause? (regardless of the number of tables or the number of entries in the tables)

Not sure whether this topic has already been discussed over. Even if so, I wish to know whether with the latest versions of mySQL(5.1 and above), things have changed.

An Explain statement clearly shows a lot of difference in the number of rows taken for consideration.

The Syntax I am using is:

Using JOIN ON

SELECT <field names> FROM <table1> JOIN <table2> ON <join_condition> AND JOIN <table3> ON <join_condition>  AND JOIN <table4> ON <join_condition>  ....  

Using WHERE

SELECT <field names>  FROM <table names list separated by comma>  WHERE <join_condition>  AND <join_condition>  AND <join_condition>  ....  

So not sure whether usage of JOIN ON or WHERE clause would make a difference. Please assist.

like image 608
Vasanthakumar V Avatar asked Mar 17 '11 12:03

Vasanthakumar V


People also ask

Are joins faster than WHERE clause?

“Is there a performance difference between putting the JOIN conditions in the ON clause or the WHERE clause in MySQL?” No, there's no difference. The following queries are algebraically equivalent inside MySQL and will have the same execution plan.

Which join has better performance?

There is not a "better" or a "worse" join type. They have different meaning and they must be used depending on it. In your case, you probably do not have employees with no work_log (no rows in that table), so LEFT JOIN and JOIN will be equivalent in results.

Do joins slow down query?

Joins: If your query joins two tables in a way that substantially increases the row count of the result set, your query is likely to be slow. There's an example of this in the subqueries lesson. Aggregations: Combining multiple rows to produce a result requires more computation than simply retrieving those rows.


2 Answers

Here is a good analysis of these two approaches: SQL left join vs multiple tables on FROM line?

This explanation is general, I'm not too sure what MySQL does in this matter; but either way the point is that a JOIN is always more explicit and clear and can be moved from one engine to another with no major changes in the logic of the query.

like image 137
DeveloperInToronto Avatar answered Sep 19 '22 21:09

DeveloperInToronto


I suspect that using "JOIN" is doing a "LEFT JOIN". Try using an "INNER JOIN" and see if the explain statement is the same.

SELECT <field names> FROM <table1> INNER JOIN <table2> ON <join_condition> INNER JOIN <table3> ON <join_condition>  INNER JOIN <table4> ON <join_condition>  ... 

When using an inner join, I would not expect there to be a performance difference between your two queries. I would worry more about having the proper indexes. Make sure the join conditions can make a match using an indexed column.

like image 40
Nick Clark Avatar answered Sep 20 '22 21:09

Nick Clark