Dus*_*wal 5 postgresql index index-tuning postgresql-performance
我有一个数据库,其中有一列名为timestamp
type timestamp without timezone
,该列上有一个 btree 索引。
我有一个查询如下
SELECT
*
FROM
employee
WHERE
timestamp >= timestamp '2020-01-27 13:24:09'
Run Code Online (Sandbox Code Playgroud)
timestamp
但是,解释分析显示未使用该列上的索引:
Seq Scan on employee (cost=0.00..5498.73 rows=34377 width=381) (actual time=0.016..37.944 rows=34251 loops=1)
Filter: ("timestamp" >= '2020-01-27 13:24:09'::timestamp without time zone)
Rows Removed by Filter: 21167
Planning Time: 0.255 ms
Execution Time: 40.277 ms
Run Code Online (Sandbox Code Playgroud)
如果我使用包含时间戳的过滤条件更改查询,而不是1 month
(1 year
如上所述),则使用列上的索引。
Bitmap Heap Scan on employee (cost=59.51..4510.25 rows=2996 width=381) (actual time=2.164..5.204 rows=2958 loops=1)
Recheck Cond: ("timestamp" >= '2020-12-27 13:24:09'::timestamp without time zone)
Heap Blocks: exact=208
-> Bitmap Index Scan on timestamp_salary_idx (cost=0.00..58.76 rows=2996 width=0) (actual time=2.113..2.114 rows=2958 loops=1)
Index Cond: ("timestamp" >= '2020-12-27 13:24:09'::timestamp without time zone)
Planning Time: 0.195 ms
Execution Time: 5.485 ms
Run Code Online (Sandbox Code Playgroud)
为什么索引不用于范围超过 1 年的查询?