sql中的in与not in,exists与not exists的区别

1、in和exists(用到索引)

in是把外表和内表作hash连接,而exists是对外表作loop循环,每次loop循环再对内表进行查询,一直以来认为exists比in效率高的说法是不准确的。如果查询的两个表大小相当,那么用in和exists差别不大;如果两个表中一个较小一个较大,则子查询表大的用exists,

子查询表小的用in;

2、not in 和not exists

如果查询语句使用了not in,那么对内外表都进行全表扫描,没有用到索引;而not exists的子查询依然能用到表上的索引。所以无论哪个表大,用not exists都比not in 要快。(尽量不用not in)

posted on 2017-02-09 10:52  心有猛虎细嗅蔷薇  阅读(239)  评论(0编辑  收藏  举报