postgresql中查询优化的可能性

时间:2014-12-09 21:56:49

标签: postgresql optimization benchmarking

我必须解决我班上有关postgresql中查询优化的问题。

我必须优化以下查询。

 select ol_number,
   sum(ol_quantity) as sum_qty,
   sum(ol_amount) as sum_amount,
   avg(ol_quantity) as avg_qty,
   avg(ol_amount) as avg_amount,
   count(*) as count_order
  from   orderline
  where  ol_delivery_d > '2007-01-02 00:00:00.000000'
  group by ol_number order by ol_number

对我来说,它似乎已经过优化,但我不确定。

是否可以通过索引或其他方式优化该查询(物化视图也可以)?


执行计划(也在评论中链接)

"Sort  (cost=63652.89..63652.92 rows=11 width=13) (actual time=4026.270..4026.272 rows=15 loops=1)"
"  Sort Key: ol_number"
"  Sort Method: quicksort  Memory: 26kB"
"  ->  HashAggregate  (cost=63652.54..63652.70 rows=11 width=13) (actual time=4026.218..4026.247 rows=15 loops=1)"
"        ->  Seq Scan on order_line  (cost=0.00..44671.46 rows=1265405 width=13) (actual time=0.008..486.399 rows=1259054 loops=1)"
"              Filter: (ol_delivery_d > '2007-01-02 00:00:00'::timestamp without time zone)"
"              Rows Removed by Filter: 538903"
"Total runtime: 4026.346 ms"

我意识到以下诀窍因为我认为avg与sum / count相同,所以我认为这是相关的意识到这个或我错了吗?

select ol_number, sum_qty, sum_amount, 
sum_qty/count_order as avg_qty,      
sum_amount/count_order as avg_amount, count_order 
from (select   ol_number, sum(ol_quantity) as sum_qty, sum(ol_amount) as sum_amount, count(*) as   count_order 
from order_line 
where ol_delivery_d > '2007-01-02 00:00:00.000000' 
group by ol_number 
order by ol_number) as t

0 个答案:

没有答案