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

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

然后dept表也插些干扰数据

测试语句

开始验证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') ;

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

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 ;

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

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

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

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 ;

在简单比较下时间

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

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

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

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

  1. inner join 和 exists 效率_一阵骚操作,我把SQL执行效率提高了10000000倍!

    作者:风过无痕-唐 http://www.cnblogs.com/tangyanbo/p/4462734.html 场景 我用的数据库是mysql5.6,下面简单的介绍下场景 课程表: create ...

  2. 简单实例使用_一个关于jmeter的使用简单实例

    收益: 从一个小的测试实例中可以快速简单的了解到jmeter的一些使用技能,比如关联,正则表达式的使用,if控制器,jmeter作用域. 背景: 收到一个很简单的性能测试的需求: 压测下面页面:htt ...

  3. java 热词_一个热词推荐的简单实现

    为什么想做这个东西 一直好奇像亚马逊这类网站的搜索是如何做到推荐的,最近刚好看到一篇文章:Redis 与搜索热词推荐,然而只写了思路.所以,就是想自己实现一个. 先上个效果图,再聊: P.S. 按四年 ...

  4. inner join 和 exists 效率_19条效率至少提高3倍的MySQL技巧

    点击蓝色"程序猿DD"关注我哟 加个"星标",不忘签到哦 来源:https://zhuanlan.zhihu.com/p/49888088 关注我,回复口令获取 ...

  5. inner join 和 exists 效率_19 个让 MySQL 效率提高 3 倍的 SQL 优化技巧

    在介绍具体的 SQL 调优的方法前,我们先来简单了解下 MySQL 调优金字塔理论. 如上图所示,数据库优化维度有四个:硬件.系统配置.数据库表结构.SQL 及索引. 优化成本: 硬件>系统配置 ...

  6. mysql join 与 cross join 效率_浅析Mysql Join语法以及性能优化

    在讲MySQL的Join语法前还是先回顾一下联结的语法,呵呵,其实连我自己都忘得差不多了,那就大家一起温习吧,这里我有个比较简便的记忆方法,内外联结的区别是内联结将去除所有不符合条件的记录,而外联结则 ...

  7. mysql exists 效率_Mysql之exists和inner join效率问题(1)

    使用两张大小差距比较大的表来进行测试 前言:在使用laravel的过程中发现whereHas实现的方式是exists子查询,又听说exists尽量少使用,容易导致慢查询,于是就有了接下来两篇文章. 两 ...

  8. db2嵌套查询效率_提高DB2 查询性能的常用方法

    [IT168 技术文档]本文先对 DB2 提供的几种用于提高查询性能的相关工具和命令进行介绍,然后根据笔者的工作经验介绍一些常用的技巧和方法来提高查询性能.主要集中于如何创建和维护索引.改写查询以及改 ...

  9. db2嵌套查询效率_提高 DB2 查询性能的常用方法

    提高 DB2 查询性能的常用方法 张大为 和 陈力 2007 年 9 月 28 日发布 简介 随着 DB2 应用的逐渐增多,越来越多的数据库开发人员在项目开发过程中都会遇到查询过于复杂,导致性能难以接 ...

最新文章

  1. 橡鹭科技获源码资本亿元融资,美团原核心高管加盟,发力服务机器人
  2. php 匹配标记,php – 正则表达式匹配没有标记的链接
  3. 一文看懂Python(二)-----字符串篇
  4. 快速排序的C++实现(利用二分分治法)
  5. CNN: 查看tensorflow Pb模型所有层的名字
  6. AcWing之找替换空格
  7. java虚拟机堆栈工作原理_java虚拟机工作原理?
  8. python字符串数组_python将字符串转换成数组的方法
  9. django 设置登录成功后的页面
  10. 正则爬取猫眼电影排行榜 待修改版
  11. 安卓系统的电视机_天猫魔盒强刷机教程,把天猫魔盒刷成安卓系统教程?
  12. ut-890/485-usb驱动 FOR Linux
  13. CCNA考试题库中英文翻译版及答案13
  14. 任玉刚【Android开发艺术探索】读后笔记三
  15. 台式计算机驱动程序未被安装,Win10提示该设备的驱动程序未被安装。(代码28)
  16. 代替Beyond Compare的代码对比工具WinMerge
  17. S32DS Components组件配置
  18. MyBatis从入门到精通(九):MyBatis高级结果映射之一对一映射
  19. 超详细手把手教你cordova开发使用指南+自定义插件,jsbridge
  20. 红旗linux桌面版反应慢,亲自感受红旗Linux系统优化大全

热门文章

  1. 在Docker中配置ASP.NETCore的HTTPS模式
  2. 记一次排查线上程序内存的忽高忽低,又是大集合惹祸了
  3. 直接使用汇编编写 .NET Standard 库
  4. .Net Core中IOC容器的使用
  5. dotNET Core 中怎样操作AD(续1)
  6. 【DevOps进行时】自动化测试之单元测试
  7. 【WPF on .NET Core 3.0】 Stylet演示项目 - 简易图书管理系统(2)
  8. IdentityServer4实战 - 谈谈 JWT Token 的安全策略
  9. Build 2017 | 一文看懂微软 Build 2017 大会:让 AI 走向边缘
  10. 个性化配置你的SQL Server on Linux