我有一个包含 boolean 列的表 - "is_woman"bool DEFAULT true
我有一个包含此列的 btree 索引(以及一些其他列,如年龄、城镇等)- is_woman ASC NULLS LAST
我对此列有一个查询 - is_woman IS FALSE
结果我得到了解释:
-> Index Scan using woman_idx on superjob (cost=... rows=... width=32) (actual time=... rows=... loops=1)
Index Cond: (town = 1) AND (is_woman = false) AND (age >= 35) AND (age <= 60))
Filter: (is_woman IS FALSE)
为什么有两个 is_woman 条件?一个在索引部分,第二个 - 在过滤器中?
已更新
在@dmitry 的帮助下,我创建了两个 partial indexes :第一个用于 is_woman 为 false
的男性,第二个用于 is_woman 为 true
的女性。
解释
相同的查询:
Bitmap Index Scan on is_woman_woman_idx (...) (actual time=469.446..469.446 rows=406867 loops=1)
Index Cond: ((age >= 1) AND (town = 1))
Execution time: 1827.239 ms
没有 Filter
部分,此查询运行速度更快:
- 实际时间
2.227..2754.378
→469.446..469.446
- 执行时间
2792.804 毫秒
→1827.239 毫秒
最佳答案
已更新
我看不出这个 EXPLAIN
有什么问题,除了你正在索引 boolean
列(显然,该列具有低基数字段)。使用 Partial Index 可能会有所帮助定义类似于:
CREATE INDEX ON yourtable WHERE is_woman = FALSE;
至于问题本身,您有一个带有 WHERE ...
条件的查询。 Postgres planner/optimizer
决定使用 woman_idx
索引扫描而不是顺序扫描 - Index Cond
指示用于索引扫描。
如果您可以看到 Filter
语句,则表示 plan 节点 检查它扫描的每一行的条件(在我们的例子中是每个 woman_idx
扫描),只输出满足条件的。详情请查看EXPLAIN
文档。
关于postgresql - postgres中解释的索引和过滤器部分中的 boolean 列,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33609952/