Is it better to use INNER JOIN or EXISTS to find belonging to several in m2m relation?

前端 未结 4 1495
清歌不尽
清歌不尽 2021-02-18 15:03

Given m2m relation: items-categories I have three tables:

  • items,
  • categories and
  • ite
4条回答
  •  [愿得一人]
    2021-02-18 15:23

    You are using Join in Option A and subquery in Option B. The difference is:

    In most cases JOINs are faster than sub-queries and it is very rare for a sub-query to be faster.

    In JOINs RDBMS can create an execution plan that is better for your query and can predict what data should be loaded to be processed and save time, unlike the sub-query where it will run all the queries and load all their data to do the processing.

    The good thing in sub-queries is that they are more readable than JOINs: that's why most new SQL people prefer them; it is the easy way; but when it comes to performance, JOINS are better in most cases even though they are not hard to read too.

提交回复
热议问题