Subqueries vs joins

前端 未结 14 1995
闹比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:42

    With a subquery, you have to re-execute the 2nd SELECT for each result, and each execution typically returns 1 row.

    With a join, the 2nd SELECT returns a lot more rows, but you only have to execute it once. The advantage is that now you can join on the results, and joining relations is what a database is supposed to be good at. For example, maybe the optimizer can spot how to take better advantage of an index now.

提交回复
热议问题