IN 的执行效率太低了,怎么提高? 学写了一个SQL,都是用IN的,发现效率非常低。EXISTS不太会用。
有A、B两个表,A(pid,name),B(cid,pid)。
现在要做的就是对A表进行筛选,如果A表中PID不在B表中,那么就把A表中的这条数据删掉。
DELETE FROM A a WHERE a.pid in(select b.pid from A b where b.pid not in(select c.pid from B c))
哪位高手能不能帮我重写一下这个SQL,让其效率提高?谢谢 ------解决方案-------------------- DELETE FROM A a WHERE not exists(select 1 from B b where a.pid = b.pid) ------解决方案--------------------
delete from a where not exists (select 1 from b where b.pid=a.pid);
------解决方案-------------------- 1)DELETE FROM A a WHERE a.pid in(select b.pid from A b where b.pid not in(select c.pid from B c))