The difference between oracle in and exist

Comparison of oracle in and exists

"Exists" and "in" are in Oracle, both query whether the value of a set exists in another set, but have different usages for different data, mainly in terms of efficiency.

 

select * from table_1 t where t.id in(select id from table_2)

 

The above query uses the in statement, in() is executed only once, it finds all the id fields in the table_2 table and caches them. After that, check whether the id of the table_1 table is equal to the id in the table_2 table, if they are equal, then the table_1 The records of the table are added to the result set until all records of table_1 are traversed.

So it is not suitable to use in() when the table_2 table data is large, because it will traverse all the table_2 table data once.

For example: table_1 table has 10,000 records, table_2 table has 1,000,000 records, then it is possible to traverse 10,000*1,000,000 times at most, which is very inefficient.

Conclusion: in() is suitable for the case where the table_2 table is smaller than the table_1 table data

 

select * from table_1 t1 where exists(select 1 from table_2 t2 where t1.id=t2.id)

 

The above query uses the exists statement, exists() will execute table_1.length times, it does not cache the exists() result set, because the content of the exists() result set is not important, what matters is whether there are records in the result set, if Returns true if there is, false if not.

It is suitable to use exists() when the table_2 table is larger than the table_1 table data, because it does not have the traversal operation, and only needs to execute the query again.

For example: table_1 table has 10000 records, table_2 table has 1000000 records, then exists() will be executed 10000 times to determine whether the id in table_1 table is equal to the id in table_2 table.

For example: table_1 table has 10000 records, table_2 table has 100000000 records, then exists() is still executed 10000 times, because it only executes table_1.length times, it can be seen that the more data in table_2 table, the more suitable exists() is to play its effect.

Conclusion: exists() is suitable for the case where the table_2 table is larger than the table_1 table data

When the data in table_1 is as large as the data in table_2, the efficiency of in and exists is similar, and you can use either one.

 

Guess you like

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