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
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.
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.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With