inner join 和 exists 效率_一个in、exists、join的简单测试
創(chuàng)建兩張表先單獨(dú)插入兩條數(shù)據(jù)
然后批量插入部門(mén)號(hào)為10,20,30,40的數(shù)據(jù)各10499099條
然后dept表也插些干擾數(shù)據(jù)
測(cè)試語(yǔ)句
開(kāi)始驗(yàn)證in和exists和join
先比較個(gè)占比多的部門(mén),再比較占比少的
1、 in 占比多
select count(*) from scott.EMP_TEST e where e.deptno in (select d.deptno from scott.DEPT_TEST d where d.dname='SALES') ;
跟真實(shí)執(zhí)行計(jì)劃基本一樣,所以之后的都用autotrace來(lái)看
2.exists 占比多
select count(*) from scott.EMP_TEST e where exists (select d.deptno from scott.DEPT_TEST d where d.dname='SALES' and e.deptno=d.deptno) ;
3.join 占比多
select count(*) from scott.EMP_TEST e inner join scott.DEPT_TEST d on e.deptno=d.deptno and d.dname='SALES' ;
http://4.in 占比少
select count(*) from scott.EMP_TEST e where e.deptno in (select d.deptno from scott.DEPT_TEST d where d.dname='TEST') ;
5.exists 占比少
select count(*) from scott.EMP_TEST e where exists (select d.deptno from scott.DEPT_TEST d where d.dname='TEST' and e.deptno=d.deptno) ;
6.join 占比少
select count(*) from scott.EMP_TEST e inner join scott.DEPT_TEST d on e.deptno=d.deptno and d.dname='TEST' ;
7.not in 占比多
select count(*) from scott.EMP_TEST e where e.deptno not in (select d.deptno from scott.DEPT_TEST d where d.dname<>'SALES') ;
8.not exists 占比多
select count(*) from scott.EMP_TEST e where not exists (select d.deptno from scott.DEPT_TEST d where d.dname<>'SALES' and e.deptno=d.deptno) ;
9.join 占比多
select count(*) from scott.EMP_TEST e left join scott.DEPT_TEST d on e.deptno=d.deptno and d.dname<>'SALES' where d.deptno is null ;
10.not in 占比少
select count(*) from scott.EMP_TEST e where e.deptno not in (select d.deptno from scott.DEPT_TEST d where d.dname<>'TEST') ;
11. not exists 占比少
select count(*) from scott.EMP_TEST e where not exists (select d.deptno from scott.DEPT_TEST d where d.dname<>'TEST' and e.deptno=d.deptno) ;
12. join 占比少
select count(*) from scott.EMP_TEST e left join scott.DEPT_TEST d on e.deptno=d.deptno and d.dname<>'TEST' where d.deptno is null ;
下面創(chuàng)建兩個(gè)簡(jiǎn)單索引來(lái)測(cè)試下
1、 in 占比多
select count(*) from scott.EMP_TEST e where e.deptno in (select d.deptno from scott.DEPT_TEST d where d.dname='SALES') ;
分析不走索引的原因是因?yàn)榻y(tǒng)計(jì)信息不全,收集下統(tǒng)計(jì)信息
2.exists 占比多
select count(*) from scott.EMP_TEST e where exists (select d.deptno from scott.DEPT_TEST d where d.dname='SALES' and e.deptno=d.deptno) ;
3.join 占比多
select count(*) from scott.EMP_TEST e inner join scott.DEPT_TEST d on e.deptno=d.deptno and d.dname='SALES' ;
http://4.in 占比少
select count(*) from scott.EMP_TEST e where e.deptno in (select d.deptno from scott.DEPT_TEST d where d.dname='TEST') ;
5.exists 占比少
select count(*) from scott.EMP_TEST e where exists (select d.deptno from scott.DEPT_TEST d where d.dname='TEST' and e.deptno=d.deptno) ;
6.join 占比少
select count(*) from scott.EMP_TEST e inner join scott.DEPT_TEST d on e.deptno=d.deptno and d.dname='TEST' ;
7.not in 占比多
select count(*) from scott.EMP_TEST e where e.deptno not in (select d.deptno from scott.DEPT_TEST d where d.dname<>'SALES') ;
8.not exists 占比多
select count(*) from scott.EMP_TEST e where not exists (select d.deptno from scott.DEPT_TEST d where d.dname<>'SALES' and e.deptno=d.deptno) ;
9.join 占比多
select count(*) from scott.EMP_TEST e left join scott.DEPT_TEST d on e.deptno=d.deptno and d.dname<>'SALES' where d.deptno is null ;
10.not in 占比少
select count(*) from scott.EMP_TEST e where e.deptno not in (select d.deptno from scott.DEPT_TEST d where d.dname<>'TEST') ;
11. not exists 占比少
select count(*) from scott.EMP_TEST e where not exists (select d.deptno from scott.DEPT_TEST d where d.dname<>'TEST' and e.deptno=d.deptno) ;
12. join 占比少
select count(*) from scott.EMP_TEST e left join scott.DEPT_TEST d on e.deptno=d.deptno and d.dname<>'TEST' where d.deptno is null ;
在簡(jiǎn)單比較下時(shí)間
可以看出在相同條件下,執(zhí)行計(jì)劃是相同的,時(shí)間消耗也是一樣的
因?yàn)橛信笥颜f(shuō)not exists的子查詢會(huì)走索引not in索引失效,所以not exists會(huì)快,所以為了說(shuō)服他又做了點(diǎn)補(bǔ)充
并沒(méi)有什么差別。而且not in和not exists都會(huì)使索引失效,但是不影響子查詢的索引使用!!!!所以直接說(shuō)誰(shuí)比誰(shuí)快的都是不負(fù)責(zé)任的說(shuō)法,還是要具體情況具體分析。分情況使用。那些說(shuō)not exists比not in快的,這種情況很多,因?yàn)椴煌脑蛟斐伤麄兊膕ql的執(zhí)行計(jì)劃不同了,所以效率也不同了
拋開(kāi)執(zhí)行計(jì)劃和實(shí)際情景,不考慮數(shù)據(jù)情況,數(shù)據(jù)量、索引情況甚至統(tǒng)計(jì)信息等這些因素,直接說(shuō)哪一種比較快都是不靠譜的
總結(jié)
以上是生活随笔為你收集整理的inner join 和 exists 效率_一个in、exists、join的简单测试的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: python 写入excel_一行一行整
- 下一篇: unable to launch什么意思