I can get same result for these queries, but which one is the fastest, and most efficient?
where in() or inner join?
SELECT `stats`.`userid`,`stats`.`sumpoint`
FROM `stats`
INNER JOIN users
ON `stats`.`userid` = `users`.`userid`
WHERE `users`.`nick` = '$nick'
ORDER BY `statoylar`.`sumpoint` DESC limit 0,10
and
SELECT `stats`.`userid`,`stats`.`sumpoint`
FROM `stats`
WHERE userid
IN (
SELECT userid
FROM `users`
WHERE `users`.`nick` = '$nick'
)
ORDER BY `stats`.`sumpoint` DESC limit 0,10
“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.
I ran some tests and the results show that it is actually very close, but the WHERE clause is actually slightly faster! =) I absolutely agree that it makes more sense to apply the filter on the WHERE clause, I was just curious as to the performance implications.
No, it doesn't. Query optimizer will transform your code anyway. You better choose a convention and go with it.
A where clause will generally increase the performance of the database. Generally, it is more expensive to return data and filter in the application. The database can optimize the query, using indexes and partitions. The database may be running in parallel, executing the query in parallel.
to check the performance execute both Query with EXPLAIN SELECT ....
AFAIK, INNER JOIN
is faster than IN
btw what is your type of table engine MYISAM
or INNODB
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