Postgres 9.4.12慢查询

时间:2017-07-21 23:08:42

标签: postgresql performance postgresql-9.4

我在一张包含900万行的表格中查询了我的帖子。

查询:EXPLAIN (ANALYZE, BUFFERS) SELECT date_point, geo, alarms, status FROM tracking_master WHERE id_asset = 151 AND date_point >= '2017-07-21 19:20:05' AND date_point <= '2017-07-21 19:25:05' ORDER BY date_point asc LIMIT 1000

这里查询EXPLAIN(ANALYZE,BUFFERS):

Limit  (cost=161175.31..161175.32 rows=1 width=579) (actual time=60734.109..60734.113 rows=5 loops=1)
  Buffers: shared hit=23470 read=114350
  ->  Sort  (cost=161175.31..161175.32 rows=1 width=579) (actual time=60734.107..60734.110 rows=5 loops=1)
        Sort Key: date_point
        Sort Method: quicksort  Memory: 27kB
        Buffers: shared hit=23470 read=114350
        ->  Index Scan using idx_tracking_master_id_asset on tracking_master  (cost=0.43..161175.30 rows=1 width=579) (actual time=80.682..60734.081 rows=5 loops=1)
              Index Cond: (id_asset = 151)
              Filter: ((date_point >= '2017-07-21 19:20:05-03'::timestamp with time zone) AND (date_point <= '2017-07-21 19:25:05-03'::timestamp with time zone))
              Rows Removed by Filter: 202512
              Buffers: shared hit=23470 read=114350
Planning time: 0.204 ms
Execution time: 60734.152 ms

1 个答案:

答案 0 :(得分:2)

没有查询,很难提供帮助。看来你的where子句中有这样的东西:

    id_asset=151 
AND (date_point >= '2017-07-21 19:20:05-03') 
AND (date_point <= '2017-07-21 19:25:05-03')

并在id_asset列上有一个索引。

尝试在(id_asset, date_point)上创建多列索引。我发现你也在使用ORDER BY子句。在日期列上指定排序顺序可能会有所帮助。

示例:

CREATE INDEX multicol_index ON tracking_master (id_asset, date_point DESC);

(提示:创建索引后使用VACUUM ANALYZE)