跳到主要内容

1 篇博文 含有标签「子查询优化」

查看所有标签

SQL优化技巧 - IN子查询优化

· 阅读需 11 分钟
PawSQL Team
Optimize your SQL Queries by Clicks!

问题定义

为了获取最近一年内有订单的用户信息,可以使用以下的三种写法去实现,它们在语义上是等价的。那它们的性能如何?如何对它们进行优化?这是本文讨论的问题。

  • Query1 - IN子查询(= ANY)
select * from customer where c_custkey in (select o_custkey from orders where O_ORDERDATE>=current_date - interval 1 year)
  • Query2 - EXISTS子查询
select * from customer where exists (select * from orders where c_custkey = o_custkey and O_ORDERDATE>=current_date - interval 1 year)