in and exists

http://www.jianshu.com/p/39dcef831ba2

1. in and exists
in are hash connections between the outer and inner tables, while exists is a loop for the outer, and the inner table is queried every time the loop is looped. If the two tables of the query are of the same size, there is little difference between using in and exists; if one of the two tables is smaller and the other is larger, use exists for the larger sub-query table, and in for the smaller sub-query table;

although usually not It is recommended to use * in sql statements, but * can be used with confidence in the exists subquery. exists only cares about whether the row exists, not the value of each column.

For example: table A (small table), table B (large table)

select * from A where cc in(select cc from B)
is inefficient, and the index of column cc on table A is used;

select * from A where exists(select cc from B where cc=A.cc)
is efficient and uses the index of the cc column on the B table.

On the contrary:

select * from B where cc in(select cc from A)
is efficient, using the index of cc column on B table

select * from B where exists(select cc from A where cc=B.cc) 
is inefficient, using to the index of the cc column on the A table.

2. not in and not exists
not in is not logically equivalent to not exists.
The difference between not exists and not in manifests itself when the input list contains null values. The input list contains null, not in always returns false and unknown. That is, null in ('a', 'b', null) returns null, and 'c' not in ('a', 'b', null) returns null, so filter out null before using not in value. And for not exists, always return true and false.

Text / GALAXY_ZMY (author of short book)
original link: http://www.jianshu.com/p/39dcef831ba2
The copyright belongs to the author, please contact the author for authorization and mark "author of short book".

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326514121&siteId=291194637
Recommended