“Django/docs/3.0.x/ref/models/indexes”的版本间差异

来自菜鸟教程
Django/docs/3.0.x/ref/models/indexes
跳转至:导航、​搜索
(autoload)
 
(Page commit)
 
第1行: 第1行:
 +
{{DISPLAYTITLE:模型索引参考 — Django 文档}}
 
<div id="module-django.db.models.indexes" class="section">
 
<div id="module-django.db.models.indexes" class="section">
  
 
<span id="model-index-reference"></span>
 
<span id="model-index-reference"></span>
= Model index reference =
+
= 型号索引参考 =
  
Index classes ease creating database indexes. They can be added using the
+
索引类简化了创建数据库索引的过程。 可以使用 [[../options#django.db.models.Options|Meta.indexes]] 选项添加它们。 本文档解释了 [[#django.db.models.Index|Index]] 的 API 参考,其中包括 [[#index-options|索引选项]]
[[../options#django.db.models.Options|<code>Meta.indexes</code>]] option. This document
 
explains the API references of [[#django.db.models.Index|<code>Index</code>]] which includes the [[#index-options|index
 
options]].
 
  
 
<div class="admonition-referencing-built-in-indexes admonition">
 
<div class="admonition-referencing-built-in-indexes admonition">
  
Referencing built-in indexes
+
引用内置索引
  
Indexes are defined in <code>django.db.models.indexes</code>, but for convenience
+
索引在 <code>django.db.models.indexes</code> 中定义,但为了方便起见,它们被导入 [[../../../topics/db/models#module-django.db|django.db.models]]。 标准约定是使用 <code>from django.db import models</code> 并将索引称为 <code>models.&lt;IndexClass&gt;</code>
they're imported into [[../../../topics/db/models#module-django.db|<code>django.db.models</code>]]. The standard convention is
 
to use <code>from django.db import models</code> and refer to the indexes as
 
<code>models.&lt;IndexClass&gt;</code>.
 
  
  
第22行: 第17行:
 
<div id="index-options" class="section">
 
<div id="index-options" class="section">
  
== <code>Index</code> options ==
+
== Index 选项 ==
  
; ''class'' <code>Index</code><span class="sig-paren">(</span>''<span class="n">fields</span><span class="o">=</span><span class="default_value">()</span>'', ''<span class="n">name</span><span class="o">=</span><span class="default_value">None</span>'', ''<span class="n">db_tablespace</span><span class="o">=</span><span class="default_value">None</span>'', ''<span class="n">opclasses</span><span class="o">=</span><span class="default_value">()</span>'', ''<span class="n">condition</span><span class="o">=</span><span class="default_value">None</span>''<span class="sig-paren">)</span>
+
; ''<span class="pre">class</span>'' <span class="sig-name descname"><span class="pre">Index</span></span><span class="sig-paren">(</span>''<span class="n"><span class="pre">fields</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">()</span></span>'', ''<span class="n"><span class="pre">name</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">None</span></span>'', ''<span class="n"><span class="pre">db_tablespace</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">None</span></span>'', ''<span class="n"><span class="pre">opclasses</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">()</span></span>'', ''<span class="n"><span class="pre">condition</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">None</span></span>''<span class="sig-paren">)</span>
: Creates an index (B-Tree) in the database.
+
: 在数据库中创建索引(B 树)。
  
 
<div id="fields" class="section">
 
<div id="fields" class="section">
  
=== <code>fields</code> ===
+
=== fields ===
  
; <code>Index.</code><code>fields</code>
+
; <span class="sig-prename descclassname"><span class="pre">Index.</span></span><span class="sig-name descname"><span class="pre">fields</span></span>
 
:  
 
:  
  
A list or tuple of the name of the fields on which the index is desired.
+
需要索引的字段名称的列表或元组。
  
By default, indexes are created with an ascending order for each column. To
+
默认情况下,索引是按每列的升序创建的。 要为列定义降序索引,请在字段名称前添加一个连字符。
define an index with a descending order for a column, add a hyphen before the
 
field's name.
 
  
For example <code>Index(fields=['headline', '-pub_date'])</code> would create SQL with
+
例如,<code>Index(fields=['headline', '-pub_date'])</code> 将使用 <code>(headline, pub_date DESC)</code> 创建 SQL。 MySQL 不支持索引排序。 在这种情况下,会创建一个降序索引作为普通索引。
<code>(headline, pub_date DESC)</code>. Index ordering isn't supported on MySQL. In that
 
case, a descending index is created as a normal index.
 
  
  
第48行: 第39行:
 
<div id="name" class="section">
 
<div id="name" class="section">
  
=== <code>name</code> ===
+
=== name ===
  
; <code>Index.</code><code>name</code>
+
; <span class="sig-prename descclassname"><span class="pre">Index.</span></span><span class="sig-name descname"><span class="pre">name</span></span>
 
:  
 
:  
  
The name of the index. If <code>name</code> isn't provided Django will auto-generate a
+
索引的名称。 如果未提供 <code>name</code>,Django 将自动生成一个名称。 为了兼容不同的数据库,索引名称不能超过 30 个字符,并且不应以数字 (0-9) 或下划线 (_) 开头。
name. For compatibility with different databases, index names cannot be longer
 
than 30 characters and shouldn't start with a number (0-9) or underscore (_).
 
  
 
<div class="admonition-partial-indexes-in-abstract-base-classes admonition">
 
<div class="admonition-partial-indexes-in-abstract-base-classes admonition">
  
Partial indexes in abstract base classes
+
抽象基类中的部分索引
  
You must always specify a unique name for an index. As such, you
+
您必须始终为索引指定唯一名称。 因此,您通常不能在抽象基类上指定部分索引,因为 [[../options#django.db.models.Options|Meta.indexes]] 选项由子类继承,属性值完全相同(包括 <code>name</code>)每一次。 为了解决名称冲突,部分名称可能包含 <code>'%(app_label)s'</code> <code>'%(class)s'</code>,分别由具体模型的小写应用程序标签和类名称替换。 例如<code>Index(fields=['title'], name='%(app_label)s_%(class)s_title_index')</code>
cannot normally specify a partial index on an abstract base class, since
 
the [[../options#django.db.models.Options|<code>Meta.indexes</code>]] option is
 
inherited by subclasses, with exactly the same values for the attributes
 
(including <code>name</code>) each time. To work around name collisions, part of the
 
name may contain <code>'%(app_label)s'</code> and <code>'%(class)s'</code>, which are
 
replaced, respectively, by the lowercased app label and class name of the
 
concrete model. For example <code>Index(fields=['title'], name='%(app_label)s_%(class)s_title_index')</code>.
 
  
  
第74行: 第56行:
 
<div class="versionchanged">
 
<div class="versionchanged">
  
Interpolation of <code>'%(app_label)s'</code> and <code>'%(class)s'</code> was added.
+
<span class="versionmodified changed"> 3.0 版更改:添加了 </span> <code>'%(app_label)s'</code> <code>'%(class)s'</code> 的插值。
  
  
第82行: 第64行:
 
<div id="db-tablespace" class="section">
 
<div id="db-tablespace" class="section">
  
=== <code>db_tablespace</code> ===
+
=== db_tablespace ===
  
; <code>Index.</code><code>db_tablespace</code>
+
; <span class="sig-prename descclassname"><span class="pre">Index.</span></span><span class="sig-name descname"><span class="pre">db_tablespace</span></span>
 
:  
 
:  
  
The name of the [[../../../topics/db/tablespaces|<span class="doc">database tablespace</span>]] to use for
+
用于此索引的 [[../../../topics/db/tablespaces|数据库表空间]] 的名称。 对于单字段索引,如果未提供 <code>db_tablespace</code>,则在字段的 <code>db_tablespace</code> 中创建索引。
this index. For single field indexes, if <code>db_tablespace</code> isn't provided, the
 
index is created in the <code>db_tablespace</code> of the field.
 
  
If [[../fields#django.db.models.Field|<code>Field.db_tablespace</code>]] isn't specified (or if the index uses multiple
+
如果未指定 [[../fields#django.db.models.Field|Field.db_tablespace]](或索引使用多个字段),则在模型 <code>class Meta</code> 内的 [[../options#django.db.models.Options|db_tablespace]] 选项中指定的表空间中创建索引. 如果这两个表空间都没有设置,则在与表相同的表空间中创建索引。
fields), the index is created in tablespace specified in the
 
[[../options#django.db.models.Options|<code>db_tablespace</code>]] option inside the model's
 
<code>class Meta</code>. If neither of those tablespaces are set, the index is created
 
in the same tablespace as the table.
 
  
 
<div class="admonition seealso">
 
<div class="admonition seealso">
  
参见
+
也可以看看
  
For a list of PostgreSQL-specific indexes, see
+
有关 PostgreSQL 特定索引的列表,请参阅 [[../../contrib/postgres/indexes#module-django.contrib.postgres|django.contrib.postgres.indexes]]
[[../../contrib/postgres/indexes#module-django.contrib.postgres|<code>django.contrib.postgres.indexes</code>]].
 
  
  
第110行: 第85行:
 
<div id="opclasses" class="section">
 
<div id="opclasses" class="section">
  
=== <code>opclasses</code> ===
+
=== opclasses ===
  
; <code>Index.</code><code>opclasses</code>
+
; <span class="sig-prename descclassname"><span class="pre">Index.</span></span><span class="sig-name descname"><span class="pre">opclasses</span></span>
 
:  
 
:  
  
 
<div class="versionadded">
 
<div class="versionadded">
  
 +
<span class="versionmodified added">2.2 版中的新功能。</span>
  
  
 
</div>
 
</div>
The names of the [https://www.postgresql.org/docs/current/indexes-opclass.html PostgreSQL operator classes] to use for
+
用于此索引的 [https://www.postgresql.org/docs/current/indexes-opclass.html PostgreSQL 运算符类] 的名称。 如果您需要自定义运算符类,则必须为索引中的每个字段提供一个。
this index. If you require a custom operator class, you must provide one for
 
each field in the index.
 
  
For example, <code>GinIndex(name='json_index', fields=['jsonfield'], opclasses=['jsonb_path_ops'])</code> creates a gin index on <code>jsonfield</code> using
+
例如,<code>GinIndex(name='json_index', fields=['jsonfield'], opclasses=['jsonb_path_ops'])</code> 使用 <code>jsonb_path_ops</code> <code>jsonfield</code> 上创建一个 gin 索引。
<code>jsonb_path_ops</code>.
 
  
<code>opclasses</code> are ignored for databases besides PostgreSQL.
+
<code>opclasses</code> 对于 PostgreSQL 以外的数据库将被忽略。
  
[[#django.db.models.Index.name|<code>Index.name</code>]] is required when using <code>opclasses</code>.
+
使用 <code>opclasses</code> 时需要 [[#django.db.models.Index.name|Index.name]]
  
  
第135行: 第108行:
 
<div id="condition" class="section">
 
<div id="condition" class="section">
  
=== <code>condition</code> ===
+
=== condition ===
  
; <code>Index.</code><code>condition</code>
+
; <span class="sig-prename descclassname"><span class="pre">Index.</span></span><span class="sig-name descname"><span class="pre">condition</span></span>
 
:  
 
:  
  
 
<div class="versionadded">
 
<div class="versionadded">
  
 +
<span class="versionmodified added">2.2 版中的新功能。</span>
  
  
 
</div>
 
</div>
If the table is very large and your queries mostly target a subset of rows,
+
如果表非常大并且您的查询主要针对行的子集,则将索引限制为该子集可能很有用。 将条件指定为 [[../querysets#django.db.models|Q]]。 例如,<code>condition=Q(pages__gt=400)</code> 索引超过 400 页的记录。
it may be useful to restrict an index to that subset. Specify a condition as a
 
[[../querysets#django.db.models|<code>Q</code>]]. For example, <code>condition=Q(pages__gt=400)</code>
 
indexes records with more than 400 pages.
 
  
[[#django.db.models.Index.name|<code>Index.name</code>]] is required when using <code>condition</code>.
+
使用 <code>condition</code> 时需要 [[#django.db.models.Index.name|Index.name]]
  
 
<div class="admonition-restrictions-on-postgresql admonition">
 
<div class="admonition-restrictions-on-postgresql admonition">
  
Restrictions on PostgreSQL
+
PostgreSQL 的限制
  
PostgreSQL requires functions referenced in the condition to be marked as
+
PostgreSQL 要求将条件中引用的函数标记为 IMMUTABLE。 Django 不会验证这一点,但 PostgreSQL 会出错。 这意味着不接受诸如 [[../database-functions#date-functions|Date 函数]] [[../database-functions#django.db.models.functions|Concat]] 之类的函数。 如果您将日期存储在 [[../fields#django.db.models|DateTimeField]] 中,与 <code>datetime</code> 对象的比较可能需要提供 <code>tzinfo</code> 参数,否则比较可能会由于转换 Django 而导致可变函数用于 [[../querysets#field-lookups|查找]]
IMMUTABLE. Django doesn't validate this but PostgreSQL will error. This
 
means that functions such as [[../database-functions#date-functions|<span class="std std-ref">Date functions</span>]] and
 
[[../database-functions#django.db.models.functions|<code>Concat</code>]] aren't accepted. If you store
 
dates in [[../fields#django.db.models|<code>DateTimeField</code>]], comparison to
 
<code>datetime</code> objects may require the <code>tzinfo</code> argument
 
to be provided because otherwise the comparison could result in a mutable
 
function due to the casting Django does for [[../querysets#field-lookups|<span class="std std-ref">lookups</span>]].
 
  
  
第169行: 第133行:
 
<div class="admonition-restrictions-on-sqlite admonition">
 
<div class="admonition-restrictions-on-sqlite admonition">
  
Restrictions on SQLite
+
SQLite 的限制
  
SQLite [https://www.sqlite.org/partialindex.html imposes restrictions]
+
SQLite [https://www.sqlite.org/partialindex.html 对如何构建部分索引施加了限制]
on how a partial index can be constructed.
 
  
  
第178行: 第141行:
 
<div class="admonition-oracle admonition">
 
<div class="admonition-oracle admonition">
  
Oracle
+
甲骨文
  
Oracle does not support partial indexes. Instead, partial indexes can be
+
Oracle 不支持部分索引。 相反,可以使用功能索引来模拟部分索引。 使用 [[../../../topics/migrations|migration]] 使用 [[../../migration-operations#django.db.migrations.operations|RunSQL]] 添加索引。
emulated using functional indexes. Use a [[../../../topics/migrations|<span class="doc">migration</span>]] to add the index using [[../../migration-operations#django.db.migrations.operations|<code>RunSQL</code>]].
 
  
  
第187行: 第149行:
 
<div class="admonition-mysql-and-mariadb admonition">
 
<div class="admonition-mysql-and-mariadb admonition">
  
MySQL and MariaDB
+
MySQL MariaDB
  
The <code>condition</code> argument is ignored with MySQL and MariaDB as neither
+
MySQL 和 MariaDB 会忽略 <code>condition</code> 参数,因为它们都不支持条件索引。
supports conditional indexes.
 
  
  
第200行: 第161行:
  
 
</div>
 
</div>
 +
<div class="clearer">
  
[[Category:Django 3.0.x 中文文档]]
+
 
 +
 
 +
</div>
 +
 
 +
[[Category:Django 3.0.x 文档]]

2021年10月31日 (日) 04:09的最新版本

型号索引参考

索引类简化了创建数据库索引的过程。 可以使用 Meta.indexes 选项添加它们。 本文档解释了 Index 的 API 参考,其中包括 索引选项

引用内置索引

索引在 django.db.models.indexes 中定义,但为了方便起见,它们被导入 django.db.models。 标准约定是使用 from django.db import models 并将索引称为 models.<IndexClass>


Index 选项

class Index(fields=(), name=None, db_tablespace=None, opclasses=(), condition=None)
在数据库中创建索引(B 树)。

fields

Index.fields

需要索引的字段名称的列表或元组。

默认情况下,索引是按每列的升序创建的。 要为列定义降序索引,请在字段名称前添加一个连字符。

例如,Index(fields=['headline', '-pub_date']) 将使用 (headline, pub_date DESC) 创建 SQL。 MySQL 不支持索引排序。 在这种情况下,会创建一个降序索引作为普通索引。


name

Index.name

索引的名称。 如果未提供 name,Django 将自动生成一个名称。 为了兼容不同的数据库,索引名称不能超过 30 个字符,并且不应以数字 (0-9) 或下划线 (_) 开头。

抽象基类中的部分索引

您必须始终为索引指定唯一名称。 因此,您通常不能在抽象基类上指定部分索引,因为 Meta.indexes 选项由子类继承,属性值完全相同(包括 name)每一次。 为了解决名称冲突,部分名称可能包含 '%(app_label)s''%(class)s',分别由具体模型的小写应用程序标签和类名称替换。 例如Index(fields=['title'], name='%(app_label)s_%(class)s_title_index')


3.0 版更改:添加了 '%(app_label)s''%(class)s' 的插值。


db_tablespace

Index.db_tablespace

用于此索引的 数据库表空间 的名称。 对于单字段索引,如果未提供 db_tablespace,则在字段的 db_tablespace 中创建索引。

如果未指定 Field.db_tablespace(或索引使用多个字段),则在模型 class Meta 内的 db_tablespace 选项中指定的表空间中创建索引. 如果这两个表空间都没有设置,则在与表相同的表空间中创建索引。

也可以看看

有关 PostgreSQL 特定索引的列表,请参阅 django.contrib.postgres.indexes


opclasses

Index.opclasses

2.2 版中的新功能。


用于此索引的 PostgreSQL 运算符类 的名称。 如果您需要自定义运算符类,则必须为索引中的每个字段提供一个。

例如,GinIndex(name='json_index', fields=['jsonfield'], opclasses=['jsonb_path_ops']) 使用 jsonb_path_opsjsonfield 上创建一个 gin 索引。

opclasses 对于 PostgreSQL 以外的数据库将被忽略。

使用 opclasses 时需要 Index.name


condition

Index.condition

2.2 版中的新功能。


如果表非常大并且您的查询主要针对行的子集,则将索引限制为该子集可能很有用。 将条件指定为 Q。 例如,condition=Q(pages__gt=400) 索引超过 400 页的记录。

使用 condition 时需要 Index.name

对 PostgreSQL 的限制

PostgreSQL 要求将条件中引用的函数标记为 IMMUTABLE。 Django 不会验证这一点,但 PostgreSQL 会出错。 这意味着不接受诸如 Date 函数Concat 之类的函数。 如果您将日期存储在 DateTimeField 中,与 datetime 对象的比较可能需要提供 tzinfo 参数,否则比较可能会由于转换 Django 而导致可变函数用于 查找


SQLite 的限制

SQLite 对如何构建部分索引施加了限制


甲骨文

Oracle 不支持部分索引。 相反,可以使用功能索引来模拟部分索引。 使用 migration 使用 RunSQL 添加索引。


MySQL 和 MariaDB

MySQL 和 MariaDB 会忽略 condition 参数,因为它们都不支持条件索引。