oracle sql 高级编程学习笔记(二十二)

一、model 空值

model 子句存在空值的原因有两个
1、单员格存在,但值为空
2、单元格不存在

1、实例演示 nav

select product,
       country,
       year,
       week,
       sale      
  from sales_fact
 where country = 'Australia'
   and product = 'Xtend Memory'
  model keep nav return updated rows
--keep   nav是默认值 nav(non avaliable values) 没有可用值
  partition by(product, country)
  dimension by(year, week) 
  measures(sale) 
  rules sequential order(sale [ 2001, 1 ]
 order by year, week = sale [ 2001, 1 ], sale [ 2002, 1 ]
 order by year, week = sale [ 2001, 1 ] + sale [ 2002, 1 ])
 order by product, country, year, week

这里写图片描述

2、实例演示 ignore nav

select product,
       country,
       year,
       week,
       sale      
  from sales_fact
 where country = 'Australia'
   and product = 'Xtend Memory'
  model ignore nav return updated rows 
-- 如果位空 则返回0
  partition by(product, country)
  dimension by(year, week) 
  measures(sale) 
  rules sequential order(sale [ 2001, 1 ]
 order by year, week = sale [ 2001, 1 ], sale [ 2002, 1 ]
 order by year, week = sale [ 2001, 1 ] + sale [ 2002, 1 ])
 order by product, country, year, week

这里写图片描述

可以回忆上一篇 https://blog.csdn.net/whandgdh/article/details/82697327 中presetv 以及presentnnv 两个函数对空值的处理

二、model子句性能调优

model 子句最关键的就是规则求解。
规则求解可以使用如下5种算法:
1、ACYCLIC
2、ACYCLIC FAST
3、CYCLIC
4、ORDERED
5、ORDERED FAST
ACYCLIC FAST ORDERED FAST是相对较优的算法,允许单元格高效求解。
如果算法指定了automatic order 会选择使用ACYCLIC和CYCLIC 算法
如果指定了 sequential order 则会选择使用ORDERED 算法
如果规则访问某个独立的单元格不进行聚合有可能选择 ACYCLIC FAST 或ORDERED FAST

2、执行计划

2、1 ACYCLIC
实例演示 :model 子句开始求库存的实例 我们在规则种再加上order

select product, country, year, week, inventory, sale, receipts
  from sales_fact
 where country = 'Australia'
   and product = 'Xtend Memory' 
   model return updated rows 
   partition by(product, country)
   dimension by(year, week)
   measures(0 inventory, sale, receipts) 
   rules automatic
   order(inventory [ year, week ] order by year ,week
-- order 来控制规则之间的依赖关系,避免循环依赖性
= nvl(inventory [ cv(year), cv(week) - 1 ], 0) - sale [ cv(year), cv(week) ] + receipts [ cv(year), cv(week) ])
 order by product, country, year, week;

这里写图片描述
ACYCLIC 表明了规则之间没有可能的CYCLIC(循环)依赖关系
2.2、ACYCLIC FAST
如果规则只访问一个单元格,可以使用ACYCLIC FAST 算法

select product, country, year, week,   sale ,sale_modify
  from sales_fact
 where country = 'Australia'
   and product = 'Xtend Memory' 
   model return updated rows 
   partition by(product, country)
   dimension by(year, week)
   measures( 0 sale_modify, sale ) 
   rules automatic
   order(sale_modify[2001,1]=sale[2001,1]*10)
 order by product, country, year, week;

这里写图片描述
2.3 、CYCLIC

select product, country, year, week, inventory, sale, receipts
  from sales_fact
 where country = 'Australia'
   and product = 'Xtend Memory' 
   model return updated rows 
   partition by(product, country)
   dimension by(year, week)
   measures(0 inventory, sale, receipts) 
   rules automatic
   order(inventory [ year, week ] = nvl(inventory [ cv(year), cv(week) - 1 ], 0) - sale [ cv(year), cv(week) ] + receipts [ cv(year), cv(week) ])
 order by product, country, year, week;

这里写图片描述

2.4、ORDERED

 select product, country, year, week, inventory, sale, receipts,sale_modify
  from sales_fact
 where country = 'Australia'
   and product = 'Xtend Memory' 
   model return updated rows 
   partition by(product, country)
   dimension by(year, week)
   measures(0 inventory,0 sale_modify,sale, receipts) 
   rules sequential
   order(inventory [ year, week ] order by year ,week
-- order 来控制规则之间的依赖关系,避免循环依赖性
= nvl(inventory [ cv(year), cv(week) - 1 ], 0) - sale [ cv(year), cv(week) ] + receipts [ cv(year), cv(week) ],
sale_modify[2001,1]=sale[2001,1]*10
)
 order by product, country, year, week;

这里写图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

菜菜的中年程序猿

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值