如何加速这个Django PostgreSQL查询?

时间:2014-08-22 13:24:56

标签: sql django postgresql

我正在尝试加速Django(150ms)中看起来相当慢的PostgreSQL查询

这是查询:

SELECT ••• FROM "predictions_prediction"
INNER JOIN "minute_in_time_minute"
ON ( "predictions_prediction"."minute_id" = "minute_in_time_minute"."id" )
WHERE ("minute_in_time_minute"."datetime" >= '2014-08-21 13:12:00+00:00'
AND "predictions_prediction"."location_id" = 1
AND "minute_in_time_minute"."datetime" < '2014-08-24 13:12:00+00:00'
AND "predictions_prediction"."tide_level" >= 3.0)
ORDER BY "minute_in_time_minute"."datetime" ASC

这是PostgreSQL EXPLAIN的结果:

Sort  (cost=17731.45..17739.78 rows=3331 width=32) (actual time=151.755..151.901 rows=3515 loops=1)
  Sort Key: minute_in_time_minute.datetime
  Sort Method: quicksort  Memory: 371kB
  ->  Hash Join  (cost=3187.44..17536.56 rows=3331 width=32) (actual time=96.757..150.693 rows=3515 loops=1)
        Hash Cond: (predictions_prediction.minute_id = minute_in_time_minute.id)
        ->  Seq Scan on predictions_prediction  (cost=0.00..11232.00 rows=411175 width=20) (actual time=0.017..88.063 rows=410125 loops=1)
              Filter: ((tide_level >= 3::double precision) AND (location_id = 1))
              Rows Removed by Filter: 115475
        ->  Hash  (cost=3134.21..3134.21 rows=4258 width=12) (actual time=9.221..9.221 rows=4320 loops=1)
              Buckets: 1024  Batches: 1  Memory Usage: 203kB
              ->  Bitmap Heap Scan on minute_in_time_minute  (cost=92.07..3134.21 rows=4258 width=12) (actual time=1.147..8.220 rows=4320 loops=1)
                    Recheck Cond: ((datetime >= '2014-08-21 13:18:00+00'::timestamp with time zone) AND (datetime < '2014-08-24 13:18:00+00'::timestamp with time zone))
                    ->  Bitmap Index Scan on minute_in_time_minute_datetime_key  (cost=0.00..91.00 rows=4258 width=0) (actual time=0.851..0.851 rows=4320 loops=1)
                          Index Cond: ((datetime >= '2014-08-21 13:18:00+00'::timestamp with time zone) AND (datetime < '2014-08-24 13:18:00+00'::timestamp with time zone))

我尝试在外部工具(http://explain.depesz.com/s/CWW)中对其进行可视化,这表明问题的开头是Seq Scan on predictions_prediction

到目前为止我尝试过:

  • predictions_prediction.tide_level
  • 上添加索引
  • tide_level
  • 上的locationpredictions.prediction上添加综合索引

但就我所见,两者都没有任何影响。

有人可以帮我解释一下查询计划吗?

由于

1 个答案:

答案 0 :(得分:0)

尝试在predictions_prediction上创建以下复合索引

(minute_id, location_id, tide_level)

此类查询的150ms(一个连接,其中几个带有不等式和顺序的条件)实际上相对正常,因此您可能无法加快它的速度。