模型索引参考Link to this heading
索引类便于创建数据库索引。它们可以使用 Meta.indexes 选项来添加。本文档解释了 Index 的 API 引用,其中包括 index options 。
Index 选项Link to this heading
- class Index(*expressions, fields=(), name=None, db_tablespace=None, opclasses=(), condition=None, include=None)Link to this definition
在数据库中创建一个索引(B 树)。
expressionsLink to this heading
- Index.expressionsLink to this definition
Positional argument *expressions allows creating functional indexes on
expressions and database functions.
例子:
Index(Lower('title').desc(), 'pub_date', name='lower_title_date_idx')
creates an index on the lowercased value of the title field in descending
order and the pub_date field in the default ascending order.
Another example:
Index(F('height') * F('weight'), Round('weight'), name='calc_idx')
creates an index on the result of multiplying fields height and weight
and the weight rounded to the nearest integer.
Index.name is required when using *expressions.
fieldsLink to this heading
- Index.fieldsLink to this definition
需要索引字段的名称列表或元组。
默认情况下,索引是以每列的升序创建的。要为列定义一个降序索引,请在字段名前添加一个连字符。
例如 Index(fields=['headline', '-pub_date']) 将创建 SQL 为 (headline, pub_date DESC)。MySQL 上不支持索引排序。在这种情况下,降序索引会像普通索引一样被创建。
nameLink to this heading
- Index.nameLink to this definition
索引的名称。如果没有提供 name,Django 会自动生成一个名称。为了兼容不同的数据库,索引名不能超过 30 个字符,并且不应该以数字(0-9)或下划线(_)开头。
db_tablespaceLink to this heading
- Index.db_tablespaceLink to this definition
该索引要使用的 数据库表空间 名称。对于单字段索引,如果没有提供 db_tablespace,则在字段的 db_tablespace 中创建索引。
如果没有指定 Field.db_tablespace (或者如果索引使用了多个字段),则在模型的 class Meta 里面的 db_tablespace 选项中指定的表空间创建索引。如果这两个表空间都没有设置,则在与表相同的表空间中创建索引。
opclassesLink to this heading
- Index.opclassesLink to this definition
要为这个索引使用的 PostgreSQL 运算符类 名称。如果你需要一个自定义的操作类,你必须为索引中的每个字段提供一个操作类。
例如,GinIndex(name='json_index', fields=['jsonfield'], opclasses=['jsonb_path_ops']) 使用 jsonb_path_ops 在 jsonfield 上创建一个 gin 索引。
opclasses 对于 PostgreSQL 以外的数据库来说是被忽略的。
Index.name 在使用 opclasses 时需要。
conditionLink to this heading
- Index.conditionLink to this definition
如果表非常大,而且你的查询主要针对行的子集,那么将索引限制在该子集上可能会很有用。将条件指定为 Q。例如,condition=Q(pages__gt=400) 对超过 400 页的记录进行索引。
Index.name 在使用 condition 时需要。
includeLink to this heading
- Index.includeLink to this definition
A list or tuple of the names of the fields to be included in the covering index
as non-key columns. This allows index-only scans to be used for queries that
select only included fields (include) and filter only by indexed
fields (fields).
例子:
Index(name='covering_index', fields=['headline'], include=['pub_date'])
will allow filtering on headline, also selecting pub_date, while
fetching data only from the index.
Using include will produce a smaller index than using a multiple column
index but with the drawback that non-key columns can not be used for sorting or
filtering.
include is ignored for databases besides PostgreSQL.
Index.name is required when using include.
See the PostgreSQL documentation for more details about covering indexes.