下面是我的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) );
表已正确索引。可以重写此代码以获得更好的性能吗?
您也可以尝试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 );