从 TPCH 测试学习性能优化技巧之 Q5

一、     查询要求

 

Q5语句查询得到通过某个地区零件供货商而获得的收入(收入按sum(l_extendedprice * (1 -l_discount))计算)统计信息。可用于决定在给定的区域是否需要建立一个当地分配中心。

Q5语句的特点是:带有分组、排序、聚集操作并存的多表连接查询操作。

 

 

二、     Oracle执行

 

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

select /*+ parallel(n) */

         n_name,

         sum(l_extendedprice * (1 - l_discount)) as revenue

from

         customer,

         orders,

         lineitem,

         supplier,

         nation,

         region

where

         c_custkey = o_custkey

         and l_orderkey = o_orderkey

         and l_suppkey = s_suppkey

         and c_nationkey = s_nationkey

         and s_nationkey = n_nationkey

         and n_regionkey = r_regionkey

         and r_name = 'ASIA'

         and o_orderdate >= date '1995-01-01'

         and o_orderdate < date '1995-01-01' + interval '1' year

group by

         n_name

order by

         revenue desc;

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

脚本执行时间,单位:秒

并行数124812
Oracle672368301224225

 

三、     SPL优化

 

优化原理和Q3类似,只是涉及的外键表更多一些。

 

SPL脚本如下:

 A
1=1
2=now()
3>date=date("1995-01-01")
4>name="ASIA"
5=elapse@y(date,1)
6=file(path+"region.ctx").create().cursor().select(R_NAME==name).fetch()
7=file(path+"nation.ctx").create().cursor().switch@i(N_REGIONKEY,   A6:R_REGIONKEY).fetch().keys@i(N_NATIONKEY)
8=file(path+"supplier.ctx").create().cursor@m(S_SUPPKEY,S_NATIONKEY;S_NATIONKEY:A7;A1).fetch().keys@i(S_SUPPKEY)
9=file(path+"customer.ctx").create().cursor@m(C_CUSTKEY,C_NATIONKEY;C_NATIONKEY:A7;A1).fetch().keys@i(C_CUSTKEY)
10=file(path+"orders.ctx").create().cursor@m(O_ORDERKEY,O_CUSTKEY;O_ORDERDATE>=date   && O_ORDERDATE < A5,O_CUSTKEY:A9;A1)
11=file(path+"lineitem.ctx").create().news(A10,L_ORDERKEY,L_SUPPKEY,L_EXTENDEDPRICE,L_DISCOUNT,O_CUSTKEY;L_SUPPKEY:A8)
12=A11.select(O_CUSTKEY.C_NATIONKEY==L_SUPPKEY.S_NATIONKEY)
13=A12.groups@u(L_SUPPKEY.S_NATIONKEY.N_NAME;sum(L_EXTENDEDPRICE   * (1 - L_DISCOUNT)):revenue)
14=A13.sort(revenue   :-1)
15=now()
16=interval@s(A2,A15)

这里大量使用了前面题目中说过的游标建立时过滤和将关联字段转换成外键表指针的技巧。

与Q3不大相同的是,最后用于分组的字段不是已经有序的L_ORDERKEY,所以不能再使用groups@o。

 

脚本执行时间,单位:秒

并行数124812
Oracle672368301224225
SPL组表353177914934
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值