使用exists 使用exists代替in
1.exists只检查行的存在性,in 检查实际的值,所以exists的性能比in好
验证
select * from emp where deptno in(select deptno from dept where loc='NEW YORK');select * from emp ewhere exists(select 1 from dept d where d.deptno=e.deptno and loc='NEW YORK');
使用exists代替distinct
1.exists只检查行的存在性,distinct用于禁止重复行的显示,而且distinct在禁止重复行的显示前需要排序检索的行,所以exists的性能比distinct好
验证
select distinct e.deptno,d.dname from emp e,dept dwhere e.deptno=d.deptno;select d.deptno,d.dname from dept dwhere exists(select 1 from emp e where e.deptno=d.deptno);