Subqueries vs joins

前端 未结 14 1874
闹比i
闹比i 2020-11-22 16:10

I refactored a slow section of an application we inherited from another company to use an inner join instead of a subquery like:

WHERE id IN (SELECT id FROM          


        
相关标签:
14条回答
  • 2020-11-22 16:34

    This question is somewhat general, so here's a general answer:

    Basically, queries take longer when MySQL has tons of rows to sort through.

    Do this:

    Run an EXPLAIN on each of the queries (the JOIN'ed one, then the Subqueried one), and post the results here.

    I think seeing the difference in MySQL's interpretation of those queries would be a learning experience for everyone.

    0 讨论(0)
  • 2020-11-22 16:34

    It isn't so much the subquery as the IN clause, although joins are at the foundation of at least Oracle's SQL engine and run extremely quickly.

    0 讨论(0)
  • 2020-11-22 16:35

    Here's an example of how subqueries are evaluated in MySQL 6.0.

    The new optimizer will convert this kind of subqueries into joins.

    0 讨论(0)
  • 2020-11-22 16:39

    The where subquery has to run 1 query for each returned row. The inner join just has to run 1 query.

    0 讨论(0)
  • 2020-11-22 16:40

    before the queries are run against the dataset they are put through a query optimizer, the optimizer attempts to organize the query in such a fashion that it can remove as many tuples (rows) from the result set as quickly as it can. Often when you use subqueries (especially bad ones) the tuples can't be pruned out of the result set until the outer query starts to run.

    With out seeing the the query its hard to say what was so bad about the original, but my guess would be it was something that the optimizer just couldn't make much better. Running 'explain' will show you the optimizers method for retrieving the data.

    0 讨论(0)
  • 2020-11-22 16:40

    Taken from the Reference Manual (14.2.10.11 Rewriting Subqueries as Joins):

    A LEFT [OUTER] JOIN can be faster than an equivalent subquery because the server might be able to optimize it better—a fact that is not specific to MySQL Server alone.

    So subqueries can be slower than LEFT [OUTER] JOINS.

    0 讨论(0)
提交回复
热议问题