小编Dej*_*ell的帖子

从大表的索引字段中选择前 10 个需要太长时间

我有一个包含 165M 记录的表,如下所示:

Performance
   id        integer
   installs  integer
   hour      timestamp without time zone
Run Code Online (Sandbox Code Playgroud)

我也有一个小时索引:

CREATE INDEX hour_idx
  ON performance
  USING btree
  (hour DESC NULLS LAST);
Run Code Online (Sandbox Code Playgroud)

但是,选择按小时排序的前 10 条记录需要 6 分钟!

EXPLAIN ANALYZE  select hour from performance order by hour desc limit 10
Run Code Online (Sandbox Code Playgroud)

退货

Limit  (cost=7952135.23..7952135.25 rows=10 width=8) (actual time=376313.958..376313.964 rows=10 loops=1)
  ->  Sort  (cost=7952135.23..8368461.00 rows=166530310 width=8) (actual time=376313.957..376313.960 rows=10 loops=1)
        Sort Key: hour
        Sort Method: top-N heapsort  Memory: 25kB
        ->  Seq Scan on performance  (cost=0.00..4353475.10 rows=166530310 width=8) (actual time=0.006..327149.828 rows=192330557 …
Run Code Online (Sandbox Code Playgroud)

postgresql performance index explain

8
推荐指数
1
解决办法
817
查看次数

标签 统计

explain ×1

index ×1

performance ×1

postgresql ×1