{"title":"Model index reference","version":"4.2","locale":"el","docname":"ref/models/indexes","url":"/el/4.2/ref/models/indexes/","canonical":"https://djangodocs.dev/el/4.2/ref/models/indexes/","summary":"Index classes ease creating database indexes. They can be added using the Meta.indexes option. This document explains the API references of Index which includes the…","html":"<span id=\"model-index-reference\"></span><h1>Model index reference<a class=\"heading-anchor\" href=\"#module-django.db.models.indexes\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h1>\n<p>Index classes ease creating database indexes. They can be added using the\n<a class=\"reference internal\" href=\"/el/4.2/ref/models/options/#django.db.models.Options.indexes\" title=\"django.db.models.Options.indexes\"><code class=\"xref py py-attr docutils literal notranslate\"><span class=\"pre\">Meta.indexes</span></code></a> option. This document\nexplains the API references of <a class=\"reference internal\" href=\"#django.db.models.Index\" title=\"django.db.models.Index\"><code class=\"xref py py-class docutils literal notranslate\"><span class=\"pre\">Index</span></code></a> which includes the <a class=\"reference internal\" href=\"#index-options\">index\noptions</a>.</p>\n<aside class=\"admonition-referencing-built-in-indexes admonition\">\n<p class=\"admonition-title\">Referencing built-in indexes</p>\n<p>Indexes are defined in <code class=\"docutils literal notranslate\"><span class=\"pre\">django.db.models.indexes</span></code>, but for convenience\nthey’re imported into <a class=\"reference internal\" href=\"/el/4.2/topics/db/models/#module-django.db.models\" title=\"django.db.models\"><code class=\"xref py py-mod docutils literal notranslate\"><span class=\"pre\">django.db.models</span></code></a>. The standard convention is\nto use <code class=\"docutils literal notranslate\"><span class=\"pre\">from</span> <span class=\"pre\">django.db</span> <span class=\"pre\">import</span> <span class=\"pre\">models</span></code> and refer to the indexes as\n<code class=\"docutils literal notranslate\"><span class=\"pre\">models.&lt;IndexClass&gt;</span></code>.</p>\n</aside>\n<section id=\"index-options\">\n<h2><code class=\"docutils literal notranslate\"><span class=\"pre\">Index</span></code> options<a class=\"heading-anchor\" href=\"#index-options\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h2>\n<dl class=\"py class\">\n<dt class=\"sig sig-object py\" id=\"django.db.models.Index\">\n<em class=\"property\"><span class=\"k\"><span class=\"pre\">class</span></span><span class=\"w\"> </span></em><span class=\"sig-name descname\"><span class=\"pre\">Index</span></span><span class=\"sig-paren\">(</span><em class=\"sig-param\"><span class=\"o\"><span class=\"pre\">*</span></span><span class=\"n\"><span class=\"pre\">expressions</span></span></em>, <em class=\"sig-param\"><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></em>, <em class=\"sig-param\"><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></em>, <em class=\"sig-param\"><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></em>, <em class=\"sig-param\"><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></em>, <em class=\"sig-param\"><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></em>, <em class=\"sig-param\"><span class=\"n\"><span class=\"pre\">include</span></span><span class=\"o\"><span class=\"pre\">=</span></span><span class=\"default_value\"><span class=\"pre\">None</span></span></em><span class=\"sig-paren\">)</span><a class=\"heading-anchor\" href=\"#django.db.models.Index\"><span class=\"visually-hidden\">Link to this definition</span><span aria-hidden=\"true\">#</span></a></dt>\n<dd><p>Creates an index (B-Tree) in the database.</p>\n</dd></dl>\n\n<section id=\"expressions\">\n<h3><code class=\"docutils literal notranslate\"><span class=\"pre\">expressions</span></code><a class=\"heading-anchor\" href=\"#expressions\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h3>\n<dl class=\"py attribute\">\n<dt class=\"sig sig-object py\" id=\"django.db.models.Index.expressions\">\n<span class=\"sig-prename descclassname\"><span class=\"pre\">Index.</span></span><span class=\"sig-name descname\"><span class=\"pre\">expressions</span></span><a class=\"heading-anchor\" href=\"#django.db.models.Index.expressions\"><span class=\"visually-hidden\">Link to this definition</span><span aria-hidden=\"true\">#</span></a></dt>\n<dd></dd></dl>\n\n<p>Positional argument <code class=\"docutils literal notranslate\"><span class=\"pre\">*expressions</span></code> allows creating functional indexes on\nexpressions and database functions.</p>\n<p>For example:</p>\n<div class=\"code-block\" data-language=\"default\"><div class=\"code-block-toolbar\"><span class=\"code-block-language\">Code</span><button type=\"button\" class=\"copy-button\" data-copy hidden><span class=\"copy-button-label\">Copy</span></button></div><pre role=\"group\" tabindex=\"0\" aria-label=\"Code code\"><code><span class=\"n\">Index</span><span class=\"p\">(</span><span class=\"n\">Lower</span><span class=\"p\">(</span><span class=\"s2\">&quot;title&quot;</span><span class=\"p\">)</span><span class=\"o\">.</span><span class=\"n\">desc</span><span class=\"p\">(),</span> <span class=\"s2\">&quot;pub_date&quot;</span><span class=\"p\">,</span> <span class=\"n\">name</span><span class=\"o\">=</span><span class=\"s2\">&quot;lower_title_date_idx&quot;</span><span class=\"p\">)</span>\n</code></pre></div>\n<p>creates an index on the lowercased value of the <code class=\"docutils literal notranslate\"><span class=\"pre\">title</span></code> field in descending\norder and the <code class=\"docutils literal notranslate\"><span class=\"pre\">pub_date</span></code> field in the default ascending order.</p>\n<p>Another example:</p>\n<div class=\"code-block\" data-language=\"default\"><div class=\"code-block-toolbar\"><span class=\"code-block-language\">Code</span><button type=\"button\" class=\"copy-button\" data-copy hidden><span class=\"copy-button-label\">Copy</span></button></div><pre role=\"group\" tabindex=\"0\" aria-label=\"Code code\"><code><span class=\"n\">Index</span><span class=\"p\">(</span><span class=\"n\">F</span><span class=\"p\">(</span><span class=\"s2\">&quot;height&quot;</span><span class=\"p\">)</span> <span class=\"o\">*</span> <span class=\"n\">F</span><span class=\"p\">(</span><span class=\"s2\">&quot;weight&quot;</span><span class=\"p\">),</span> <span class=\"n\">Round</span><span class=\"p\">(</span><span class=\"s2\">&quot;weight&quot;</span><span class=\"p\">),</span> <span class=\"n\">name</span><span class=\"o\">=</span><span class=\"s2\">&quot;calc_idx&quot;</span><span class=\"p\">)</span>\n</code></pre></div>\n<p>creates an index on the result of multiplying fields <code class=\"docutils literal notranslate\"><span class=\"pre\">height</span></code> and <code class=\"docutils literal notranslate\"><span class=\"pre\">weight</span></code>\nand the <code class=\"docutils literal notranslate\"><span class=\"pre\">weight</span></code> rounded to the nearest integer.</p>\n<p><a class=\"reference internal\" href=\"#django.db.models.Index.name\" title=\"django.db.models.Index.name\"><code class=\"xref py py-attr docutils literal notranslate\"><span class=\"pre\">Index.name</span></code></a> is required when using <code class=\"docutils literal notranslate\"><span class=\"pre\">*expressions</span></code>.</p>\n<aside class=\"admonition-restrictions-on-oracle admonition\">\n<p class=\"admonition-title\">Restrictions on Oracle</p>\n<p>Oracle requires functions referenced in an index to be marked as\n<code class=\"docutils literal notranslate\"><span class=\"pre\">DETERMINISTIC</span></code>. Django doesn’t validate this but Oracle will error. This\nmeans that functions such as\n<a class=\"reference internal\" href=\"/el/4.2/ref/models/database-functions/#django.db.models.functions.Random\" title=\"django.db.models.functions.Random\"><code class=\"xref py py-class docutils literal notranslate\"><span class=\"pre\">Random()</span></code></a> aren’t accepted.</p>\n</aside>\n<aside class=\"admonition-restrictions-on-postgresql admonition\">\n<p class=\"admonition-title\">Restrictions on PostgreSQL</p>\n<p>PostgreSQL requires functions and operators referenced in an index to be\nmarked as <code class=\"docutils literal notranslate\"><span class=\"pre\">IMMUTABLE</span></code>. Django doesn’t validate this but PostgreSQL will\nerror. This means that functions such as\n<a class=\"reference internal\" href=\"/el/4.2/ref/models/database-functions/#django.db.models.functions.Concat\" title=\"django.db.models.functions.Concat\"><code class=\"xref py py-class docutils literal notranslate\"><span class=\"pre\">Concat()</span></code></a> aren’t accepted.</p>\n</aside>\n<aside class=\"admonition-mysql-and-mariadb admonition\">\n<p class=\"admonition-title\">MySQL and MariaDB</p>\n<p>Functional indexes are ignored with MySQL &lt; 8.0.13 and MariaDB as neither\nsupports them.</p>\n</aside>\n</section>\n<section id=\"fields\">\n<h3><code class=\"docutils literal notranslate\"><span class=\"pre\">fields</span></code><a class=\"heading-anchor\" href=\"#fields\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h3>\n<dl class=\"py attribute\">\n<dt class=\"sig sig-object py\" id=\"django.db.models.Index.fields\">\n<span class=\"sig-prename descclassname\"><span class=\"pre\">Index.</span></span><span class=\"sig-name descname\"><span class=\"pre\">fields</span></span><a class=\"heading-anchor\" href=\"#django.db.models.Index.fields\"><span class=\"visually-hidden\">Link to this definition</span><span aria-hidden=\"true\">#</span></a></dt>\n<dd></dd></dl>\n\n<p>A list or tuple of the name of the fields on which the index is desired.</p>\n<p>By default, indexes are created with an ascending order for each column. To\ndefine an index with a descending order for a column, add a hyphen before the\nfield’s name.</p>\n<p>For example <code class=\"docutils literal notranslate\"><span class=\"pre\">Index(fields=['headline',</span> <span class=\"pre\">'-pub_date'])</span></code> would create SQL with\n<code class=\"docutils literal notranslate\"><span class=\"pre\">(headline,</span> <span class=\"pre\">pub_date</span> <span class=\"pre\">DESC)</span></code>.</p>\n<aside class=\"admonition-mysql-and-mariadb admonition\">\n<p class=\"admonition-title\">MySQL and MariaDB</p>\n<p>Index ordering isn’t supported on MySQL &lt; 8.0.1 and MariaDB &lt; 10.8. In that\ncase, a descending index is created as a normal index.</p>\n</aside>\n</section>\n<section id=\"name\">\n<h3><code class=\"docutils literal notranslate\"><span class=\"pre\">name</span></code><a class=\"heading-anchor\" href=\"#name\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h3>\n<dl class=\"py attribute\">\n<dt class=\"sig sig-object py\" id=\"django.db.models.Index.name\">\n<span class=\"sig-prename descclassname\"><span class=\"pre\">Index.</span></span><span class=\"sig-name descname\"><span class=\"pre\">name</span></span><a class=\"heading-anchor\" href=\"#django.db.models.Index.name\"><span class=\"visually-hidden\">Link to this definition</span><span aria-hidden=\"true\">#</span></a></dt>\n<dd></dd></dl>\n\n<p>The name of the index. If <code class=\"docutils literal notranslate\"><span class=\"pre\">name</span></code> isn’t provided Django will auto-generate a\nname. For compatibility with different databases, index names cannot be longer\nthan 30 characters and shouldn’t start with a number (0-9) or underscore (_).</p>\n<aside class=\"admonition-partial-indexes-in-abstract-base-classes admonition\">\n<p class=\"admonition-title\">Partial indexes in abstract base classes</p>\n<p>You must always specify a unique name for an index. As such, you\ncannot normally specify a partial index on an abstract base class, since\nthe <a class=\"reference internal\" href=\"/el/4.2/ref/models/options/#django.db.models.Options.indexes\" title=\"django.db.models.Options.indexes\"><code class=\"xref py py-attr docutils literal notranslate\"><span class=\"pre\">Meta.indexes</span></code></a> option is\ninherited by subclasses, with exactly the same values for the attributes\n(including <code class=\"docutils literal notranslate\"><span class=\"pre\">name</span></code>) each time. To work around name collisions, part of the\nname may contain <code class=\"docutils literal notranslate\"><span class=\"pre\">'%(app_label)s'</span></code> and <code class=\"docutils literal notranslate\"><span class=\"pre\">'%(class)s'</span></code>, which are\nreplaced, respectively, by the lowercased app label and class name of the\nconcrete model. For example <code class=\"docutils literal notranslate\"><span class=\"pre\">Index(fields=['title'],</span>\n<span class=\"pre\">name='%(app_label)s_%(class)s_title_index')</span></code>.</p>\n</aside>\n</section>\n<section id=\"db-tablespace\">\n<h3><code class=\"docutils literal notranslate\"><span class=\"pre\">db_tablespace</span></code><a class=\"heading-anchor\" href=\"#db-tablespace\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h3>\n<dl class=\"py attribute\">\n<dt class=\"sig sig-object py\" id=\"django.db.models.Index.db_tablespace\">\n<span class=\"sig-prename descclassname\"><span class=\"pre\">Index.</span></span><span class=\"sig-name descname\"><span class=\"pre\">db_tablespace</span></span><a class=\"heading-anchor\" href=\"#django.db.models.Index.db_tablespace\"><span class=\"visually-hidden\">Link to this definition</span><span aria-hidden=\"true\">#</span></a></dt>\n<dd></dd></dl>\n\n<p>The name of the <a class=\"reference internal\" href=\"/el/4.2/topics/db/tablespaces/\"><span class=\"doc\">database tablespace</span></a> to use for\nthis index. For single field indexes, if <code class=\"docutils literal notranslate\"><span class=\"pre\">db_tablespace</span></code> isn’t provided, the\nindex is created in the <code class=\"docutils literal notranslate\"><span class=\"pre\">db_tablespace</span></code> of the field.</p>\n<p>If <a class=\"reference internal\" href=\"/el/4.2/ref/models/fields/#django.db.models.Field.db_tablespace\" title=\"django.db.models.Field.db_tablespace\"><code class=\"xref py py-attr docutils literal notranslate\"><span class=\"pre\">Field.db_tablespace</span></code></a> isn’t specified (or if the index uses multiple\nfields), the index is created in tablespace specified in the\n<a class=\"reference internal\" href=\"/el/4.2/ref/models/options/#django.db.models.Options.db_tablespace\" title=\"django.db.models.Options.db_tablespace\"><code class=\"xref py py-attr docutils literal notranslate\"><span class=\"pre\">db_tablespace</span></code></a> option inside the model’s\n<code class=\"docutils literal notranslate\"><span class=\"pre\">class</span> <span class=\"pre\">Meta</span></code>. If neither of those tablespaces are set, the index is created\nin the same tablespace as the table.</p>\n<aside class=\"admonition admonition-seealso\">\n<p class=\"admonition-title\">Δείτε επίσης</p>\n<p>For a list of PostgreSQL-specific indexes, see\n<a class=\"reference internal\" href=\"/el/4.2/ref/contrib/postgres/indexes/#module-django.contrib.postgres.indexes\" title=\"django.contrib.postgres.indexes\"><code class=\"xref py py-mod docutils literal notranslate\"><span class=\"pre\">django.contrib.postgres.indexes</span></code></a>.</p>\n</aside>\n</section>\n<section id=\"opclasses\">\n<h3><code class=\"docutils literal notranslate\"><span class=\"pre\">opclasses</span></code><a class=\"heading-anchor\" href=\"#opclasses\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h3>\n<dl class=\"py attribute\">\n<dt class=\"sig sig-object py\" id=\"django.db.models.Index.opclasses\">\n<span class=\"sig-prename descclassname\"><span class=\"pre\">Index.</span></span><span class=\"sig-name descname\"><span class=\"pre\">opclasses</span></span><a class=\"heading-anchor\" href=\"#django.db.models.Index.opclasses\"><span class=\"visually-hidden\">Link to this definition</span><span aria-hidden=\"true\">#</span></a></dt>\n<dd></dd></dl>\n\n<p>The names of the <a class=\"reference external\" href=\"https://www.postgresql.org/docs/current/indexes-opclass.html\">PostgreSQL operator classes</a> to use for\nthis index. If you require a custom operator class, you must provide one for\neach field in the index.</p>\n<p>For example, <code class=\"docutils literal notranslate\"><span class=\"pre\">GinIndex(name='json_index',</span> <span class=\"pre\">fields=['jsonfield'],</span>\n<span class=\"pre\">opclasses=['jsonb_path_ops'])</span></code> creates a gin index on <code class=\"docutils literal notranslate\"><span class=\"pre\">jsonfield</span></code> using\n<code class=\"docutils literal notranslate\"><span class=\"pre\">jsonb_path_ops</span></code>.</p>\n<p><code class=\"docutils literal notranslate\"><span class=\"pre\">opclasses</span></code> are ignored for databases besides PostgreSQL.</p>\n<p><a class=\"reference internal\" href=\"#django.db.models.Index.name\" title=\"django.db.models.Index.name\"><code class=\"xref py py-attr docutils literal notranslate\"><span class=\"pre\">Index.name</span></code></a> is required when using <code class=\"docutils literal notranslate\"><span class=\"pre\">opclasses</span></code>.</p>\n</section>\n<section id=\"condition\">\n<h3><code class=\"docutils literal notranslate\"><span class=\"pre\">condition</span></code><a class=\"heading-anchor\" href=\"#condition\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h3>\n<dl class=\"py attribute\">\n<dt class=\"sig sig-object py\" id=\"django.db.models.Index.condition\">\n<span class=\"sig-prename descclassname\"><span class=\"pre\">Index.</span></span><span class=\"sig-name descname\"><span class=\"pre\">condition</span></span><a class=\"heading-anchor\" href=\"#django.db.models.Index.condition\"><span class=\"visually-hidden\">Link to this definition</span><span aria-hidden=\"true\">#</span></a></dt>\n<dd></dd></dl>\n\n<p>If the table is very large and your queries mostly target a subset of rows,\nit may be useful to restrict an index to that subset. Specify a condition as a\n<a class=\"reference internal\" href=\"/el/4.2/ref/models/querysets/#django.db.models.Q\" title=\"django.db.models.Q\"><code class=\"xref py py-class docutils literal notranslate\"><span class=\"pre\">Q</span></code></a>. For example, <code class=\"docutils literal notranslate\"><span class=\"pre\">condition=Q(pages__gt=400)</span></code>\nindexes records with more than 400 pages.</p>\n<p><a class=\"reference internal\" href=\"#django.db.models.Index.name\" title=\"django.db.models.Index.name\"><code class=\"xref py py-attr docutils literal notranslate\"><span class=\"pre\">Index.name</span></code></a> is required when using <code class=\"docutils literal notranslate\"><span class=\"pre\">condition</span></code>.</p>\n<aside class=\"admonition-restrictions-on-postgresql admonition\">\n<p class=\"admonition-title\">Restrictions on PostgreSQL</p>\n<p>PostgreSQL requires functions referenced in the condition to be marked as\nIMMUTABLE. Django doesn’t validate this but PostgreSQL will error. This\nmeans that functions such as <a class=\"reference internal\" href=\"/el/4.2/ref/models/database-functions/#date-functions\"><span class=\"std std-ref\">Date functions</span></a> and\n<a class=\"reference internal\" href=\"/el/4.2/ref/models/database-functions/#django.db.models.functions.Concat\" title=\"django.db.models.functions.Concat\"><code class=\"xref py py-class docutils literal notranslate\"><span class=\"pre\">Concat</span></code></a> aren’t accepted. If you store\ndates in <a class=\"reference internal\" href=\"/el/4.2/ref/models/fields/#django.db.models.DateTimeField\" title=\"django.db.models.DateTimeField\"><code class=\"xref py py-class docutils literal notranslate\"><span class=\"pre\">DateTimeField</span></code></a>, comparison to\n<a class=\"reference external\" href=\"https://docs.python.org/3/library/datetime.html#datetime.datetime\" title=\"(στη Python έκδοση 3.14)\"><code class=\"xref py py-class docutils literal notranslate\"><span class=\"pre\">datetime</span></code></a> objects may require the <code class=\"docutils literal notranslate\"><span class=\"pre\">tzinfo</span></code> argument\nto be provided because otherwise the comparison could result in a mutable\nfunction due to the casting Django does for <a class=\"reference internal\" href=\"/el/4.2/ref/models/querysets/#field-lookups\"><span class=\"std std-ref\">lookups</span></a>.</p>\n</aside>\n<aside class=\"admonition-restrictions-on-sqlite admonition\">\n<p class=\"admonition-title\">Restrictions on SQLite</p>\n<p>SQLite <a class=\"reference external\" href=\"https://www.sqlite.org/partialindex.html\">imposes restrictions</a>\non how a partial index can be constructed.</p>\n</aside>\n<aside class=\"admonition-oracle admonition\">\n<p class=\"admonition-title\">Oracle</p>\n<p>Oracle does not support partial indexes. Instead, partial indexes can be\nemulated by using functional indexes together with\n<a class=\"reference internal\" href=\"/el/4.2/ref/models/conditional-expressions/#django.db.models.expressions.Case\" title=\"django.db.models.expressions.Case\"><code class=\"xref py py-class docutils literal notranslate\"><span class=\"pre\">Case</span></code></a> expressions.</p>\n</aside>\n<aside class=\"admonition-mysql-and-mariadb admonition\">\n<p class=\"admonition-title\">MySQL and MariaDB</p>\n<p>The <code class=\"docutils literal notranslate\"><span class=\"pre\">condition</span></code> argument is ignored with MySQL and MariaDB as neither\nsupports conditional indexes.</p>\n</aside>\n</section>\n<section id=\"include\">\n<h3><code class=\"docutils literal notranslate\"><span class=\"pre\">include</span></code><a class=\"heading-anchor\" href=\"#include\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h3>\n<dl class=\"py attribute\">\n<dt class=\"sig sig-object py\" id=\"django.db.models.Index.include\">\n<span class=\"sig-prename descclassname\"><span class=\"pre\">Index.</span></span><span class=\"sig-name descname\"><span class=\"pre\">include</span></span><a class=\"heading-anchor\" href=\"#django.db.models.Index.include\"><span class=\"visually-hidden\">Link to this definition</span><span aria-hidden=\"true\">#</span></a></dt>\n<dd></dd></dl>\n\n<p>A list or tuple of the names of the fields to be included in the covering index\nas non-key columns. This allows index-only scans to be used for queries that\nselect only included fields (<a class=\"reference internal\" href=\"#django.db.models.Index.include\" title=\"django.db.models.Index.include\"><code class=\"xref py py-attr docutils literal notranslate\"><span class=\"pre\">include</span></code></a>) and filter only by indexed\nfields (<a class=\"reference internal\" href=\"#django.db.models.Index.fields\" title=\"django.db.models.Index.fields\"><code class=\"xref py py-attr docutils literal notranslate\"><span class=\"pre\">fields</span></code></a>).</p>\n<p>For example:</p>\n<div class=\"code-block\" data-language=\"default\"><div class=\"code-block-toolbar\"><span class=\"code-block-language\">Code</span><button type=\"button\" class=\"copy-button\" data-copy hidden><span class=\"copy-button-label\">Copy</span></button></div><pre role=\"group\" tabindex=\"0\" aria-label=\"Code code\"><code><span class=\"n\">Index</span><span class=\"p\">(</span><span class=\"n\">name</span><span class=\"o\">=</span><span class=\"s2\">&quot;covering_index&quot;</span><span class=\"p\">,</span> <span class=\"n\">fields</span><span class=\"o\">=</span><span class=\"p\">[</span><span class=\"s2\">&quot;headline&quot;</span><span class=\"p\">],</span> <span class=\"n\">include</span><span class=\"o\">=</span><span class=\"p\">[</span><span class=\"s2\">&quot;pub_date&quot;</span><span class=\"p\">])</span>\n</code></pre></div>\n<p>will allow filtering on <code class=\"docutils literal notranslate\"><span class=\"pre\">headline</span></code>, also selecting <code class=\"docutils literal notranslate\"><span class=\"pre\">pub_date</span></code>, while\nfetching data only from the index.</p>\n<p>Using <code class=\"docutils literal notranslate\"><span class=\"pre\">include</span></code> will produce a smaller index than using a multiple column\nindex but with the drawback that non-key columns can not be used for sorting or\nfiltering.</p>\n<p><code class=\"docutils literal notranslate\"><span class=\"pre\">include</span></code> is ignored for databases besides PostgreSQL.</p>\n<p><a class=\"reference internal\" href=\"#django.db.models.Index.name\" title=\"django.db.models.Index.name\"><code class=\"xref py py-attr docutils literal notranslate\"><span class=\"pre\">Index.name</span></code></a> is required when using <code class=\"docutils literal notranslate\"><span class=\"pre\">include</span></code>.</p>\n<p>See the PostgreSQL documentation for more details about <a class=\"reference external\" href=\"https://www.postgresql.org/docs/current/indexes-index-only-scans.html\">covering indexes</a>.</p>\n<aside class=\"admonition-restrictions-on-postgresql admonition\">\n<p class=\"admonition-title\">Restrictions on PostgreSQL</p>\n<p>PostgreSQL supports covering B-Tree and <a class=\"reference internal\" href=\"/el/4.2/ref/contrib/postgres/indexes/#django.contrib.postgres.indexes.GistIndex\" title=\"django.contrib.postgres.indexes.GistIndex\"><code class=\"xref py py-class docutils literal notranslate\"><span class=\"pre\">GiST</span> <span class=\"pre\">indexes</span></code></a>. PostgreSQL 14+ also supports\ncovering <a class=\"reference internal\" href=\"/el/4.2/ref/contrib/postgres/indexes/#django.contrib.postgres.indexes.SpGistIndex\" title=\"django.contrib.postgres.indexes.SpGistIndex\"><code class=\"xref py py-class docutils literal notranslate\"><span class=\"pre\">SP-GiST</span> <span class=\"pre\">indexes</span></code></a>.</p>\n</aside>\n<aside class=\"version-note version-changed\" data-version=\"4.1\">\n<p class=\"version-note-title\">Changed in Django 4.1</p><p>Support for covering SP-GiST indexes with PostgreSQL 14+ was added.</p>\n</aside>\n</section>\n</section>","rootId":"module-django.db.models.indexes","toc":[{"title":"Index options","anchor":"index-options","children":[{"title":"expressions","anchor":"expressions","children":[]},{"title":"fields","anchor":"fields","children":[]},{"title":"name","anchor":"name","children":[]},{"title":"db_tablespace","anchor":"db-tablespace","children":[]},{"title":"opclasses","anchor":"opclasses","children":[]},{"title":"condition","anchor":"condition","children":[]},{"title":"include","anchor":"include","children":[]}]}],"breadcrumbs":[{"docname":"ref/index","title":"API Reference","url":"/el/4.2/ref/"},{"docname":"ref/models/index","title":"Models","url":"/el/4.2/ref/models/"}],"prev":{"docname":"ref/models/fields","title":"Model field reference","url":"/el/4.2/ref/models/fields/"},"next":{"docname":"ref/models/constraints","title":"Constraints reference","url":"/el/4.2/ref/models/constraints/"},"formats":{"html":"/el/4.2/ref/models/indexes/","markdown":"/el/4.2/ref/models/indexes.md","json":"/el/4.2/ref/models/indexes.json"},"source":"https://github.com/django/django/blob/stable/4.2.x/docs/ref/models/indexes.txt","official":"https://docs.djangoproject.com/el/4.2/ref/models/indexes/","inVersions":["6.1","6.0","5.2","5.1","5.0","4.2","4.1","4.0","3.2","3.1","3.0","2.2","2.1","2.0","1.11"],"inLocales":["en","zh-hans","fr","ja","id","it","pt-br","ko","es","el","pl"]}