Oracle数据库11g:SQL计划管理(三)(4)

日期: 2008-06-17 作者:Jim Czuprynski翻译:黄永兵 来源:TechTarget中国

SPM_3_1


—–
— Query: SPM_3_1.sql
—–
VARIABLE cust_last_name VARchar2(30);
VARIABLE beg_prod_id NUMBER;
VARIABLE end_prod_id NUMBER;
BEGIN
:cust_last_name := ’Sand%’;
:beg_prod_id := 100;
:end_prod_id := 500;
END;
/
PRINT :cust_last_name :beg_prod_id :end_prod_id
select /*SPM_3.1*/
S.cust_id
,C.cust_last_name
,S.prod_id
,P.prod_name
,SUM(S.amount_sold)
,SUM(S.quantity_sold)
FROM
sh.sales S
,sh.customers C
,sh.products P
where S.cust_id = C.cust_id
AND S.prod_id = P.prod_id
AND S.prod_id BETWEEN :beg_prod_id AND :end_prod_id
AND C.cust_last_name like :cust_last_name
GROUP BY
S.cust_id
,C.cust_last_name
,S.prod_id
,P.prod_name
ORDER BY
S.cust_id
,C.cust_last_name
,S.prod_id
,P.prod_name
;


SPM_3_2:


—–
— Query: SPM_3_2.1
— Optimizer will use  “star transformation” method to retrieve a
— smaller subset more efficiently
—–
select /*SPM_3_2.1*/
S.cust_id
,C.cust_last_name
,S.prod_id
,P.prod_name
,S.amount_sold
,S.quantity_sold
FROM
sh.sales S
,sh.customers C
,sh.products P
where S.cust_id = C.cust_id
AND S.prod_id = P.prod_id
AND S.prod_id BETWEEN 100 AND 500
AND C.cust_last_name like ’Sand%’
ORDER BY
S.cust_id
,C.cust_last_name
,S.prod_id
,P.prod_name
;


—–
— Query: SPM_3_2.2
— Optimizer will choose a full table scan of SH.CUSTOMERS because of the larger
— subset of data that’s retrieved
—–
select /*SPM_3_2.2*/
S.cust_id
,C.cust_last_name
,S.prod_id
,P.prod_name
,S.amount_sold
,S.quantity_sold
FROM
sh.sales S
,sh.customers C
,sh.products P
where S.cust_id = C.cust_id
AND S.prod_id = P.prod_id
AND S.prod_id BETWEEN 0 AND 99999
AND C.cust_last_name like ’Zil%’
ORDER BY
S.cust_id
,C.cust_last_name
,S.prod_id
,P.prod_name
;


—–
— Query: SPM_3_2.3
— Optimizer will still choose a full table scan of SH.CUSTOMERS but
— return an even larger result set
—–
select /*SPM_3_2.3*/
S.cust_id
,C.cust_last_name
,S.prod_id
,P.prod_name
,S.amount_sold
,S.quantity_sold
FROM
sh.sales S
,sh.customers C
,sh.products P
where S.cust_id = C.cust_id
AND S.prod_id = P.prod_id
AND S.prod_id BETWEEN 0 AND 99999
AND C.cust_last_name like ’S%’
ORDER BY
S.cust_id
,C.cust_last_name
,S.prod_id
,P.prod_name
;

我们一直都在努力坚持原创.......请不要一声不吭,就悄悄拿走。

我原创,你原创,我们的内容世界才会更加精彩!

【所有原创内容版权均属TechTarget,欢迎大家转发分享。但未经授权,严禁任何媒体(平面媒体、网络媒体、自媒体等)以及微信公众号复制、转载、摘编或以其他方式进行使用。】

微信公众号

TechTarget微信公众号二维码

TechTarget

官方微博

TechTarget中国官方微博二维码

TechTarget中国

电子邮件地址不会被公开。 必填项已用*标注

敬请读者发表评论,本站保留删除与本文无关和不雅评论的权力。

相关推荐