PostgreSQL 特有模型索引¶
以下是 PostgreSQL 特有的 索引 可以从 django.contrib.postgres.indexes
模块中获得。
BloomIndex
¶
- class BloomIndex(*expressions, length=None, columns=(), **options)[source]¶
创建一个 bloom 索引。
要使用这个索引访问,你需要激活 PostgreSQL 上的 bloom 扩展。你可以使用
BloomExtension
迁移操作来安装它。为
length
参数提供一个从 1 到 4096 的整数位,用于指定每个索引条目的长度。PostgreSQL 的默认值是 80。columns
参数取一个元组或最多 32 个值的列表,这些值是 1 到 4095 的整数位。
BrinIndex
¶
BTreeIndex
¶
- class BTreeIndex(*expressions, fillfactor=None, deduplicate_items=None, **options)[source]¶
创建一个 B 树索引。
为 fillfactor 参数提供一个从 10 到 100 的整数值,以调整索引页的打包程度。PostgreSQL 的默认值是 90。
向 deduplicate_items 参数提供一个布尔值,以控制是否启用去重。PostgreSQL 默认启用去重。
Changed in Django 5.1:增加了
deduplicate_items
参数。
GinIndex
¶
- class GinIndex(*expressions, fastupdate=None, gin_pending_list_limit=None, **options)[source]¶
创建一个 gin 索引 。
To use this index on data types not in the built-in operator classes, you need to activate the btree_gin extension on PostgreSQL. You can install it using the
BtreeGinExtension
migration operation.将
fastupdate
参数设置为False
,以禁用 PostgreSQL 中默认启用的 GIN 快速更新技术 。将一个整数值以千字节为单位提供给 gin_pending_list_limit 参数,以调整当启用
fastupdate
时用于限制 GIN 待处理列表的最大大小。
GistIndex
¶
- class GistIndex(*expressions, buffering=None, fillfactor=None, **options)[source]¶
创建一个 GiST 索引 。这些索引在空间字段上会自动创建
spatial_index=True
。它们在其他类型上也很有用,比如HStoreField
或者 range fields。To use this index on data types not in the built-in gist operator classes, you need to activate the btree_gist extension on PostgreSQL. You can install it using the
BtreeGistExtension
migration operation.将
buffering
参数设置为True
或False
,手动启用或禁用索引的 buffering build 。为 fillfactor 参数提供一个从 10 到 100 的整数值,以调整索引页的打包程度。PostgreSQL 的默认值是 90。
HashIndex
¶
- class HashIndex(*expressions, fillfactor=None, **options)[source]¶
创建一个哈希索引。
为 fillfactor 参数提供一个从 10 到 100 的整数值,以调整索引页的打包程度。PostgreSQL 的默认值是 90。
SpGistIndex
¶
- class SpGistIndex(*expressions, fillfactor=None, **options)[source]¶
创建 SP-GIST 索引 。
为 fillfactor 参数提供一个从 10 到 100 的整数值,以调整索引页的打包程度。PostgreSQL 的默认值是 90。
OpClass()
表达式¶
- class OpClass(expression, name)[source]¶
一个
OpClass()
表达式表示带有自定义 operator class 的expression
,它可用于定义功能索引、功能唯一约束或排除约束。要使用它,您需要在INSTALLED_APPS
中添加'django.contrib.postgres'
。将name
参数设置为 operator class 的名称。例子:
Index( OpClass(Lower("username"), name="varchar_pattern_ops"), name="lower_username_idx", )
使用
varchar_pattern_ops
在Lower('username')
上创建一个索引。:UniqueConstraint( OpClass(Upper("description"), name="text_pattern_ops"), name="upper_description_unique", )
使用
text_pattern_ops
在Upper('description')
上创建一个唯一约束。:ExclusionConstraint( name="exclude_overlapping_ops", expressions=[ (OpClass("circle", name="circle_ops"), RangeOperators.OVERLAPS), ], )
使用
circle_ops
在circle
上创建一个排除约束。