小编典典

为什么SQL“ OT IN”这么慢?

sql

下面是我的SQL代码:

select count(1) 
from customers 
where id in(
    select custid
    from accounts
    where sid in(72,73,74,75,76,77,78,79)
) 
and id not in(
    select custid 
    from accounts 
    where sid in(80,81)
);

表已正确索引。可以重写此代码以获得更好的性能吗?


阅读 184

收藏
2021-04-28

共1个答案

小编典典

您也可以尝试EXISTS:

select count(1) 
from customers c
where exists (
    select 1
    from accounts a
    where sid in(72,73,74,75,76,77,78,79)
    and a.custid = c.custid
) 
and not exists (
    select 1
    from accounts a
    where sid in(80,81)
    and a.custid = c.custid
);
2021-04-28