inner join 和 exists 效率_一个in、exists、join的简单测试
创建两张表先单独插入两条数据
然后批量插入部门号为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的简单测试相关推荐
- inner join 和 exists 效率_一阵骚操作,我把SQL执行效率提高了10000000倍!
作者:风过无痕-唐 http://www.cnblogs.com/tangyanbo/p/4462734.html 场景 我用的数据库是mysql5.6,下面简单的介绍下场景 课程表: create ...
- 简单实例使用_一个关于jmeter的使用简单实例
收益: 从一个小的测试实例中可以快速简单的了解到jmeter的一些使用技能,比如关联,正则表达式的使用,if控制器,jmeter作用域. 背景: 收到一个很简单的性能测试的需求: 压测下面页面:htt ...
- java 热词_一个热词推荐的简单实现
为什么想做这个东西 一直好奇像亚马逊这类网站的搜索是如何做到推荐的,最近刚好看到一篇文章:Redis 与搜索热词推荐,然而只写了思路.所以,就是想自己实现一个. 先上个效果图,再聊: P.S. 按四年 ...
- inner join 和 exists 效率_19条效率至少提高3倍的MySQL技巧
点击蓝色"程序猿DD"关注我哟 加个"星标",不忘签到哦 来源:https://zhuanlan.zhihu.com/p/49888088 关注我,回复口令获取 ...
- inner join 和 exists 效率_19 个让 MySQL 效率提高 3 倍的 SQL 优化技巧
在介绍具体的 SQL 调优的方法前,我们先来简单了解下 MySQL 调优金字塔理论. 如上图所示,数据库优化维度有四个:硬件.系统配置.数据库表结构.SQL 及索引. 优化成本: 硬件>系统配置 ...
- mysql join 与 cross join 效率_浅析Mysql Join语法以及性能优化
在讲MySQL的Join语法前还是先回顾一下联结的语法,呵呵,其实连我自己都忘得差不多了,那就大家一起温习吧,这里我有个比较简便的记忆方法,内外联结的区别是内联结将去除所有不符合条件的记录,而外联结则 ...
- mysql exists 效率_Mysql之exists和inner join效率问题(1)
使用两张大小差距比较大的表来进行测试 前言:在使用laravel的过程中发现whereHas实现的方式是exists子查询,又听说exists尽量少使用,容易导致慢查询,于是就有了接下来两篇文章. 两 ...
- db2嵌套查询效率_提高DB2 查询性能的常用方法
[IT168 技术文档]本文先对 DB2 提供的几种用于提高查询性能的相关工具和命令进行介绍,然后根据笔者的工作经验介绍一些常用的技巧和方法来提高查询性能.主要集中于如何创建和维护索引.改写查询以及改 ...
- db2嵌套查询效率_提高 DB2 查询性能的常用方法
提高 DB2 查询性能的常用方法 张大为 和 陈力 2007 年 9 月 28 日发布 简介 随着 DB2 应用的逐渐增多,越来越多的数据库开发人员在项目开发过程中都会遇到查询过于复杂,导致性能难以接 ...
最新文章
- oledb 访问接口sqlncli10返回了消息 没有活动事务_这样理解分布式事务你是不是就会懂了?...
- 组合内容_剑与远征:亚龙组合成型,新的更新内容,比以前更强了?
- win7笔记本为手机共享wifi
- 5895. 获取单值网格的最小操作数
- 3dmax如何显示参考图_3dmax软件当中的参照文件的作用与应用,了解一下
- 机器视觉系统设计难点有哪些?
- Android ViewPager+TabHost实现首页导航
- 【转载】快速掌握一个语言最常用的50%
- 视频教程-SPSS统计自动化-VBA脚本开发-其他
- 阿铭Linux_网站维护学习笔记20190416
- SylixOS之OK6410开发板系统烧写
- 【python报错解决】findfont: Font family [‘Arial‘] not found. Falling back to DejaVu Sans.
- php 2个数组并集,php中数组的并集、交集和差集函数介绍_PHP教程
- HTML背景样式简单介绍
- 阿里实习生电话面试果断被鄙视
- PHP将ppt转成图片查看
- 云服务器是一个个虚拟机,云服务器就是一个虚拟机吗
- Java 获取本地时间与网络时间
- Excle中如何快速筛选数据
- 积分计算曲线围绕X轴旋转形成的立体体积