MySQL中 in 和 exists区别

2022-09-28 11:09:21

MySQL中 in 和 exists区别

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

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

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

3、 not in 和 not exists:如果查询语句使用了 not in,那么内外表都进行全表扫描,没有用到索引;而 not extsts 的子查询依然能用到表上的索引。所以无论那个表大,用not exists 都比 not in要快。

  • 作者:技术砖家--Felix
  • 原文链接:https://haitao.blog.csdn.net/article/details/122495936
    更新时间:2022-09-28 11:09:21