繁体   English   中英

如何提高sql查询的性能

[英]How to Improve Performance of sql query

桌子

CREATE TABLE ABC(key number(5), orders number(5), cost number(5), dat date);
insert into ABC (key, orders, cost, dat) values (1, 3, 5, to_date('10-01- 
2017', 'mm-dd-yyyy'));
insert into ABC (key, orders, cost,dat) values (1, 5, 2, to_date('02-10- 
2017', 'mm-dd-yyyy')); 
insert into ABC (key, orders, cost,dat) values (1, 6, 1, to_date('03-10- 
2017', 'mm-dd-yyyy'));
insert into ABC (key, orders, cost,dat) values (1, 7, 2, to_date('05-10- 
2017', 'mm-dd-yyyy')); 
insert into ABC (key, orders, cost,dat) values (1, 8, 3, to_date('07-10- 
2017', 'mm-dd-yyyy')); 
insert into ABC (key, orders, cost,dat) values (1, 3, 4, to_date('08-10- 
2017', 'mm-dd-yyyy')); 
insert into ABC (key, orders, cost,dat) values (2, 3, 6, to_date('02-10- 
2017', 'mm-dd-yyyy')); 
insert into ABC (key, orders, cost,dat) values (2, 3, 9, to_date('01-10- 
2017', 'mm-dd-yyyy')); 
insert into ABC (key, orders, cost,dat) values (2, 2 ,5, to_date('03-10- 
2017', 'mm-dd-yyyy')); 
insert into ABC (key, orders, cost,dat) values (2, 3, 2, to_date('05-10- 
2017', 'mm-dd-yyyy')); 
insert into ABC (key, orders, cost,dat) values (2, 1, 1, to_date('06-10- 
2017', 'mm-dd-yyyy')); 
insert into ABC (key, orders, cost,dat) values (3, 4, 12, to_date('10-10- 
2017', 'mm-dd-yyyy')); 
insert into ABC (key, orders, cost,dat) values (3, 3, 9, to_date('01-10- 
2017', 'mm-dd-yyyy')); 
insert into ABC (key, orders, cost,dat) values (3, 2 ,5, to_date('05-10- 
2017', 'mm-dd-yyyy')); 
insert into ABC (key, orders, cost,dat) values (3, 3, 2, to_date('06-10- 
2017', 'mm-dd-yyyy')); 
insert into ABC (key, orders, cost,dat) values (3, 1, 1, to_date('07-10- 
2017', 'mm-dd-yyyy')); 
insert into ABC (key, orders, cost,dat) values (3, 4, 12, to_date('11-10- 
2017', 'mm-dd-yyyy')); 
insert into ABC (key, orders, cost, dat) values (1, 3, 5, to_date('10-01- 
2017', 'mm-dd-yyyy'));
insert into ABC (key, orders, cost,dat) values (1, 5, 2, to_date('02-17- 
2017', 'mm-dd-yyyy')); 
insert into ABC (key, orders, cost,dat) values (1, 6, 1, to_date('03-18- 
2017', 'mm-dd-yyyy'));
insert into ABC (key, orders, cost,dat) values (1, 7, 2, to_date('05-14- 
2017', 'mm-dd-yyyy')); 
 insert into ABC (key, orders, cost,dat) values (1, 8, 3, to_date('07-13- 
 2017', 'mm-dd-yyyy')); 
 insert into ABC (key, orders, cost,dat) values (1, 3, 4, to_date('08-12- 
 2017', 'mm-dd-yyyy')); 
insert into ABC (key, orders, cost,dat) values (2, 3, 6, to_date('02-11- 
2017', 'mm-dd-yyyy')); 
insert into ABC (key, orders, cost,dat) values (2, 3, 9, to_date('01-15- 
2017', 'mm-dd-yyyy')); 
insert into ABC (key, orders, cost,dat) values (2, 2 ,5, to_date('03-14- 
2017', 'mm-dd-yyyy')); 
insert into ABC (key, orders, cost,dat) values (2, 3, 2, to_date('05-18- 
 2017', 'mm-dd-yyyy')); 
insert into ABC (key, orders, cost,dat) values (2, 1, 1, to_date('06-19- 
2017', 'mm-dd-yyyy')); 
 insert into ABC (key, orders, cost,dat) values (3, 4, 12, to_date('10-11- 
 2017', 'mm-dd-yyyy')); 
 insert into ABC (key, orders, cost,dat) values (3, 3, 9, to_date('01-12- 
 2017', 'mm-dd-yyyy')); 
 insert into ABC (key, orders, cost,dat) values (3, 2 ,5, to_date('05-16- 
 2017', 'mm-dd-yyyy')); 
 insert into ABC (key, orders, cost,dat) values (3, 3, 2, to_date('06-17- 
 2017', 'mm-dd-yyyy')); 
 insert into ABC (key, orders, cost,dat) values (3, 1, 1, to_date('07-12- 
 2017', 'mm-dd-yyyy')); 
  insert into ABC (key, orders, cost,dat) values (3, 4, 12, to_date('12-11- 
  2017', 'mm-dd-yyyy')); 
 `

现在,查询

with
one as 
(select t.key, sum(t.cost) as tot, sum(t.orders) as qty from table t
where t.date >= to_date('01-01-2017','mm-dd-yyyy') and 
t.date < to_date('04-01-2017','mm-dd-yyyy')
group by t.key),
two as 
(select t.key, sum(t.cost) as tot, sum(t.orders) as qty from table t
where t.date >= to_date('04-01-2017','mm-dd-yyyy') and 
t.date < to_date('07-01-2017','mm-dd-yyyy')
group by t.key),
three as
(select t.key, sum(t.cost) as tot, sum(t.orders) as qty from table t
where t.date >= to_date('07-01-2017','mm-dd-yyyy') and 
t.date < to_date('10-01-2017','mm-dd-yyyy')
group by t.key),
four as
(select t.key, sum(t.cost) as tot, sum(t.orders) as qty from table t
where t.date >= to_date('10-01-2017','mm-dd-yyyy') and 
t.date < to_date('01-01-2018','mm-dd-yyyy')
group by t.key)
select o.key, o.tot, o.qty, s.tot, s.qty, t.tot, t.qty, f.tot,f.qty 
from one o
left join two s on s.key = o.key
left join three t on t.key = o.key
left join four f on f.key = o.key

我想知道是否可以提高此SQL查询的性能?

如果您注意到该代码,则可以看到存在一个“ Where”语句,该语句从各个日期组获取结果。 除了where子句,大多数查询都是重复的。

有没有一种方法可以优化查询? 在性能和行数方面。

预期的输出格式:我可以编码格式部分,但是,这就是想法 在此处输入图片说明

看起来您正在尝试获取季度总计/数量。 使用sum(case / when)可以简化。

SQL提琴结果

select 
        t.key, 
        sum( case when t.dat >= Tmp.Q1From and t.dat < Tmp.Q1End then t.cost else 0 end ) as Q1Tot, 
        sum( case when t.dat >= Tmp.Q1From and t.dat < Tmp.Q1End then t.orders else 0 end ) as Q1Qty,
        sum( case when t.dat >= Tmp.Q1End and t.dat < Tmp.Q2End then t.cost else 0 end ) as Q2Tot, 
        sum( case when t.dat >= Tmp.Q1End and t.dat < Tmp.Q2End then t.orders else 0 end ) as Q2Qty,
        sum( case when t.dat >= Tmp.Q2End and t.dat < Tmp.Q3End then t.cost else 0 end ) as Q3Tot, 
        sum( case when t.dat >= Tmp.Q2End and t.dat < Tmp.Q3End then t.orders else 0 end ) as Q3Qty,
        sum( case when t.dat >= Tmp.Q3End and t.dat < Tmp.Q4End then t.cost else 0 end ) as Q4Tot, 
        sum( case when t.dat >= Tmp.Q3End and t.dat < Tmp.Q4End then t.orders else 0 end ) as Q4Qty
    from 
        ABC t,
           ( select 
                   to_date('01-01-2017', 'mm-dd-yyyy') Q1From,
                   to_date('04-01-2017', 'mm-dd-yyyy') Q1End,
                   to_date('07-01-2017', 'mm-dd-yyyy') Q2End,
                   to_date('10-01-2017', 'mm-dd-yyyy') Q3End,
                   to_date('01-01-2018', 'mm-dd-yyyy') Q4End
                from 
                   dual ) Tmp
    where 
            t.dat >= to_date('01-01-2017', 'mm-dd-yyyy')
        and t.dat < to_date('01-01-2018', 'mm-dd-yyyy')
    group by 
        t.key

并且肯定在(date,key)上有一个索引…date字段用于优化WHERE子句,KEY用于优化组

一劳永逸会避免遗漏一些数字,例如第一季度未售出但IS在第二,第三或第四季度售出的商品,否则将被排除在最终结果之外。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM