PostgreSQL 特有模型索引¶
以下是 PostgreSQL 特有的 索引 可以从 django.contrib.postgres.indexes 模块中获得。
BloomIndex¶
-
class
BloomIndex(*expressions, length=None, columns=(), **options)¶ 创建一个 bloom 索引。
要使用这个索引访问,你需要激活 PostgreSQL 上的 bloom 扩展。你可以使用
BloomExtension迁移操作来安装它。为
length参数提供一个从 1 到 4096 的整数位,用于指定每个索引条目的长度。PostgreSQL 的默认值是 80。columns参数取一个元组或最多 32 个值的列表,这些值是 1 到 4095 的整数位。
BrinIndex¶
BTreeIndex¶
-
class
BTreeIndex(*expressions, fillfactor=None, **options)¶ 创建一个 B 树索引。
为 fillfactor 参数提供一个从 10 到 100 的整数值,以调整索引页的打包程度。PostgreSQL 的默认值是 90。
GinIndex¶
-
class
GinIndex(*expressions, fastupdate=None, gin_pending_list_limit=None, **options)¶ 创建一个 gin 索引 。
要想在不在 内置运算符类 的数据类型上使用这个索引,需要在 PostgreSQL 上激活 btree_gin 扩展 。你可以使用
BtreeGinExtension迁移操作来安装它。将
fastupdate参数设置为False,以禁用 PostgreSQL 中默认启用的 GIN 快速更新技术 。Provide an integer number of kilobytes to the gin_pending_list_limit parameter to tune the maximum size of the GIN pending list which is used when
fastupdateis enabled.
GistIndex¶
-
class
GistIndex(*expressions, buffering=None, fillfactor=None, **options)¶ 创建一个 GiST 索引 。这些索引在空间字段上会自动创建
spatial_index=True。它们在其他类型上也很有用,比如HStoreField或者 range fields。要在不在内置的 gist 操作类 的数据类型上使用这个索引,需要在 PostgreSQL 上激活 btree_gist 扩展 。你可以使用
BtreeGistExtension迁移操作来安装它。将
buffering参数设置为True或False,手动启用或禁用索引的 buffering build 。为 fillfactor 参数提供一个从 10 到 100 的整数值,以调整索引页的打包程度。PostgreSQL 的默认值是 90。
HashIndex¶
-
class
HashIndex(*expressions, fillfactor=None, **options)¶ 创建一个哈希索引。
为 fillfactor 参数提供一个从 10 到 100 的整数值,以调整索引页的打包程度。PostgreSQL 的默认值是 90。
SpGistIndex¶
-
class
SpGistIndex(*expressions, fillfactor=None, **options)¶ 创建 SP-GIST 索引 。
为 fillfactor 参数提供一个从 10 到 100 的整数值,以调整索引页的打包程度。PostgreSQL 的默认值是 90。
Changed in Django 4.1:Support for covering SP-GiST indexes on PostgreSQL 14+ was added.
OpClass() 表达式¶
-
class
OpClass(expression, name)¶ An
OpClass()expression represents theexpressionwith a custom operator class that can be used to define functional indexes, functional unique constraints, or exclusion constraints. To use it, you need to add'django.contrib.postgres'in yourINSTALLED_APPS. Set thenameparameter to the name of the operator class.例子:
Index( OpClass(Lower('username'), name='varchar_pattern_ops'), name='lower_username_idx', )
creates an index on
Lower('username')usingvarchar_pattern_ops.UniqueConstraint( OpClass(Upper('description'), name='text_pattern_ops'), name='upper_description_unique', )
creates a unique constraint on
Upper('description')usingtext_pattern_ops.ExclusionConstraint( name='exclude_overlapping_ops', expressions=[ (OpClass('circle', name='circle_ops'), RangeOperators.OVERLAPS), ], )
使用
circle_ops在circle上创建一个排除约束。Changed in Django 4.0:增加了对函数唯一约束的支持。
Changed in Django 4.1:Support for exclusion constraints was added.