当前位置:网站首页>Mysql的in和exists用法区别
Mysql的in和exists用法区别
2022-08-03 07:40:00 【棒棒吃不胖】
一.构建模拟环境
表A有某学校所有学生的姓名
表B有某学校所有的班级
要求通过班级查询所有学生的姓名
二.比较
1)首先用in查询姓名
select * from A where name in (select name from B);
该命令等价于
先走子查询 select name from B;
再走A表 select * from A where A.name=B.name;
它相当于一个大循环,嵌套着里面的一个小循环
其中子查询为外部循环,主查询语句是内部的循环
可以看出:
内表的结果无论如何都需要遍历(外层循环),这是不可避免的。
所以子查询A小于B,使用in性能越好。
2)用exists查询姓名
select * from A where exists ( select 1 from B where A.name=B.name );
该命令等价于:
先走表A select * from A;
再走表B select * from B where A.name=B.name;
它也是一个大循环嵌套一个内循环
不过,此处表A查询是外循环
可以看出:
外层表(A)的结果无论如何都需要遍历(外层循环),这是不可避免的。
所以A数据量大于B,使用exists性能越好。
三.结论
当A是外层循环(必须遍历),B是内层循环
A数据量小于B数据量,用in
A数据量大于B数据量,用exists(此时B是外层循环)
一句话:选择外层循环表数据量小的。
边栏推荐
猜你喜欢

ORB-SLAM2提取特征点

AI mid-stage sequence labeling task: three data set construction process records

Fortify白盒神器20.1.1下载及安装(非百度网盘)

pyspark---encode the suuid interval (based on the number of exposures and clicks)

千万级别的表分页查询非常慢,怎么办?

进程的创建

【云原生--Kubernetes】Pod容器与镜像拉取策略
Golang协程goroutine的调度与状态变迁分析

【云原生--Kubernetes】Pod重启策略

【云原生--Kubernetes】kubectl命令详解
随机推荐
DSP Trick:向量长度估算
information_schema
《剑指Offer》刷题之打印从1到最大的n位数
用diskpart的offline命令弹出顽固硬盘
酷雷曼上新6大功能,全景营销持续加码
23届微软秋招内推
基于SSM开发的的小区物业管理系统小程序源码
解决GANs训练中模式崩塌/训练崩溃的十五个方法
STL-vector容器
mysql服务器上的mysql这个实例中表的介绍
【Kaggle实战】泰坦尼克号生存人数预测(从零到提交到Kaggle再到模型的保存与恢复)
ORB-SLAM2提取特征点
Mysql如何对两张表的相同字段,同时查询两张数据表
加速FinOps实践,为企业降本增效
- display image API OpenCV 】 【 imshow () to a depth (data type) at different image processing methods
thop 使用心得
Evaluate: A detailed introduction to the introduction of huggingface evaluation indicator module
ArcEngine (six) use the tool tool to realize the zoom in, zoom out and translation of the pull box
升级
Detailed explanation of cause and effect diagram of test case design method