Chinaunix首页 | 论坛 | 博客

-

  • 博客访问: 4133016
  • 博文数量: 172
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 1923
  • 用 户 组: 普通用户
  • 注册时间: 2018-12-20 14:57
文章分类
文章存档

2021年(19)

2020年(81)

2019年(68)

2018年(4)

我的朋友

分类: 敏捷开发

2020-06-17 18:28:09


一、     查询要求


Q10语句是查询每个国家在某时刻起的三个月内货运存在问题的客户和造成的损失。

Q10语句的特点是:带有分组、排序、聚集操作并存的多表连接查询操作。查询语句没有从语法上限制返回多少条元组,但是TPC-H标准规定,查询结果只返回前10行(通常依赖于应用程序实现)。

 


二、     Oracle执行


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

select * from (

         select  /*+ parallel(n) */

                   c_custkey,c_name,

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

                   c_acctbal,n_name,c_address,c_phone,c_comment

         from

                   customer,orders,lineitem,nation

         where

                   c_custkey = o_custkey

                   and l_orderkey = o_orderkey

                   and o_orderdate >= date '1993-05-01'

                   and o_orderdate < date '1993-05-01' + interval '3' month

                   and l_returnflag = 'R'

                   and c_nationkey = n_nationkey

         group by

                   c_custkey,

                   c_name,

                   c_acctbal,

                   c_phone,

                   n_name,

                   c_address,

                   c_comment

         order by

                   revenue desc

) where rownum <=10;

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

脚本执行时间,单位:秒

并行数 1 2 4 8 12
Oracle 591 399 313 237 215

 


三、     SPL优化


这里的orderslineitem主子表关联优化原理与Q3中类似。

 

SPL脚本如下:


A
1 =1
2 =now()
3 >date=date("1993-05-01")
4 =elapse@m(date,3)
5 =file(path+"orders.ctx").create().cursor@m(O_ORDERKEY,O_CUSTKEY,O_ORDERDATE;O_ORDERDATE>=date   && O_ORDERDATE
6 =file(path+"lineitem.ctx").create().news(A5,L_ORDERKEY,L_EXTENDEDPRICE,L_DISCOUNT,L_RETURNFLAG,O_CUSTKEY,O_ORDERDATE;L_RETURNFLAG=="R")
7 =A6.groups@u(O_CUSTKEY:c_custkey;sum(L_EXTENDEDPRICE*(1-L_DISCOUNT)):revenue)
8 =A7.sort(revenue:-1).to(10).derive@o().keys@i(c_custkey)
9 =file(path+"nation.ctx").create().cursor(N_NATIONKEY,N_NAME).fetch().keys@i(N_NATIONKEY)
10 =file(path+"customer.ctx").create()
11 =A8.joinx@q(c_custkey,A10:C_CUSTKEY,C_NAME,C_ADDRESS,C_NATIONKEY,C_PHONE,C_ACCTBAL,C_COMMENT).fetch()
12 =A11.switch(C_NATIONKEY,A9:N_NATIONKEY)
13 =A12.new(c_custkey:C_CUSTKEY,C_NAME,revenue,C_ACCTBAL,C_NATIONKEY.N_NAME:N_NAME,C_ADDRESS,C_PHONE,C_COMMENT)
14 =A13.sort(revenue:-1)
15 =now()
16 =interval@s(A2,A15)

先把orderslineitem表的连接结果集上的分组汇总运算做完,然后再基于这个结果集继续做外部相关的外键表关联运算。因为最终结果只要取10条记录,可以只针对这10条记录再做剩下的关联计算,所以不必在分组前做,否则计算量会增大。

A8中也可以使用top函数取出前10名,比sort全排序会更快,不过此时数据量已经不大,差别不太明显了。

A8算出来之后,因为customer表对C_CUSTKEY有序,用A8.cursor().joinx@qcustomer表中有序匹配快速地把相关记录取出来,再去做其它join,无须遍历customer表,减少数据读取量。

 

脚本执行时间,单位:秒

并行数 1 2 4 8 12
Oracle 591 399 313 237 215
SPL组表 108 61 36 23 21

 

 


阅读(1575) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~