在postgres中解释索引和过滤器部分的布尔列

时间:2015-11-09 13:15:52

标签: postgresql boolean sql-execution-plan b-tree-index

我有一个包含布尔列的表 - "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.378469.446..469.446
  • 执行时间2792.804 ms1827.239 ms

1 个答案:

答案 0 :(得分:1)

<强>已更新

我看不出这个EXPLAIN有什么问题,除了您正在索引boolean列(显然,列的基数较低的字段)。 可能有利于使用Partial Index定义类似于:

CREATE INDEX ON yourtable WHERE is_woman = FALSE;

至于问题本身,您有一个WHERE ...条件的查询。 Postgres planner/optimizer决定使用woman_idx索引扫描而不是顺序扫描 - Index Cond指示用于索引扫描。

如果您看到Filter语句,则表示计划节点检查其扫描的每一行的条件(在我们的示例中为每个woman_idx扫描),并仅输出那些通过这个条件的人。有关详细信息,请查看EXPLAIN文档。

相关问题