2016-06-25 16:32:56 +00:00
|
|
|
=====================
|
|
|
|
Model index reference
|
|
|
|
=====================
|
|
|
|
|
|
|
|
.. module:: django.db.models.indexes
|
|
|
|
|
|
|
|
.. currentmodule:: django.db.models
|
|
|
|
|
2016-06-20 15:50:05 +00:00
|
|
|
Index classes ease creating database indexes. They can be added using the
|
|
|
|
:attr:`Meta.indexes <django.db.models.Options.indexes>` option. This document
|
|
|
|
explains the API references of :class:`Index` which includes the `index
|
|
|
|
options`_.
|
2016-06-25 16:32:56 +00:00
|
|
|
|
|
|
|
.. admonition:: Referencing built-in indexes
|
|
|
|
|
|
|
|
Indexes are defined in ``django.db.models.indexes``, but for convenience
|
|
|
|
they're imported into :mod:`django.db.models`. The standard convention is
|
|
|
|
to use ``from django.db import models`` and refer to the indexes as
|
|
|
|
``models.<IndexClass>``.
|
|
|
|
|
|
|
|
``Index`` options
|
|
|
|
=================
|
|
|
|
|
2019-10-10 18:04:17 +00:00
|
|
|
.. class:: Index(*expressions, fields=(), name=None, db_tablespace=None, opclasses=(), condition=None, include=None)
|
2016-06-25 16:32:56 +00:00
|
|
|
|
|
|
|
Creates an index (B-Tree) in the database.
|
|
|
|
|
2019-10-10 18:04:17 +00:00
|
|
|
``expressions``
|
|
|
|
---------------
|
|
|
|
|
|
|
|
.. attribute:: Index.expressions
|
|
|
|
|
|
|
|
Positional argument ``*expressions`` allows creating functional indexes on
|
|
|
|
expressions and database functions.
|
|
|
|
|
|
|
|
For example::
|
|
|
|
|
|
|
|
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.
|
|
|
|
|
|
|
|
:attr:`Index.name` is required when using ``*expressions``.
|
|
|
|
|
|
|
|
.. admonition:: Restrictions on Oracle
|
|
|
|
|
|
|
|
Oracle requires functions referenced in an index to be marked as
|
|
|
|
``DETERMINISTIC``. Django doesn't validate this but Oracle will error. This
|
|
|
|
means that functions such as
|
|
|
|
:class:`Random() <django.db.models.functions.Random>` aren't accepted.
|
|
|
|
|
|
|
|
.. admonition:: Restrictions on PostgreSQL
|
|
|
|
|
|
|
|
PostgreSQL requires functions and operators referenced in an index to be
|
|
|
|
marked as ``IMMUTABLE``. Django doesn't validate this but PostgreSQL will
|
|
|
|
error. This means that functions such as
|
|
|
|
:class:`Concat() <django.db.models.functions.Concat>` aren't accepted.
|
|
|
|
|
|
|
|
.. admonition:: MySQL and MariaDB
|
|
|
|
|
|
|
|
Functional indexes are ignored with MySQL < 8.0.13 and MariaDB as neither
|
|
|
|
supports them.
|
|
|
|
|
2016-06-25 16:32:56 +00:00
|
|
|
``fields``
|
2017-03-20 22:30:32 +00:00
|
|
|
----------
|
2016-06-25 16:32:56 +00:00
|
|
|
|
|
|
|
.. attribute:: Index.fields
|
|
|
|
|
2018-03-08 15:56:55 +00:00
|
|
|
A list or tuple of the name of the fields on which the index is desired.
|
2016-06-25 16:32:56 +00:00
|
|
|
|
2016-07-22 12:52:44 +00:00
|
|
|
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 ``Index(fields=['headline', '-pub_date'])`` would create SQL with
|
2022-04-20 09:59:48 +00:00
|
|
|
``(headline, pub_date DESC)``.
|
|
|
|
|
2023-08-03 16:54:29 +00:00
|
|
|
.. admonition:: MariaDB
|
2022-04-20 09:59:48 +00:00
|
|
|
|
2023-08-03 16:54:29 +00:00
|
|
|
Index ordering isn't supported on MariaDB < 10.8. In that case, a
|
|
|
|
descending index is created as a normal index.
|
2016-07-22 12:52:44 +00:00
|
|
|
|
2016-06-25 16:32:56 +00:00
|
|
|
``name``
|
|
|
|
--------
|
|
|
|
|
|
|
|
.. attribute:: Index.name
|
|
|
|
|
|
|
|
The name of the index. If ``name`` isn't provided Django will auto-generate a
|
|
|
|
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 (_).
|
2016-08-08 11:50:25 +00:00
|
|
|
|
2019-04-24 10:58:49 +00:00
|
|
|
.. admonition:: Partial indexes in abstract base classes
|
|
|
|
|
|
|
|
You must always specify a unique name for an index. As such, you
|
|
|
|
cannot normally specify a partial index on an abstract base class, since
|
|
|
|
the :attr:`Meta.indexes <django.db.models.Options.indexes>` option is
|
|
|
|
inherited by subclasses, with exactly the same values for the attributes
|
2019-07-05 12:15:41 +00:00
|
|
|
(including ``name``) each time. To work around name collisions, part of the
|
|
|
|
name may contain ``'%(app_label)s'`` and ``'%(class)s'``, which are
|
|
|
|
replaced, respectively, by the lowercased app label and class name of the
|
|
|
|
concrete model. For example ``Index(fields=['title'],
|
|
|
|
name='%(app_label)s_%(class)s_title_index')``.
|
2019-04-24 10:58:49 +00:00
|
|
|
|
2017-06-27 19:15:15 +00:00
|
|
|
``db_tablespace``
|
|
|
|
-----------------
|
|
|
|
|
|
|
|
.. attribute:: Index.db_tablespace
|
|
|
|
|
|
|
|
The name of the :doc:`database tablespace </topics/db/tablespaces>` to use for
|
|
|
|
this index. For single field indexes, if ``db_tablespace`` isn't provided, the
|
|
|
|
index is created in the ``db_tablespace`` of the field.
|
|
|
|
|
|
|
|
If :attr:`.Field.db_tablespace` isn't specified (or if the index uses multiple
|
|
|
|
fields), the index is created in tablespace specified in the
|
|
|
|
:attr:`~django.db.models.Options.db_tablespace` option inside the model's
|
|
|
|
``class Meta``. If neither of those tablespaces are set, the index is created
|
|
|
|
in the same tablespace as the table.
|
|
|
|
|
2016-08-08 11:50:25 +00:00
|
|
|
.. seealso::
|
|
|
|
|
|
|
|
For a list of PostgreSQL-specific indexes, see
|
|
|
|
:mod:`django.contrib.postgres.indexes`.
|
2017-10-11 17:25:52 +00:00
|
|
|
|
|
|
|
``opclasses``
|
|
|
|
-------------
|
|
|
|
|
|
|
|
.. attribute:: Index.opclasses
|
|
|
|
|
|
|
|
The names of the `PostgreSQL operator classes
|
2019-03-30 01:49:44 +00:00
|
|
|
<https://www.postgresql.org/docs/current/indexes-opclass.html>`_ to use for
|
2017-10-11 17:25:52 +00:00
|
|
|
this index. If you require a custom operator class, you must provide one for
|
|
|
|
each field in the index.
|
|
|
|
|
|
|
|
For example, ``GinIndex(name='json_index', fields=['jsonfield'],
|
|
|
|
opclasses=['jsonb_path_ops'])`` creates a gin index on ``jsonfield`` using
|
|
|
|
``jsonb_path_ops``.
|
|
|
|
|
|
|
|
``opclasses`` are ignored for databases besides PostgreSQL.
|
|
|
|
|
|
|
|
:attr:`Index.name` is required when using ``opclasses``.
|
2018-09-13 07:34:02 +00:00
|
|
|
|
|
|
|
``condition``
|
|
|
|
-------------
|
|
|
|
|
|
|
|
.. attribute:: Index.condition
|
|
|
|
|
|
|
|
If the table is very large and your queries mostly target a subset of rows,
|
|
|
|
it may be useful to restrict an index to that subset. Specify a condition as a
|
|
|
|
:class:`~django.db.models.Q`. For example, ``condition=Q(pages__gt=400)``
|
|
|
|
indexes records with more than 400 pages.
|
|
|
|
|
|
|
|
:attr:`Index.name` is required when using ``condition``.
|
|
|
|
|
|
|
|
.. admonition:: Restrictions on PostgreSQL
|
|
|
|
|
2019-06-24 06:58:56 +00:00
|
|
|
PostgreSQL requires functions referenced in the condition to be marked as
|
2018-09-13 07:34:02 +00:00
|
|
|
IMMUTABLE. Django doesn't validate this but PostgreSQL will error. This
|
|
|
|
means that functions such as :ref:`date-functions` and
|
|
|
|
:class:`~django.db.models.functions.Concat` aren't accepted. If you store
|
|
|
|
dates in :class:`~django.db.models.DateTimeField`, comparison to
|
|
|
|
:class:`~datetime.datetime` objects may require the ``tzinfo`` argument
|
|
|
|
to be provided because otherwise the comparison could result in a mutable
|
|
|
|
function due to the casting Django does for :ref:`lookups <field-lookups>`.
|
|
|
|
|
|
|
|
.. admonition:: Restrictions on SQLite
|
|
|
|
|
|
|
|
SQLite `imposes restrictions <https://www.sqlite.org/partialindex.html>`_
|
|
|
|
on how a partial index can be constructed.
|
|
|
|
|
|
|
|
.. admonition:: Oracle
|
|
|
|
|
|
|
|
Oracle does not support partial indexes. Instead, partial indexes can be
|
2019-10-10 18:04:17 +00:00
|
|
|
emulated by using functional indexes together with
|
|
|
|
:class:`~django.db.models.expressions.Case` expressions.
|
2018-09-13 07:34:02 +00:00
|
|
|
|
|
|
|
.. admonition:: MySQL and MariaDB
|
|
|
|
|
|
|
|
The ``condition`` argument is ignored with MySQL and MariaDB as neither
|
|
|
|
supports conditional indexes.
|
2019-10-31 12:33:53 +00:00
|
|
|
|
|
|
|
``include``
|
|
|
|
-----------
|
|
|
|
|
|
|
|
.. attribute:: Index.include
|
|
|
|
|
|
|
|
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 (:attr:`~Index.include`) and filter only by indexed
|
|
|
|
fields (:attr:`~Index.fields`).
|
|
|
|
|
|
|
|
For example::
|
|
|
|
|
|
|
|
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.
|
|
|
|
|
|
|
|
:attr:`Index.name` is required when using ``include``.
|
|
|
|
|
|
|
|
See the PostgreSQL documentation for more details about `covering indexes`_.
|
|
|
|
|
|
|
|
.. admonition:: Restrictions on PostgreSQL
|
|
|
|
|
2022-05-19 07:26:48 +00:00
|
|
|
PostgreSQL supports covering B-Tree and :class:`GiST indexes
|
|
|
|
<django.contrib.postgres.indexes.GistIndex>`. PostgreSQL 14+ also supports
|
|
|
|
covering :class:`SP-GiST indexes
|
2021-05-28 22:52:57 +00:00
|
|
|
<django.contrib.postgres.indexes.SpGistIndex>`.
|
|
|
|
|
2019-10-31 12:33:53 +00:00
|
|
|
.. _covering indexes: https://www.postgresql.org/docs/current/indexes-index-only-scans.html
|