簡體   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