请教两个SQL语句不同Cost的原因
本帖最后由 sky88088 于 2013-11-21 16:04:34 编辑 有两个效果一样的SQL语句,产生的执行计划的cost不同,我想问问为什么会有这样的差别,原理是什么?
有两个表
table_A:
name1
name2
table_B:
name,有索引table_B_idx
第一段语句
select * from table_A a
where exists (select null from table_B where a.name1=b.name or a.name2=b.name);
执行计划为
Description Object name Cost
select statement 900822
filter
table accesses full table_A 1993
inlist iterator
index range scan table_B_idx 1
第二段语句
select * from table_A a
where a.name1 in (select name from table_B) or a.name2 in (select name from table_B);
执行计划为
Description Object name Cost
select statement 1994
filter
table accesses full table_A 1994
index range scan table_B_idx 1
index range scan table_B_idx 1
事实上第二种方法确实比第一种快1/3,但是我对这两个执行计划似懂非懂,希望大牛们能指点一下这两个过程究竟区别在哪里,为什么有这么大的差别?
PS:第一次发帖,代码也没法复制,都是手敲的,哪里写得不够详细请谅解 执行计划 cost
[解决办法]
一般情况下:
有两个表:TABLE_A,TABLE_B
SELECT * FROM TABLE_A A WHERE A.ID IN (SELECT B.ID FROM TABLE_B)
SELECT * FROM TABLE_A A where exists (SELECT 1 FORM TABLE_B B WHERE B.ID = A.ID)
这里应该很明白了吧?相当于上面的查询要进行很多次语句执行,COST当然会要高出很多。
[解决办法]