我应该使用哪种INDEX类型?

时间:2017-01-25 11:25:05

标签: sql performance postgresql indexing average

我有一个庞大的数据集(客户,时间戳,消费)。 我需要使用索引来提高各种查询的性能,但我似乎无法创建任何可以提高此特定查询性能的索引:

SELECT customer, AVG(consumption) 
FROM alicante_1y 
GROUP BY customer;

从我一直在阅读的内容来看,没有简单/直接的方法来改善AVG功能......

任何帮助表示赞赏。提前谢谢。

解释(分析,详细)输出:

HashAggregate  (cost=194302.67..194315.09 rows=993 width=16) (actual time=6847.581..6848.630 rows=994 loops=1)  
Output: customer, avg(consumption)
Group Key: alicante_1y.customer
->  Seq Scan on public.alicante_1y  (cost=0.00..150840.45 rows=8692445 width=16) (actual time=0.175..1829.867 rows=8692445 loops=1)
Output: customer, t, consumption
Planning time: 0.633 ms
Execution time: 6849.036 ms

1 个答案:

答案 0 :(得分:0)

对索引最好的是覆盖索引。那将是:

create index idx_customer_consumption on alicante_1y(customer, consumption);

Postgres也应该能够使用索引进行聚合。