请教怎样优化商品库存流转进出存表处理

0
我把所有库存流转相关的单据都union all 放到一起,然后再用group by 把库存、日期、SKU汇总,然后横向就放各样的指标,如sum(case when 单据=入库单 and 类型=采购 then 数量 else 0) as 采购入库数量,
sum(case when 单据=入库单 and 类型=配货 then 数量 else 0) as 配货入库数量等等
  还有我想把吊牌额字段也带过来,我是每个表先关联SKU好,还是最后出来中间结果再关联商品表把吊牌价拿过来再乘加工出来的数量。
  后续可能当天只处理最近30天左右的数据,把最近30天删除,再插入最近30天,防止系统漏单或取数中间表出问题漏单错单。这个我需要写个where的时间条件,是放在union all的里面每个union all 的各个select 来单独where还是外围写一个where,效率有差别吗?
如 
select * from 表1 where 时间>最近30天
union all 
select * from 表2 where 时间>最近30天
union all 
select * from 表3 where 时间>最近30天
 
和 
select *
from
(
select * from 表1 
union all
select * from 表1 
union all
select * from 表1 
) A where A.时间>最近30天
 
请问这样的表应该怎么处理比较合理?
1.png


2.png

 
已邀请:
1

郑大鹏 2016-03-10 回答

放在union all的里面每个union all 的各个select 来单独where还是外围写一个where,效率有差别吗?
差别大了 放在里边的可能原表会有索引,检索会快一些,放在外边,会把所有数据都放在一起 生成一个临时表,然后在检索,没有索引估计会慢很多

要回复问题请先登录注册