The difference between ORACLE in and exists statement (1)


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

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

List resultSet=[];
Array A=(select * from A);
Array B=(select id from B);

for(int i=0;i<A.length;i++) {
   for(int j=0;j<B.length;j++) {
      if(A[i].id==B[j].id) {
         resultSet.add(A[i]);
         break;
      }
   }
}
return resultSet;

It can be seen that it is not suitable to use in() when the data in table B is large, because it will traverse all the data in table B once.
For example, if table A has 10,000 records, and table B has 1,000,000 records, then it is possible to traverse up to 10,000 records. *1000000 times, the efficiency is very poor.
Another example: A table has 10000 records, B table has 100 records, then it is possible to traverse 10000*100 times at most, the number of traversals is greatly reduced, and the efficiency is greatly improved.

Conclusion: in() is suitable for the case that the data in table B is smaller than that in table A

select a.* from A a
where exists(select 1 from B b where a.id=b.id)

The above query uses the exists statement, exists() will execute A.length times, it does not cache the exists() result set, because the content of the exists() result set is not important, the important thing is whether there are records in the result set, if so Returns true, otherwise returns false.
Its query process is similar to the following process

List resultSet=[];
Array A=(select * from A)

for(int i=0;i<A.length;i++) {
   if(exists(A[i].id) {    //执行select 1 from B b where b.id=a.id是否有记录返回
       resultSet.add(A[i]);
   }
}
return resultSet;

当B表比A表数据大时适合使用exists(),因为它没有那么遍历操作,只需要再执行一次查询就行.
如:A表有10000条记录,B表有1000000条记录,那么exists()会执行10000次去判断A表中的id是否与B表中的id相等.
如:A表有10000条记录,B表有100000000条记录,那么exists()还是执行10000次,因为它只执行A.length次,可见B表数据越多,越适合exists()发挥效果.
再如:A表有10000条记录,B表有100条记录,那么exists()还是执行10000次,还不如使用in()遍历10000*100次,因为in()是在内存里遍历比较,而exists()需要查询数据库,我们都知道查询数据库所消耗的性能更高,而内存比较很快.

结论:exists()适合B表比A表数据大的情况

当A表数据与B表数据一样大时,in与exists效率差不多,可任选一个使用.

--转载https://www.cnblogs.com/iceword/archive/2011/02/15/1955337.html

Guess you like

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