inner join 和 exists 效率_一个in、exists、join的简单测试

创建两张表先单独插入两条数据

01c79757c5e0935576d6d5b78596cd10.png


然后批量插入部门号为10,20,30,40的数据各10499099条

362aa9fc8cbfc420ce99d2a0fbe79568.png


然后dept表也插些干扰数据

cc911c171668ac70e2b3181816c44669.png


测试语句

062618b22accc48e1b8ceb6b3eb091bb.png


开始验证in和exists和join
先比较个占比多的部门,再比较占比少的
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') ;

d795ca7f96585012249558da01b295e1.png

b97d2c6483e7735f9eaf93eab578b55d.png

9856824f9c52a5c887f4b5ecdd3ab00b.png


跟真实执行计划基本一样,所以之后的都用autotrace来看
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) ;

953bac4e9c47e1df78bf0298f31b8b23.png


3.join 占比多
select count(*) from scott.EMP_TEST e inner join scott.DEPT_TEST d on e.deptno=d.deptno and d.dname='SALES' ;

efa2a6f3ef6f44a1b11ac991020f448b.png

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') ;

eb7b7b6d786a9d7f45a9a17e2b32a66d.png


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) ;

0631cbec03b17efd212d80629950d25a.png


6.join 占比少
select count(*) from scott.EMP_TEST e inner join scott.DEPT_TEST d on e.deptno=d.deptno and d.dname='TEST' ;

28641b9d753d1cf2d1457edf0fca8ecc.png


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') ;

e0644490dd52492102166aebab6696ac.png


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) ;

47e9ca0f42ca4326c476ddc1840a4f36.png


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 ;

4cfd9ed4ca2e4efe949b984a72c22865.png


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') ;

32369b0a3c828a9ecf222e0db0d3c668.png


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) ;

5b057cd32e88bc9837259b3ea0004f6b.png


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 ;

2ff9ad4b1dfdb2a293da16321a88491a.png


下面创建两个简单索引来测试下

3fa9a1853b0aaa396260354ba83b91f8.png


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') ;

9856824f9c52a5c887f4b5ecdd3ab00b.png


分析不走索引的原因是因为统计信息不全,收集下统计信息

c686d3b4e22522a4546efeda1f5cba07.png


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) ;

85c204d36db70289d7498ebb002ff9b3.png


3.join 占比多
select count(*) from scott.EMP_TEST e inner join scott.DEPT_TEST d on e.deptno=d.deptno and d.dname='SALES' ;

ee1e5c409ffa3fba4d8302489ba84bdf.png

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') ;

9dc0852db6013cec9d04c34cf560a740.png


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) ;

c32028c17f02efd142a830e0f9a7a05f.png


6.join 占比少
select count(*) from scott.EMP_TEST e inner join scott.DEPT_TEST d on e.deptno=d.deptno and d.dname='TEST' ;

0f336a6abbc95196655fd6da2dd9b358.png


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') ;

dabb310cd9c90b747e9719fec716ef16.png


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) ;

0abdef4e332b991e4edc3e9e01bfd9d8.png


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 ;

98fe9a56522232df8ac6001f75fe27a0.png


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') ;

425ba93aff54a79416032c55eaf9fc76.png


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) ;

e4afbbdfd2074b3800a7cd9e42e4c84f.png


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 ;

0328b4ab061c2d33ea0a0aa551ab5bc1.png


在简单比较下时间

3b7b828dfbb85e9159e1f361ac4c75f2.png

0c908c5406f36993139c914e40e80888.png

e84062bab5801014eeac5e20512fd9e2.png

4c81a90b3592414b48e8ad1ec3efddbd.png

可以看出在相同条件下,执行计划是相同的,时间消耗也是一样的
因为有朋友说not exists的子查询会走索引not in索引失效,所以not exists会快,所以为了说服他又做了点补充

6d0ea959daafc54e0b9bd42cf3f2acd3.png

f648c5e74f356c525b8e2a70d5045fdb.png

e843190db9fd82d89282f7cf80e1e713.png

4fd135eab920195341838e5f8a4177ad.png

7858687a2ce91f9724f5eaba4c370139.png

7d38822e8b4bb5746a66049a042c7008.png

fdcb6a8ee2f2562bc6eccb6f321cc3e2.png

878233f98977f9121c63fb4c32f26804.png

2f0739b255dfd2d71347dbf1e7e4dbc3.png

并没有什么差别。而且not in和not exists都会使索引失效,但是不影响子查询的索引使用!!!!所以直接说谁比谁快的都是不负责任的说法,还是要具体情况具体分析。分情况使用。那些说not exists比not in快的,这种情况很多,因为不同的原因造成他们的sql的执行计划不同了,所以效率也不同了

抛开执行计划和实际情景,不考虑数据情况,数据量、索引情况甚至统计信息等这些因素,直接说哪一种比较快都是不靠谱的

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/292795.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

httpModules 与 httpHandlers

httpModules 与 httpHandlers ASP.NET对请求处理的过程&#xff1a;当请求一个*.aspx文件的时候&#xff0c;这个请求会被inetinfo.exe进程截获&#xff0c;它判断文件的后缀&#xff08;aspx&#xff09;之后&#xff0c;将这个请求转交给ASPNET_ISAPI.dll&#xff0c;ASPNET_…

uva10160(dfs+状态压缩)

题意&#xff1a;给出n个点&#xff0c;以及m条边&#xff0c;这些边代表着这些点相连&#xff0c;修一个电力站&#xff0c;若在某一点修一个站&#xff0c;那么与这个点相连的点都可以通电&#xff0c;问所有的点都通电的话至少要修多少个电力站........思路&#xff1a;最多…

CAD数据与ArcGIS数据的互转换(转载)

CAD数据往往是分层管理的&#xff0c;将CAD数据转成arcgis数据&#xff0c;最担心的莫过于丢失了属性数据&#xff0c;arcgis9.2提供了一种方法&#xff0c;可以将CAD数据完整的转换为personal geodatabase&#xff0c;属性信息不会丢失&#xff0c;方法如下&#xff1a;ArcToo…

愚人节的礼物-栈

题目: 四月一日快到了,Vayko想了个愚人的好办法――送礼物。嘿嘿,不要想的太好,这礼物可没那么简单,Vayko为了愚人,准备了一堆盒子,其中有一个盒子里面装了礼物。盒子里面可以再放零个或者多个盒子。假设放礼物的盒子里不再放其他盒子。 用()表示一个盒子,B表示礼物,…

Task.Factory.StartNew 和 Task.Factory.FromAsync 有什么区别?

咨询区 soleiljy假设我们有一个涉及IO操作的方法 (读取数据库)&#xff0c;这个方法支持以同步或者异步的方式执行。同步方式IOMethod()异步方式BeginIOMethod() EndIOMethod()接下来我都用 Task 来包装这两个方法。public static void Main(){var task1 Task.Factory.StartN…

多比Web 3D展示(3D机房/3D监控)中间件多比Web 3D展示(3D机房/3D监控)中间件免费下载购买地址...

多比3D是实现3D场景搭建的软件开发包&#xff0c;可以创建广泛的3D应用&#xff0c;适用于高端制造、能源、国防军工、教育科研、城市规划及建筑环艺、生物医学等领域的虚拟仿真&#xff0c;应用于虚拟展示、虚拟设计、方案评审、虚拟装配、虚拟实训等工作环节。 特点与优势 支…

python 工资管理软件_智慧职教云课堂2020Python程序设计(深圳信息职业技术学院)题目答案...

智慧职教云课堂2020Python程序设计&#xff08;深圳信息职业技术学院)题目答案 更新时间&#xff1a;2020-11-23 20:41点击&#xff1a; 智慧职教云课堂2020Python程序设计&#xff08;深圳信息职业技术学院)题目答案 更多相关问题 【判断题】在声明类的成员属性时必须要用关键…

数据结构-Hash总结(二)

转载&#xff1a;http://blog.csdn.net/liufei_learning/article/details/19220391 理解Hash 哈希表(hash table)是从一个集合A到另一个集合B的映射(mapping)。 映射是一种对应关系&#xff0c;而且集合A的某个元素只能对应集合B中的一个元素。但反过来&#xff0c;集合B中的一…

中国已消失的九所世界级大学

全世界只有3.14 % 的人关注了爆炸吧知识众所周知&#xff0c;我们国家的大学数量在世界范围内数一数二&#xff0c;但是有很多赫赫有名的大学却在历史中被人们遗忘&#xff0c;下面我们一起回顾一下那些不该被忘记的大学。>>>>▌燕京大学燕京大学&#xff08;Yench…

IOS Table中Cell的重用reuse机制分析

2019独角兽企业重金招聘Python工程师标准>>> 解决代码&#xff1a; - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { NSString *CellIdentifier [NSString stringWithFormat:"Cell%d%d", …

子商城管理之签到活动

--建表脚本: --create sequence create sequence SIGN_ACTIVITY_SEQ;-- Create table create table SIGN_ACTIVITY (SIGN_ACTIVITY_ID NUMBER not null,START_TIME VARCHAR2(20),END_TIME VARCHAR2(20),ACTIVITY_INTRODUCTION VARCHAR2(300),REWA…

VS2017/2019 F12无法导航到定义

今天对项目里某个对象点F12转到定义&#xff0c;无法跳转,研究了一会儿&#xff0c;找到如下解决方案:首先确认该函数是否能够正确被跳转到..就是是否真实定义了&#xff0c;然后确保要跳转的定义源码在项目文件下&#xff08;而不是直接引用的DLL&#xff09;接下来关闭VS2017…

oracle 主键删除,联合主键的创建

1&#xff0c;主键的删除 ALTER TABLE TABLENAME DROP PRIMARY_KEY运行上面的SQL能够删除主键&#xff1b;假设不成功能够用ALTER TABLE TABLENAME DROP CONSTRAINTS COLUMN CASCADE; --删除约束ALTER TABLE TABLENAME DISABLE PRIMARY_COLUMN ; --设置被设置为主键的列为无效D…

unable to launch什么意思_都表示太...以至于,so … that…?与too… to …有着明显区别...

【2019年12月8日 百天英语-Day135】【华东师范大学-林森撰写】昨日内容复习提要&#xff1a;昨天学习了主要学习了 ①be able to …与can的区别。②如何通过不认识的单词&#xff0c;猜到作者表达的含义。昨天文章如下&#xff1a;表示有能力做某事&#xff0c;be able to …与…

数据结构-Hash总结(一):理论学习篇

转载请注明出处http://blog.csdn.net/yankai0219/article/details/8185796零、学习方法简要学习理论篇&#xff0c;进入程序学习篇&#xff0c;再回头学习理论篇和实践篇一、基本概念1.Hash定义Hash定义&#xff1a;将任意长度的输入&#xff0c;通过散列算法&#xff0c;变成固…

Prism For WPF Login对话框又简单又合理的方案之一

一、前言 这是一篇极简的小短文。首先感谢站长和各位WPF大佬对我的指导&#xff0c;我学到了很多&#xff0c;还是关于利用Prism做Login对话框的事情&#xff0c;看到站长发过一篇《WPF Prism框架Region失效了&#xff1f;》&#xff0c;目前我有一个自认为更合适的解决方法&am…

html5 的支持

html5.js让IE&#xff08;包括IE6&#xff09;支持HTML5元素方法 微软的最新浏览器IE8及以下IE版本对HTML5标签的支持是有限的&#xff0c;我们可以通过在网页中添加脚本的方式来解决目前IE浏览器对HTML5支持的问题。 让IE&#xff08;包括IE6&#xff09;支持HTML5元素&#x…

求指教、。。。关于调用so文件

问题描述今天同事给我发来一个文件&#xff0c;说让我通过android调用里面的函数文件是&#xff1a;里面的内容是&#xff1a;call_so.cpp文件的内容&#xff1a;#pragma pack(1) //非常重要的申明&#xff0c;内存对齐的方法&#xff0c;影响sizeof()的结果#include <stdio…