且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

在布尔字段上添加索引

更新时间:2022-05-28 21:50:51

不,您可以如果要对其进行过滤,则为布尔字段编制索引。这是完全合理的做法,尽管对于所有索引,PostgreSQL可能会选择忽略它,如果它不会排除足够多的表-索引扫描加上大量行读取操作可能比顺序扫描更昂贵- -可能会或不会影响您,具体取决于该列中的值。

No, you can index a boolean field if you'll be filtering by it. That's a perfectly reasonable thing to do, although as with all indexes, PostgreSQL may choose to ignore it if it won't exclude enough of the table -- an index scan plus a ton of row fetches may be more expensive than a sequential scan -- which may or may not affect you depending on the values in that column.

您还应该知道PostgreSQL允许您在索引上设置条件,我经常发现这很有用与布尔字段。 (有关详细信息,请参见部分索引。)如果通常要在其中进行过滤或排序在这种范围内,可能***通过 CREATE INDEX ... ON table(some_field)WHERE boolean_field 这样的服务。

You should also be aware that PostgreSQL lets you put conditions on indexes, which I often find useful with boolean fields. (See Partial Indexes for details.) If you'll be commonly filtering or sorting within that scope, you might be best served by something like CREATE INDEX ... ON table (some_field) WHERE boolean_field.