Which one is faster: correlated subqueries or join?

后端 未结 1 1947
臣服心动
臣服心动 2020-12-20 12:54

I know we can do correlated subqueries and join. But which one is faster? Is there a golden rule or I must measure both?

相关标签:
1条回答
  • 2020-12-20 13:25

    First, a correlated subquery really is a type of join. There is no golden rule about which produces the best execution plan. If you are interested in performance, you need to try out the different forms to see what works best. Or, at least, look at the exeuction plans to make that decision.

    In general, I tend to avoid correlated subqueries for a couple of reasons. First, they can almost always be written without the correlation. Second, many query engines turn them into nested loop joins (albeit using indexes), and other join strategies might be better. In such cases, correlated subqueries make it difficult to parallelize the query. Third, correlated subqueries are usualy in either the SELECT or WHERE clauses. I like for all my tables to be in the FROM clause.

    In MySQL however, correlated subqueries are often the most efficient way to do a query. This is especially true when using a subquery in an IN clause. So, there is no golden rule.

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