使用exists
使用exists代替in
1exists只检查行的存在性,in 检查实际的值,所以existsd的性能比in好
验证

select * from emp 
where deptno in(select  deptno from dept where   loc='NEW YORK');

select * from emp e
where  exists(select 1 from dept d where d.deptno=e.deptno and loc='NEW YORK');


这里写图片描述
使用exists代替distinct
1exists只检查行的存在性,distinct用于禁止重复行的显示,而且distinct在禁止重复行的显示前需要排序检索的行,所以exists的性能比distinct好
验证

select distinct e.deptno,d.dname  from emp e,dept d
where e.deptno=d.deptno;

select  d.deptno,d.dname  from dept d
where exists(select 1 from emp e where e.deptno=d.deptno);

这里写图片描述

posted on 2017-06-04 10:20  2637282556  阅读(230)  评论(0编辑  收藏  举报