一、 查询要求

Q22语句查询获得消费者可能购买的地理分布。本查询计算在指定的国家,比平均水平更持肯定态度但还没下七年订单的消费者数量。能反应出普通消费者的态度,即购买意向。

Q22语句的特点是:带有分组、排序、聚集、子查询、NOT EXISTS子查询操作并存的两表连接操作。

二、 Oracle执行

Oracle编写的查询SQL语句如下:

select  /+ parallel(n) /

         cntrycode,

         count(*) as numcust,

         sum(c_acctbal) as totacctbal

from

         (

                   select

                            substr(c_phone,1,2) as cntrycode,

                            c_acctbal

                   from

                            customer

                   where

                            substr(c_phone,1,2) in

                                     ('11', '14', '15', '19', '20', '21', '23')

                            and c_acctbal > (

                                     select

                                               avg(c_acctbal)

                                     from

                                               customer

                                     where

                                               c_acctbal > 0.00

                                               and substr(c_phone,1,2) in

                                                        ('11', '14', '15', '19', '20', '21', '23')

                            )

                            and not exists (

                                     select

                                               *

                                     from

                                               orders

                                     where

                                               o_custkey = c_custkey

                            )

         ) custsale

group by

         cntrycode

order by

         cntrycode;

其中/*+ parallel(n) */ 是Oracle的并行查询语法,n是并行数。

脚本执行时间,单位:秒

1png

三、 SPL优化

这个查询的外层是个常规的分组聚合,其主要复杂度在于内层的两个用于条件的子查询。这两个子查询能对应一些满足条件的customer记录,而后一个子查询是用了not exist修饰,最后结果应当是这两个子查询对应的customer记录集合的差集。

SPL脚本如下:

2png

SPL中分组计算结果是有序的,即A8会对C_CUSTKEY有序,而A6本身来自customer表,也对C_CUSTKEY有序,两个有序集可以使用归并算法高速计算差集(A9中)。

脚本执行时间,单位:秒

3png


raqsoft
114 声望10 粉丝