【Oracle】【22】in、exists、not in、not exists

Preface:

1,in 和 exists

2,not in 和 not exists

3, in and =

text:

1,in 和 exists

It is in the table for the inner and outer hash (dictionary sets) connection

exists is look for cycles each inner table and then query

Query efficiency:

It has been considered to be inaccurate than exists in high efficiency argument, if the query two tables of comparable size, and exists in it with little difference;

If the two tables in a small a large, large look-up table of the child with exists, a small table with a subquery in

- Table A (small table), Table B (large table) 

- low efficiency, and uses a table index on the ID column A 
SELECT  *  from A WHERE a.id in ( SELECT b.id from B); 

- efficiency high index uses the ID column of table B 
SELECT  *  from a WHERE  EXISTS ( SELECT b.id from B WHERE b.id = a.id) 

- high efficiency, and uses a table index on the ID column B 
SELECT  *  from B WHERE b.id in ( SELECT a.id from A) 

-Inefficient, uses the index table id column A 
SELECT  *  from B WHERE  EXISTS ( SELECT a.id from A WHERE a.id = b.id)

2,not in 和 not exists

not in, if any sub-query returns a record containing null values, the query will not return any records. If there is non-empty sub-query field restrictions, you can use.

Query efficiency:

not in appearance inside will have a full table scan, did not use the index; and the child still can not exists query to the index on the table.

So no matter which table is large, not exists than not in fast.

3, in and =

- The following two are equivalent sql 
SELECT name from Employee WHERE name in ( ' John Doe ' , ' John Doe ' , ' Wang Wu ' ); 

SELECT name from Employee WHERE name = " John Doe "  or name = ' LI four '  or name = ' Wang Wu ' ;

Reference blog:

SQL query in, exists, not in, not exists with the use of database technology _Linux difference _ commune -Linux system portal
https://www.linuxidc.com/Linux/2016-04/130285.htm

Guess you like

Origin www.cnblogs.com/huashengweilong/p/11056183.html