浅析MySQL中exists与in的使用

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‘

的结果是相同的。

时间: 2024-08-29 04:22:45

浅析MySQL中exists与in的使用的相关文章

MySQL 中 EXISTS 的用法

在MySQL中 EXISTS 和 IN 的用法有什么关系和区别呢? 假定数据库中有两个表 分别为 表 a 和表 b create table a ( a_id int, a_name varchar(20) ) create table b ( b_id int, b_name varchar(20) ) 那么 select * from a where a_name in (select b_name from b) 这条SQL语句的意义很明显是选取满足where条件下 a 中的所有列的数据

关于MySQL 中 EXISTS 的用法

在MySQL中 EXISTS 和 IN 的用法有什么关系和区别呢? 假定数据库中有两个表 分别为 表 a 和表 b create table a ( a_id int, a_name varchar(20) ) create table b ( b_id int, b_name varchar(20) ) 那么 select * from a where a_name in (select b_name from b) 这条SQL语句的意义很明显是选取满足where条件下 a 中的所有列的数据

003 mysql中exists的使用

mysql中exists可以使用在两个地方,一个是在创建库或者表的时候,配合if 使用,一个是在子查询中. # 和if一起使用,对库或表都可以使用 create database if not exists yaco charset utf8; drop database if exists yaco; # 在子查询中使用,返回True或者False,条件满足时执行前面的代码 select * from tb1 where exists(select id from tb2 where name

浅析MySQL中concat以及group_concat的使用

说明: 本文中使用的例子均在下面的数据库表tt2下执行: 一.concat()函数 1.功能:将多个字符串连接成一个字符串. 2.语法:concat(str1, str2,...) 返回结果为连接参数产生的字符串,如果有任何一个参数为null,则返回值为null. 3.举例: 例1:select concat (id, name, score) as info from tt2; 中间有一行为null是因为tt2表中有一行的score值为null. 例2:在例1的结果中三个字段id,name,s

mysql中exists的详细说明

之前碰到了一道题,下面简要说明一下: 表结构如下: CREATE TABLE `testa` ( `id` int(11) NOT NULL AUTO_INCREMENT, `cid` int(11) DEFAULT NULL comment '产品ID', `uid` int(11) DEFAULT NULL comment '用户ID', `buytime` int(11) DEFAULT NULL comment '购买时间', PRIMARY KEY (`id`)) ENGINE=Inn

浅析MySQL中的Index Condition Pushdown (ICP 索引条件下推)和Multi-Range Read(MRR 索引多范围查找)查询优化

本文出处:http://www.cnblogs.com/wy123/p/7374078.html(保留出处并非什么原创作品权利,本人拙作还远远达不到,仅仅是为了链接到原文,因为后续对可能存在的一些错误进行修正或补充,无他) ICP优化原理 Index Condition Pushdown (ICP),也称为索引条件下推,体现在执行计划的上是会出现Using index condition(Extra列,当然Extra列的信息太多了,只能做简单分析)ICP原理通俗讲就是,查询过程中,直接在查询引擎

MySQL中exists和in的区别及使用场景

exists和in的使用方式: select * from A where exists (select * from B where A.id=B.id); select * from A where A.id in (select id from B); 1.exists是对外表做loop循环,每次loop循环再对内表(子查询)进行查询,那么是对内表的查询使用的索引,而外表有多大都需要遍历,不可避免,故内表大的使用exists,可加快效率: 2.in是把外表和内表做hash连接,先查询内表,

【转】MySQL中EXISTS的用法

原文链接:https://www.cnblogs.com/qlqwjy/p/8598091.html 比如在Northwind数据库中有一个查询为 SELECT c.CustomerId,CompanyName FROM Customers c WHERE EXISTS( SELECT OrderID FROM Orders o WHERE o.CustomerID=c.CustomerID) 这里面的EXISTS是如何运作呢?子查询返回的是OrderId字段,可是外面的查询要找的是Custom

辛星浅析MySQL中的last_insert_id()

很多时候,我们在插入一条数据的时候,我们需要知道它的主键是多少,它会自动返回最后一个insert并且为auto_increment列位置的第一个发生的值. 而且需要注意的是产生的id每次连接后保存在服务器中,这意味着函数向一个给定客户端返回的值就是该客户端产生对影响auto_increment列的最新语句后的第一个auto_increment值. 而且这个值不会被其他客户端影响,即使它们产生了它们自己的auto_increment值,这个行为是保证了我们能够找回自己的id而不用担心其他客户端的活