zoukankan      html  css  js  c++  java
  • mysql中 = 与in区别_浅析mysql中 exists 与 in 的区别,空判断

    1、exists的使用

    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条件 。

    2、in 的使用

    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);

    3.1、exists 与 in 的比较

    下面来考虑exists和in的性能,考虑如下SQL语句。

    前提条件:表A(小表),表B(大表)。

    查询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);

    以上查询使用了in语句,in()只执行一次,它查出B表中的所有id字段并缓存起来。之后检查A表的id是否与B表中的id相等,如果相等则将A表的记录加入结果集中,直到遍历完A表的所有记录。

    可以看出,当B表数据较大时不适合使用in(),因为它会B表数据全部遍历一次。

    如:A表有10000条记录,B表有1000000条记录,那么最多有可能遍历10000*1000000次,效率很差。

    再如:A表有10000条记录,B表有100条记录,那么最多有可能遍历10000*100次,遍历次数大大减少,效率大大提升。

    以上查询使用了exists语句,exists()会执行A.length次,它并不缓存exists()结果集,因为exists()结果集的内容并不重要;重要的是结果集中是否有记录,如果有则返回true,没有则返回false。

    当B表比A表数据大时适合使用exists(),因为它没有那么多的遍历操作,只需要再执行一次查询就行。

    如:A表有10000条记录,B表有1000000条记录,那么exists()会执行10000次去判断A表中的id是否与B表中的id相等。

    如:A表有10000条记录,B表有100000000条记录,那么exists()还是执行10000次,因为它只执行A.length次,可见B表数据越多,越适合exists()发挥效果。

    再如:A表有10000条记录,B表有100条记录,那么exists()还是执行10000次,还不如使用in()遍历10000*100次,因为in()是在内存里遍历比较,而exists()需要查询数据库,我们都知道查询数据库所消耗的性能更高,而内存比较很快。

    3.2、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);

    not in是个范围查询,这种!=的范围查询无法使用任何索引,那么内外表都进行全表扫描,没有用到索引;

    而not extsts 的子查询依然能用到表上的索引;

    所以无论那个表大,用not exists都比not in要快

    3.3、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' ;

    的结果是相同的。

    4.空判断 is null、is not null

    总结:

    如果查询的两个表大小相当,那么用in和exists差别不大。

    如果两个表中一个较小,一个是大表,则子查询表大的用exists,子查询表小的用in。

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


    ————————————————
    版权声明:本文为CSDN博主「科材」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。
    原文链接:https://blog.csdn.net/weixin_29959015/article/details/113331554

  • 相关阅读:
    经典算法之猴子吃桃
    VS2008C#Sqlserver2008数据库的连接以及增删改查
    在数组中随机插入数字且不重复
    菲波那切数列
    Js之AJAX
    经典算法之冒泡排序
    《Head First 设计模式》 第一章 设计模式入门
    Redis 的 IO 多路复用,学习研究
    高性能MySQL 第十章 复制 Part2
    高性能MySQL 第十一章 可扩展的MySQL
  • 原文地址:https://www.cnblogs.com/hd92/p/14631955.html
Copyright © 2011-2022 走看看