百分百源码网-让建站变得如此简单! 登录 注册 签到领金币!

主页 | 如何升级VIP | TAG标签

当前位置: 主页>网站教程>数据库> mysql用什么取代in
分享文章到:

mysql用什么取代in

发布时间:10/01 来源:未知 浏览: 关键词:

Mysql中用exists代替in;exists对表面用loop逐条查询,每次查询都会查看exists的前提语句,当exists里的前提语句能够返回记载行时,前提就为真,返回当前loop到的这笔记录。

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'

的结果是雷同的。

引荐教程:mysql视频教程

以上就是mysql用什么代替in的具体内容,更多请关注百分百源码网其它相关文章!

打赏

打赏

取消

感谢您的支持,我会继续努力的!

扫码支持
扫码打赏,你说多少就多少

打开支付宝扫一扫,即可进行扫码打赏哦

百分百源码网 建议打赏1~10元,土豪随意,感谢您的阅读!

共有150人阅读,期待你的评论!发表评论
昵称: 网址: 验证码: 点击我更换图片
最新评论

本文标签

广告赞助

能出一分力是一分吧!

订阅获得更多模板

本文标签

广告赞助

订阅获得更多模板