相关疑难解决方法(0)

IS NULL 上的 Postgres 部分索引不起作用

Postgres 版本

使用 PostgreSQL 10.3。

表定义

CREATE TABLE tickets (
  id bigserial primary key,
  state character varying,
  closed timestamp
);

CREATE INDEX  "state_index" ON "tickets" ("state")
  WHERE ((state)::text = 'open'::text));
Run Code Online (Sandbox Code Playgroud)

基数

该表包含 1027616 行,其中 51533 行具有state = 'open'closed IS NULL或 5%。

条件为 on 的查询state按预期使用索引扫描执行良好:

explain analyze select * from tickets where state = 'open';

Index Scan using state_index on tickets  (cost=0.29..16093.57 rows=36599 width=212) (actual time=0.025..22.875 rows=37346 loops=1)
Planning time: 0.212 ms
Execution time: 25.697 …
Run Code Online (Sandbox Code Playgroud)

postgresql performance index postgresql-10 postgresql-performance

3
推荐指数
1
解决办法
3231
查看次数