Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Query Performance INNER JOIN ON AND comparison

Tags:

I would like to know which one is best regarding performance between the 2 queries stated below or do they perform identically?

First one: [without WHERE clause, just AND with ON]

SELECT related_tabid AS tabid, label, t.name   FROM relatedlists r      INNER JOIN tab t         ON t.tabid = r.tabid           AND t.name = 'Leads'           AND r.is_active=1  and r.related_tabid <> 0           AND t.is_active=1   ORDER BY label 

Second one: [using WHERE clause, AND associated with where instead of ON ]

SELECT related_tabid AS tabid, label, t.name   FROM relatedlists r      INNER JOIN tab t         ON t.tabid = r.tabid        WHERE t.name = 'Leads'              AND r.is_active=1  and r.related_tabid <> 0              AND t.is_active=1   ORDER BY label 
like image 349
Rashidul Islam Avatar asked Mar 27 '13 09:03

Rashidul Islam


People also ask

Which is faster inner query or join?

The retrieval time of the query using joins almost always will be faster than that of a subquery. By using joins, you can maximize the calculation burden on the database i.e., instead of multiple queries using one join query.

Is inner join better than in?

If all you need is to check for matching rows in the other table but don't need any columns from that table, use IN. If you do need columns from the second table, use Inner Join.

Does join affect query performance?

Join order in SQL2008R2 server does unquestionably affect query performance, particularly in queries where there are a large number of table joins with where clauses applied against multiple tables. Although the join order is changed in optimisation, the optimiser does't try all possible join orders.


1 Answers

Both queries are the same because the join used is INNER JOIN. INNER JOIN basically it filters only rows that has at least a match on the other table. Even the two tables are interchange, the result is still the same.

But if you are joining them via LEFT JOIN, the two queries are different from each other and will yield different result.

like image 152
John Woo Avatar answered Oct 06 '22 08:10

John Woo