Home » Performance » MySQL » The Slowness of "WHERE something IN (SELECT....)"
The Slowness of "WHERE something IN (SELECT....)" [message #2959] Tue, 15 April 2008 10:55 Go to next message
tobias74  is currently offline tobias74
Messages: 4
Registered: April 2008
Junior Member
Hello!

When discussing performance issues on inner-join-queries, people tend to advise using a subquery like "SELECT id FROM table WHERE value IN (SELECT something FROM othertable WHERE original_condition)".

As this strategy did not work for me at all, I kept looking for some solution and came across an article which can be found at http://www.artfulsoftware.com/infotree/queries.php#568

Now my question would be, whether "IN (*subquery*)" is ever fit to optimize a query, and if so, how?

Tobias


Re: The Slowness of "WHERE something IN (SELECT....)" [message #2960 is a reply to message #2959 ] Tue, 15 April 2008 12:57 Go to previous message
Hubert Roksor
Messages: 3
Registered: April 2008
Junior Member
tobias74 wrote on Tue, 15 April 2008 16:55

people tend to advise using a subquery

Hmm, not MySQL people I'm afraid. MySQL is known to execute such subqueries quite innefficiently, that's why those should be rewritten as an INNER JOIN whenever possible.

tobias74 wrote on Tue, 15 April 2008 16:55

Now my question would be, whether "IN (*subquery*)" is ever fit to optimize a query, and if so, how?

I can't think of any case where a subquery in the IN clause would be more efficient than a JOIN or a derived table (subquery in the FROM clause, also called materialization). If there's one, it must be a tricky edge case.

Note that starting with 6.0, most of subqueries in a IN clause will be transformed automatically and executed as one of the other forms described above (semi-join, materialization). You'll find more info about that in this MySQL Forge article.
Previous Topic:dump/reload in master-master
Next Topic:Is there a way to control query expiration time in cache?
Goto Forum:
  



Current Time: Fri Jan 9 00:11:09 EST 2009

Total time taken to generate the page: 0.02445 seconds
.:: Contact :: Home :: MySQL Support by Percona.com ::.

Powered by: FUDforum 2.7.5.
Copyright ©2001-2006 FUD Forum Bulletin Board Software

MySQL Performance | Forum authority Badge