首页 > 数据库 >Mysql中用exists代替in

Mysql中用exists代替in

时间:2024-08-30 23:25:12浏览次数:4  
标签:exists 中用 查询 user Mysql where id select

exists对外表用loop逐条查询,每次查询都会查看exists的条件语句,当 exists里的条件语句能够返回记录行时(无论记录行是的多少,只要能返回),条件就为真,返回当前loop到的这条记录,反之如果exists里的条 件语句不能返回记录行,则当前loop到的这条记录被丢弃,exists的条件就像一个bool条件,当能返回结果集则为true,不能返回结果集则为 false

如下:

select * from user where exists (select 1);

对user表的记录逐条取出,由于子条件中的select 1永远能返回记录行,那么user表的所有记录都将被加入结果集,所以与 select * from user;是一样的

又如下

select * from user where exists (select * from user where userId = 0);

可以知道对user表进行loop时,检查条件语句(select * from user where userId = 0),由于userId永远不为0,所以条件语句永远返回空集,条件永远为false,那么user表的所有记录都将被丢弃

not exists与exists相反,也就是当exists条件有结果集返回时,loop到的记录将被丢弃,否则将loop到的记录加入结果集

总的来说,如果A表有n条记录,那么exists查询就是将这n条记录逐条取出,然后判断n遍exists条件 

 

 

in查询相当于多个or条件的叠加,这个比较好理解,比如下面的查询

select * from user where userId in (1, 2, 3);

等效于

select * from user where userId = 1 or userId = 2 or userId = 3;

not in与in相反,如下

select * from user where userId not in (1, 2, 3);

等效于

select * from user where userId != 1 and userId != 2 and userId != 3;

总的来说,in查询就是先将子查询条件的记录全都查出来,假设结果集为B,共有m条记录,然后在将子查询条件的结果集分解成m个,再进行m次查询

 

值得一提的是,in查询的子条件返回结果必须只有一个字段,例如

select * from user where userId in (select id from B);

而不能是

select * from user where userId in (select id, age from B);

而exists就没有这个限制

 

下面来考虑exists和in的性能

考虑如下SQL语句

1: select * from A where exists (select * from B where B.id = A.id);

2: select * from A where A.id in (select id from B);

 

查询1.可以转化以下伪代码,便于理解

for ($i = 0; $i < count(A); $i++) {

  $a = get_record(A, $i); #从A表逐条获取记录

  if (B.id = $a[id]) #如果子条件成立

    $result[] = $a;

}

return $result;

大概就是这么个意思,其实可以看到,查询1主要是用到了B表的索引,A表如何对查询的效率影响应该不大

 

假设B表的所有id为1,2,3,查询2可以转换为

select * from A where A.id = 1 or A.id = 2 or A.id = 3;

这个好理解了,这里主要是用到了A的索引,B表如何对查询影响不大

 

下面再看not exists 和 not in

1. select * from A where not exists (select * from B where B.id = A.id);

2. select * from A where A.id not in (select id from B);

看查询1,还是和上面一样,用了B的索引

而对于查询2,可以转化成如下语句

select * from A where A.id != 1 and A.id != 2 and A.id != 3;

可以知道not in是个范围查询,这种!=的范围查询无法使用任何索引,等于说A表的每条记录,都要在B表里遍历一次,查看B表里是否存在这条记录

故not exists比not in效率高

 

mysql中的in语句是把外表和内表作hash 连接,而exists语句是对外表作loop循环,每次loop循环再对内表进行查询。一直大家都认为exists比in语句的效率要高,这种说法其实是不准确的。这个是要区分环境的。
 

如果查询的两个表大小相当,那么用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要快。  in 与 =的区别  select name from student where name in ('zhang','wang','li','zhao');  与  select name from student where name='zhang' or name='li' or name='wang' or name='zhao'  的结果是相同的。

标签:exists,中用,查询,user,Mysql,where,id,select
From: https://www.cnblogs.com/ataoxz/p/18389664

相关文章

  • 使用docker安装mysql
    安装Docker1、Docker教程地址:https://www.runoob.com/docker/centos-docker.install.html2、安装docker命令:yuminstalldocker-io3、启动docker命令:servicedockerstart4、查看docker是否启动成功命令:ps-ef|grepdocker使用docker安装mysql1、查询mysql命令:docke......
  • Mysql基础练习题 596.查询至少有5个学生的所有班级 (力扣)
    596.查询至少有5个学生的所有班级建表插入数据:CreatetableIfNotExistsCourses(studentvarchar(255),classvarchar(255))TruncatetableCoursesinsertintoCourses(student,class)values('A','Math')insertintoCourses(student,class)values(......
  • MYSQL-事务篇
    事务是一组操作的集合,他是不可分割的工作单位,事务会把所有的操作作为一个整体一起向系统提交或撤销操作请求,即这些操作要么同时成功要么同时失败。默认mysql的事务是自动提交的,也就是说,当执行一条DML语句,mysql会立即隐式的提交事务。事务的四大特性原子性:事务是不可分割的最......
  • [昌哥IT课堂]使用MySQL Shell 部署沙盒数据库实例详解
     概述:这部分解释了如何使用AdminAPI设置沙盒部署。部署和使用本地MySQL的沙盒实例是开始探索AdminAPI的好方法。在将功能部署到生产服务器之前,您可以在本地测试功能。AdminAPI具有内置功能,用于创建正确配置的沙箱实例,以便在本地部署的情况下与InnoDBCluster、InnoDBClusterS......
  • 怎么清除mysql磁盘mysql删除的数据
    在MySQL中,被删除的数据默认情况下是被放置在一个空间上被标记为可重用但实际并未立即释放的状态。这允许快速重用该空间,但如果需要彻底从磁盘上清除这些数据,可以使用OPTIMIZETABLE命令。请注意,OPTIMIZETABLE并不能保证彻底删除数据,因为它的目的是重新组织表并释放未使用的空间......
  • 设置 Nginx、MySQL 日志轮询
    title:设置Nginx、MySQL日志轮询tags:author:ChingeYangdate:2024-8-301.Nginx设置日志轮询机器直接安装的:/etc/logrotate.d/nginx/var/log/nginx/*.log{dailymissingokrotate30compressdelaycompressno......
  • 云计算:LNMP网站架构,前期准备,安装php,安装MySQL
    准备工作(初始化)1.关闭防火墙systemctl disablefirewalld --now    //直接永久关闭防火墙2.关闭SELINUX 查看SELINUX:getenforce永久关闭:[root@localhost~]#vim/etc/selinux/configSELINUX=enforcing|disabled或者[root@localhost~]#sed-i's/^S......
  • 【MySQL 11】索引 (带思维导图)
    文章目录......
  • 【MySQL 12】事务管理 (带思维导图)
    文章目录......
  • 【MySQL 14】用户管理
    文章目录......