Postgres查询调优

时间:2013-10-01 10:14:05

标签: sql performance postgresql group-by

我有一张包含历史记录的表格。每当计数更新时,都会添加一条记录,指定此时获取新值。表模式如下所示:

    Column     |           Type           |                             Modifiers
---------------+--------------------------+--------------------------------------------------------------------
 id            | integer                  | not null default nextval('project_accountrecord_id_seq'::regclass)
 user_id       | integer                  | not null
 created       | timestamp with time zone | not null
 service       | character varying(200)   | not null
 metric        | character varying(200)   | not null
 value         | integer                  | not null

现在,我想获取过去七天每天更新的记录总数。这就是我想出的:

SELECT
    created::timestamp::date as created_date,
    count(created)
FROM
    project_accountrecord
GROUP BY
    created::timestamp::date
ORDER BY
    created_date DESC
LIMIT 7;

运行缓慢(11406.347ms)。 EXPLAIN ANALYZE给出:

Limit  (cost=440939.66..440939.70 rows=7 width=8) (actual time=24184.547..24370.715 rows=7 loops=1)
   ->  GroupAggregate  (cost=440939.66..477990.56 rows=6711746 width=8) (actual time=24184.544..24370.699 rows=7 loops=1)
         ->  Sort  (cost=440939.66..444340.97 rows=6802607 width=8) (actual time=24161.120..24276.205 rows=92413 loops=1)
               Sort Key: (((created)::timestamp without time zone)::date)
               Sort Method: external merge  Disk: 146328kB
               ->  Seq Scan on project_accountrecord  (cost=0.00..153671.43 rows=6802607 width=8) (actual time=0.017..10132.970 rows=6802607 loops=1)
 Total runtime: 24420.988 ms

此表中有超过680万行。我该怎么做才能提高此查询的性能?理想情况下,我希望它能在一秒钟内运行,因此我可以将其缓存并在后台每天更新一次。

1 个答案:

答案 0 :(得分:2)

现在,您的查询必须扫描整个表格,计算结果并限制为最近7天。 您可以通过仅扫描过去7天来加速查询(如果您不每天更新记录,则可以更快):

where created_date>now()::date-'7 days'::interval

另一种方法是将历史结果缓存在额外的表中,并仅计算当前日期。