oracle中的 exists 和 in 的效率问题

1) select * from T1 where exists(select * from T2 where T1.a=T2.a) ;

    T1数据量小而T2数据量非常大时,T1<<T2 时,1) 的查询效率高。

2) select * from T1 where T1.a in (select T2.a from T2) ;

     T1数据量非常大而T2数据量小时,T1>>T2 时,2) 的查询效率高。

in 是把外表和内表作hash 连接,而exists是对外表作loop循环,每次loop循环再对内表进行查询.

如果两个表中一个较小,一个是大表,则子查询表大的用exists,子查询表小的用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'的结果是相同的。

not in和not exists

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

   尽量不要使用not in子句。使用minus 子句都比not in 子句快,虽然使用minus子句要进行两次查询:

select staff_name from staff_member where staff_id in (select staff_id from staff_member minus select staff_id from staff_func where func_id like '81%');

原文地址:https://www.cnblogs.com/moonandstar08/p/5052644.html