mysql exists 和join哪个效率高

mysql exists 和join哪个效率高,第1张

如果查询的两个表大小相当,那么用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'

的结果是相同的。

如说两张表一张是用户表TDefUser(userid,address,phone),一张是消费表TAccConsume(userid,time,amount),我要查消费超过5000的用户记录,那么可以写

select * from TDefUser where exists (select 1 from TAccConsume where TDefUser.userid=TAccConsume.userid and TAccConsume.amount>5000)

也可以:

select * from TDefUser where userid in (select userid from TAccConsume where TAccConsume.amount>5000)

exists()后面的子查询被称做相关子查询其运行方式是先运行主查询一次 再去子查询里查询与其对应的结果 如果是ture则输出,反之则不输出.再根据主查询中的每一行去子查询里去查询. in()后面的子查询 是返回结果集的,换句话说执行次序和exists()不一样.子查询先产生结果集,然后主查询再去结果集里去找符合要求的字段列表去.符合要求的输出,反之则不输出.

性能影响的话,这个是没有固定那一个好,要针对自身条件来使用。

一直以来认为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'

的结果是相同的。

我们简单建了一张表,如下:

我们建立了两个索引,分别为主键索引id和普通索引product_id

如果我们的where条件中存在or,及时其中带索引,也不会命中索引。这也是为什么尽量使用or的原因

我们通过主键索引查询,可以看见命中了索引:

而我们在where 条件后面加上or之后,就不会命中索引了:

可以看到如果用like查询的话,%在右边会命中索引的,而%在左边则不会。当然这也不是绝对的,当我们使用索引列进行查询的时候,就都会命中索引了:

NOT IN,NOT LIKE,NOT EXTSTS都不会命中索引


欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/zaji/7610088.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-04-07
下一篇 2023-04-07

发表评论

登录后才能评论

评论列表(0条)

保存