设为首页 收藏本站
查看: 252|回复: 0

[经验分享] Oracle Dimension 下

[复制链接]

尚未签到

发表于 2016-6-23 08:27:43 | 显示全部楼层 |阅读模式
  我们再创建一张customer_hierarchy表,用于存储客户代码、邮政编码和地区的关系,然后我们将按不同邮编或地区来查询各自的月度、季度或者年度销量信息。
  
  Roby@XUE> create table customer_hierarchy
  2 ( cust_id primary key, zip_code, region )
  3 organization index
  4 as
  5 select cust_id,
  6 mod( rownum, 6 ) || to_char(mod( rownum, 1000 ), 'fm0000') zip_code,
  7 mod( rownum, 6 ) region
  8 from ( select distinct cust_id from sales)
  9 /
  
  Table created.
  
  Roby@XUE> analyze table customer_hierarchy compute statistics;
  
  Table analyzed.
  
  改写物化视图,查询方案中添加按不同邮编的月度统计销量。
  
  Roby@XUE> drop materialized view mv_sales;
  
  Materialized view dropped.
  
  Roby@XUE> create materialized view mv_sales
  2 build immediate
  3 refresh on demand
  4 enable query rewrite
  5 as
  6 select customer_hierarchy.zip_code,
  7 time_hierarchy.mmyyyy,
  8 sum(sales.sales_amount) sales_amount
  9 from sales, time_hierarchy, customer_hierarchy
  10 where sales.trans_date = time_hierarchy.day
  11 and sales.cust_id = customer_hierarchy.cust_id
  12 group by customer_hierarchy.zip_code, time_hierarchy.mmyyyy
  13 /
  
  Materialized view created.
  
  Roby@XUE> set autotrace traceonly
  Roby@XUE> select customer_hierarchy.zip_code,
  2 time_hierarchy.mmyyyy,
  3 sum(sales.sales_amount) sales_amount
  4 from sales, time_hierarchy, customer_hierarchy
  5 where sales.trans_date = time_hierarchy.day
  6 and sales.cust_id = customer_hierarchy.cust_id
  7 group by customer_hierarchy.zip_code, time_hierarchy.mmyyyy
  8 /
  
  1216 rows selected.
  
  Execution Plan
  ----------------------------------------------------------
  0 SELECT STATEMENT Optimizer=CHOOSE (Cost=2 Card=409 Bytes=20450)
  1 0 TABLE ACCESS (FULL) OF 'MV_SALES' (Cost=2 Card=409 Bytes=20450)
  
  Statistics
  ----------------------------------------------------------
  28 recursive calls
  0 db block gets
  116 consistent gets
  5 physical reads
  
  可以看到如果按不同邮编、不同月度来统计查询的话,优化器将会查询物化视图中的查询方案,性能也是比较可观的。假如我们查不同地区年度的统计销量信息,结果又会是怎样?
  
  Roby@XUE> select customer_hierarchy.region,
  2 time_hierarchy.yyyy,
  3 sum(sales.sales_amount) sales_amount
  4 from sales, time_hierarchy, customer_hierarchy
  5 where sales.trans_date = time_hierarchy.day
  6 and sales.cust_id = customer_hierarchy.cust_id
  7 group by customer_hierarchy.region, time_hierarchy.yyyy
  8 /
  
  9 rows selected.
  
  Execution Plan
  ----------------------------------------------------------
  0 SELECT STATEMENT Optimizer=CHOOSE (Cost=1681 Card=9 Bytes=261)
  1 0 SORT (GROUP BY) (Cost=1681 Card=9 Bytes=261)
  2 1 NESTED LOOPS (Cost=35 Card=426672 Bytes=12373488)
  3 2 NESTED LOOPS (Cost=35 Card=426672 Bytes=8106768)
  4 3 TABLE ACCESS (FULL) OF 'SALES' (Cost=35 Card=426672
  5 3 INDEX (UNIQUE SCAN) OF 'SYS_IOT_TOP_7833' (UNIQUE)
  6 2 INDEX (UNIQUE SCAN) OF 'SYS_IOT_TOP_7828' (UNIQUE)
  
  Statistics
  ----------------------------------------------------------
  0 recursive calls
  0 db block gets
  428047 consistent gets
  745 physical reads
  
  可以看到查询性能大有影响。接下我们同样创建dimension sales_dimension,用于说明客户代码和邮编、地区间的关系:
  
  Roby@XUE> drop dimension time_hierarchy_dim
  2 /
  
  Dimension dropped.
  
  Roby@XUE> create dimension sales_dimension
  2 level cust_id is customer_hierarchy.cust_id
  3 level zip_code is customer_hierarchy.zip_code
  4 level region is customer_hierarchy.region
  5 level day is time_hierarchy.day
  6 level mmyyyy is time_hierarchy.mmyyyy
  7 level qtr_yyyy is time_hierarchy.qtr_yyyy
  8 level yyyy is time_hierarchy.yyyy
  9 hierarchy cust_rollup
  10 (
  11 cust_id child of
  12 zip_code child of
  13 region
  14 )
  15 hierarchy time_rollup
  16 (
  17 day child of
  18 mmyyyy child of
  19 qtr_yyyy child of
  20 yyyy
  21 )
  22 attribute mmyyyy
  23 determines mon_yyyy;
  
  Dimension created.
  
  再回到原来的查询,我们可以看到查询性能有了大幅的提升:
  
  Roby@XUE> set autotrace on
  Roby@XUE> select customer_hierarchy.region,
  2 time_hierarchy.yyyy,
  3 sum(sales.sales_amount) sales_amount
  4 from sales, time_hierarchy, customer_hierarchy
  5 where sales.trans_date = time_hierarchy.day
  6 and sales.cust_id = customer_hierarchy.cust_id
  7 group by customer_hierarchy.region, time_hierarchy.yyyy
  8 /
  
  REGION YYYY SALES_AMOUNT
  ---------- ---------- ------------
  0 2006 7.3144E+11
  0 2007 4484956329
  1 2006 7.8448E+11
  2 2006 7.7257E+11
  2 2007 4684418980
  3 2006 7.7088E+11
  4 2006 7.8004E+11
  4 2007 3127953246
  5 2006 7.3273E+11
  
  9 rows selected.
  
  Execution Plan
  ----------------------------------------------------------
  0 SELECT STATEMENT Optimizer=CHOOSE (Cost=15 Card=9 Bytes=576)
  1 0 SORT (GROUP BY) (Cost=15 Card=9 Bytes=576)
  2 1 HASH JOIN (Cost=10 Card=598 Bytes=38272)
  3 2 VIEW (Cost=3 Card=100 Bytes=700)
  4 3 SORT (UNIQUE) (Cost=3 Card=100 Bytes=700)
  5 4 INDEX (FULL SCAN) OF 'SYS_IOT_TOP_7833' (UNIQUE)
  6 2 HASH JOIN (Cost=7 Card=598 Bytes=34086)
  7 6 VIEW (Cost=4 Card=19 Bytes=133)
  8 7 SORT (UNIQUE) (Cost=4 Card=19 Bytes=133)
  9 8 INDEX (FAST FULL SCAN) OF 'SYS_IOT_TOP_7828'
  10 6 TABLE ACCESS (FULL) OF 'MV_SALES' (Cost=2 Card=409
  
  Statistics
  ----------------------------------------------------------
  364 recursive calls
  0 db block gets
  88 consistent gets
  0 physical reads
  
  Roby@XUE> set autot trace
  Roby@XUE> select customer_hierarchy.region,
  2 time_hierarchy.qtr_yyyy,
  3 sum(sales.sales_amount) sales_amount
  4 from sales, time_hierarchy, customer_hierarchy
  5 where sales.trans_date = time_hierarchy.day
  6 and sales.cust_id = customer_hierarchy.cust_id
  7 group by customer_hierarchy.region, time_hierarchy.qtr_yyyy;
  
  27 rows selected.
  
  Execution Plan
  ----------------------------------------------------------
  0 SELECT STATEMENT Optimizer=CHOOSE (Cost=23 Card=22 Bytes=154
  1 0 SORT (GROUP BY) (Cost=23 Card=22 Bytes=1540)
  2 1 HASH JOIN (Cost=11 Card=1447 Bytes=101290)
  3 2 VIEW (Cost=3 Card=100 Bytes=700)
  4 3 SORT (UNIQUE) (Cost=3 Card=100 Bytes=700)
  5 4 INDEX (FULL SCAN) OF 'SYS_IOT_TOP_7833' (UNIQUE) (
  6 2 HASH JOIN (Cost=7 Card=1447 Bytes=91161)
  7 6 VIEW (Cost=4 Card=46 Bytes=598)
  8 7 SORT (UNIQUE) (Cost=4 Card=46 Bytes=598)
  9 8 INDEX (FAST FULL SCAN) OF 'SYS_IOT_TOP_7828' (UN
  10 6 TABLE ACCESS (FULL) OF 'MV_SALES' (Cost=2 Card=409 B
  
  Statistics
  ----------------------------------------------------------
  10 recursive calls
  0 db block gets
  19 consistent gets
  0 physical reads
  
  Roby@XUE> select customer_hierarchy.region,
  2 time_hierarchy.mon_yyyy,
  3 sum(sales.sales_amount) sales_amount
  4 from sales, time_hierarchy, customer_hierarchy
  5 where sales.trans_date = time_hierarchy.day
  6 and sales.cust_id = customer_hierarchy.cust_id
  7 group by customer_hierarchy.region, time_hierarchy.mon_yyyy;
  
  75 rows selected.
  Execution Plan
  ----------------------------------------------------------
  0 SELECT STATEMENT Optimizer=CHOOSE (Cost=41 Card=56 Bytes=386
  1 0 SORT (GROUP BY) (Cost=41 Card=56 Bytes=3864)
  2 1 HASH JOIN (Cost=11 Card=3775 Bytes=260475)
  3 2 VIEW (Cost=4 Card=120 Bytes=1440)
  4 3 SORT (UNIQUE) (Cost=4 Card=120 Bytes=1440)
  5 4 INDEX (FAST FULL SCAN) OF 'SYS_IOT_TOP_7828' (UNIQ
  6 2 HASH JOIN (Cost=6 Card=409 Bytes=23313)
  7 6 VIEW (Cost=3 Card=100 Bytes=700)
  8 7 SORT (UNIQUE) (Cost=3 Card=100 Bytes=700)
  9 8 INDEX (FULL SCAN) OF 'SYS_IOT_TOP_7833' (UNIQUE)
  10 6 TABLE ACCESS (FULL) OF 'MV_SALES' (Cost=2 Card=409 B
  
  Statistics
  ----------------------------------------------------------
  0 recursive calls
  0 db block gets
  14 consistent gets
  0 physical reads
  
  参考:Tomates Kyte 《Expert One-on-One Oracle》

运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.iyunv.com/thread-233889-1-1.html 上篇帖子: oracle 显示游标 下篇帖子: oracle权限全集
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表