IN 和 EXISTS效率是不一样的,虽然它们得到的结果是一样的。
IN适合外联结的数据表数据相对于主表比较少的情况下,EXISTS在同等大数据量的情况下是优于IN的,适用于主表小于外连表。但对于海量数据查询两者最好都不要用。直接两表关联

解决方案 »

  1.   

    UP,大师都说在巨表查询的时候Exists效率高于In效率。
      

  2.   

    应该是在9i以上oracle会自动的转换
      

  3.   

    --Connected to Oracle9i Enterprise Edition Release 9.2.0.5.0
    --test_tbl1 记录数500000
    --test_tbl2 记录数200
    --test_tbl1 的ID字段没有使用到索引
    --test_tbl2 的ID字段有索引
    select count(*) from test_tbl1 where id in (
    select id from test_tbl2--执行时间2.473s select count(*) from test_tbl1 a where exists(select * from test_tbl2 b 
    where b.id =a.id )--执行时间1.522s
    如果数据量更大的话时间差别就越大,可见oracle9i的in和exists还是有差别的。
      

  4.   

    --Connected to Oracle9i Enterprise Edition Release 9.2.0.5.0
    --test_tbl3 记录数1400000
    --test_tbl2 记录数200
    --test_tbl3 的ID字段没有使用到索引
    --test_tbl2 的ID字段有索引
    select count(*) from test_tbl3 where id in (
    select id from test_tbl2--执行时间4.887s select count(*) from test_tbl3 a where exists(select * from test_tbl2 b 
    where b.id =a.id )--执行时间2.323s