The difference between in and exists in sql is a problem of efficiency

in and exists

in is to connect the outer table with the inner table as a hash, and exists is to loop the outer table, and the inner table is queried every time the loop is looped.

The long-standing belief that exists is more efficient than in is inaccurate. If the two tables queried 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 a large table, use exists for the larger sub-query table, and in for the smaller sub-query table:

for example:

table A (small table), table B (large table) 1:
select * from A where cc in (select cc from B)
is inefficient and uses the index of cc column in table A;



select * from A where exists (select cc from B where cc=A.cc)



is efficient and uses table B The index of the cc column.



Opposite 2:



select * from B where cc in (select cc from A)



is efficient and uses the index of cc column on B table;



select * from B where exists (select cc from A where cc=B.cc)



is inefficient , using the index of the cc column on the A table.




not in and not exists

If the query statement uses not in, then both the inner and outer tables will perform a full table scan, and no index is used;

The subquery of not extsts can still use the index on the table. So no matter the size of the table, using not exists is faster than not in.

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=327041815&siteId=291194637