Join vs. sub-query

前端 未结 19 2012
广开言路
广开言路 2020-11-21 05:05

I am an old-school MySQL user and have always preferred JOIN over sub-query. But nowadays everyone uses sub-query, and I hate it; I don\'t know why.

19条回答
  •  被撕碎了的回忆
    2020-11-21 05:21

    • A general rule is that joins are faster in most cases (99%).
    • The more data tables have, the subqueries are slower.
    • The less data tables have, the subqueries have equivalent speed as joins.
    • The subqueries are simpler, easier to understand, and easier to read.
    • Most of the web and app frameworks and their "ORM"s and "Active record"s generate queries with subqueries, because with subqueries are easier to split responsibility, maintain code, etc.
    • For smaller web sites or apps subqueries are OK, but for larger web sites and apps you will often have to rewrite generated queries to join queries, especial if a query uses many subqueries in the query.

    Some people say "some RDBMS can rewrite a subquery to a join or a join to a subquery when it thinks one is faster than the other.", but this statement applies to simple cases, surely not for complicated queries with subqueries which actually cause a problems in performance.

提交回复
热议问题