Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

inner join and where in() clause performance?

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
like image 219
Okan Kocyigit Avatar asked Mar 11 '11 15:03

Okan Kocyigit


People also ask

Which is faster inner join or 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.

Is it faster to filter on join or WHERE clause?

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.

Does adding WHERE condition in join in SQL improves performance?

No, it doesn't. Query optimizer will transform your code anyway. You better choose a convention and go with it.

Does adding WHERE clause improve performance?

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.


1 Answers


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

like image 112
diEcho Avatar answered Sep 21 '22 19:09

diEcho