【发布时间】:2015-11-09 13:15:51
【问题描述】:
我有一个带有布尔列的表 - "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 is false 的男性,第二个为is_woman is true 的女性。
Explain 同一个查询:
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 ms→1827.239 ms
【问题讨论】:
标签: postgresql boolean sql-execution-plan b-tree-index