剛看了《oracle 高效設計》的講到的,說到oracle中有標量子查詢,效能要由於關聯查詢,但是我在SQL server中知道關聯查詢的效能要優於標量子查詢。
我們來做個測試,看看效能:執行語句:
set autotrace on
select a.username,count(*) from all_users a,all_objects b
where a.username=b.owner(+)
group by a.username;
在SQL developer中執行,結果如下:
5356 recursive calls
0 db block gets
82152 consistent gets
再執下列語句:
set autotrace on
select a.username,(select count(*) from all_objects b where b.owner=a.username) cnt
from all_users a;
執行結果,如下:
5371 recursive calls
0 db block gets
98645 consistent gets
這時發現,通過執行關聯查詢的效能要優於標量子查詢。估計《Oracle 高效設計》是版本比較低,現在都是用的10gR2的版本有很大改變。
執行語句如下:
set autotrace on
select a.username,count(*),avg(object_id) from all_users a,all_objects b
where a.username=b.owner(+) group by a.username;
執行結果:
5371 recursive calls
0 db block gets
82157 consistent gets
執行語句:
set autotrace on
select username,to_number(substr(data,1,10)) cnt,to_number(substr(data,11)) avg from
(
select a.username,(select to_char(count(*),'fm0000000009') || avg(object_id) from all_objects b where b.owner=a.username) data from all_users a)
執行結果:
5356 recursive calls
0 db block gets
98556 consistent gets
執行語句:
set autotrace on
create or replace type myType as object
(cnt number,avg number);
select username,a.data.cnt,a.data.avg from
(select username,(select myType(count(*),avg(object_id)) from all_objects b where b.owner=a.username) data from all_users a) a;
執行結果:
5390 recursive calls
0 db block gets
98662 consistent gets
總結:
通過上面的測試發現,TOM的《Oracle高效設計》裡談到的標量子查詢的效能要由於關聯查詢,在舊版本的中可能是可以的,在10gR2中是不成立的。