Mysql的exist与in的区别
2024-09-03 22:21:41
如果查询的两个表大小相当,那么用in和exists差别不大。
如果两个表中一个较小,一个是大表,则子查询表大的用exists,子查询表小的用in:
例如:表A(小表),表B(大表)
1:
select * from A where cc in (select cc from B) 效率低,用到了A表上cc列的索引;
select * from A where exists(select cc from B where cc=A.cc) 效率高,用到了B表上cc列的索引。
相反的
2:
select * from B where cc in (select cc from A) 效率高,用到了B表上cc列的索引;
select * from B where exists(select cc from A where cc=B.cc) 效率低,用到了A表上cc列的索引。
not in 和not exists如果查询语句使用了not in 那么内外表都进行全表扫描,没有用到索引;而not extsts 的子查询依然能用到表上的索引。所以无论那个表大,用not exists都比not in要快。
最新文章
- [Evolutionary Algorithm] 进化算法简介
- Linux usual cmd
- vld使用
- Fiddler抓包之IOS
- Java——列表框:JList
- 小心对待query_cache_size
- Web版报表的架构和设计思路
- JAVA IO 类库详解
- C# ";error CS1729: 'XXClass' does not contain a constructor that takes 0 arguments";的解决方案
- C++第二课(2013.9.27 )
- 基于visual Studio2013解决C语言竞赛题之0609矩阵处理
- Windows Phone 8.1 多媒体(1):相片
- Unity3d之流光效果
- IOS 利用图片设置背景
- 02_IO操作的基本规律(InputStream,OutputStream,Reader,Writer,FileReader,FileWriter,BufferedReader,BufferedWri
- 【Json】fastjson与jackson常用操作记录
- c# 后台处理获取的JSON 数据
- python 定时修改数据库
- React-使用imutable.js来管理store中的数据
- Java如何将每个单词的第一个字符转为大写?