indexes.txt 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. =====================
  2. Model index reference
  3. =====================
  4. .. module:: django.db.models.indexes
  5. .. currentmodule:: django.db.models
  6. Index classes ease creating database indexes. They can be added using the
  7. :attr:`Meta.indexes <django.db.models.Options.indexes>` option. This document
  8. explains the API references of :class:`Index` which includes the `index
  9. options`_.
  10. .. admonition:: Referencing built-in indexes
  11. Indexes are defined in ``django.db.models.indexes``, but for convenience
  12. they're imported into :mod:`django.db.models`. The standard convention is
  13. to use ``from django.db import models`` and refer to the indexes as
  14. ``models.<IndexClass>``.
  15. ``Index`` options
  16. =================
  17. .. class:: Index(fields=(), name=None, db_tablespace=None, opclasses=(), condition=None)
  18. Creates an index (B-Tree) in the database.
  19. ``fields``
  20. ----------
  21. .. attribute:: Index.fields
  22. A list or tuple of the name of the fields on which the index is desired.
  23. By default, indexes are created with an ascending order for each column. To
  24. define an index with a descending order for a column, add a hyphen before the
  25. field's name.
  26. For example ``Index(fields=['headline', '-pub_date'])`` would create SQL with
  27. ``(headline, pub_date DESC)``. Index ordering isn't supported on MySQL. In that
  28. case, a descending index is created as a normal index.
  29. ``name``
  30. --------
  31. .. attribute:: Index.name
  32. The name of the index. If ``name`` isn't provided Django will auto-generate a
  33. name. For compatibility with different databases, index names cannot be longer
  34. than 30 characters and shouldn't start with a number (0-9) or underscore (_).
  35. .. admonition:: Partial indexes in abstract base classes
  36. You must always specify a unique name for an index. As such, you
  37. cannot normally specify a partial index on an abstract base class, since
  38. the :attr:`Meta.indexes <django.db.models.Options.indexes>` option is
  39. inherited by subclasses, with exactly the same values for the attributes
  40. (including ``name``) each time. To work around name collisions, part of the
  41. name may contain ``'%(app_label)s'`` and ``'%(class)s'``, which are
  42. replaced, respectively, by the lowercased app label and class name of the
  43. concrete model. For example ``Index(fields=['title'],
  44. name='%(app_label)s_%(class)s_title_index')``.
  45. .. versionchanged:: 3.0
  46. Interpolation of ``'%(app_label)s'`` and ``'%(class)s'`` was added.
  47. ``db_tablespace``
  48. -----------------
  49. .. attribute:: Index.db_tablespace
  50. The name of the :doc:`database tablespace </topics/db/tablespaces>` to use for
  51. this index. For single field indexes, if ``db_tablespace`` isn't provided, the
  52. index is created in the ``db_tablespace`` of the field.
  53. If :attr:`.Field.db_tablespace` isn't specified (or if the index uses multiple
  54. fields), the index is created in tablespace specified in the
  55. :attr:`~django.db.models.Options.db_tablespace` option inside the model's
  56. ``class Meta``. If neither of those tablespaces are set, the index is created
  57. in the same tablespace as the table.
  58. .. seealso::
  59. For a list of PostgreSQL-specific indexes, see
  60. :mod:`django.contrib.postgres.indexes`.
  61. ``opclasses``
  62. -------------
  63. .. attribute:: Index.opclasses
  64. The names of the `PostgreSQL operator classes
  65. <https://www.postgresql.org/docs/current/indexes-opclass.html>`_ to use for
  66. this index. If you require a custom operator class, you must provide one for
  67. each field in the index.
  68. For example, ``GinIndex(name='json_index', fields=['jsonfield'],
  69. opclasses=['jsonb_path_ops'])`` creates a gin index on ``jsonfield`` using
  70. ``jsonb_path_ops``.
  71. ``opclasses`` are ignored for databases besides PostgreSQL.
  72. :attr:`Index.name` is required when using ``opclasses``.
  73. ``condition``
  74. -------------
  75. .. attribute:: Index.condition
  76. If the table is very large and your queries mostly target a subset of rows,
  77. it may be useful to restrict an index to that subset. Specify a condition as a
  78. :class:`~django.db.models.Q`. For example, ``condition=Q(pages__gt=400)``
  79. indexes records with more than 400 pages.
  80. :attr:`Index.name` is required when using ``condition``.
  81. .. admonition:: Restrictions on PostgreSQL
  82. PostgreSQL requires functions referenced in the condition to be marked as
  83. IMMUTABLE. Django doesn't validate this but PostgreSQL will error. This
  84. means that functions such as :ref:`date-functions` and
  85. :class:`~django.db.models.functions.Concat` aren't accepted. If you store
  86. dates in :class:`~django.db.models.DateTimeField`, comparison to
  87. :class:`~datetime.datetime` objects may require the ``tzinfo`` argument
  88. to be provided because otherwise the comparison could result in a mutable
  89. function due to the casting Django does for :ref:`lookups <field-lookups>`.
  90. .. admonition:: Restrictions on SQLite
  91. SQLite `imposes restrictions <https://www.sqlite.org/partialindex.html>`_
  92. on how a partial index can be constructed.
  93. .. admonition:: Oracle
  94. Oracle does not support partial indexes. Instead, partial indexes can be
  95. emulated using functional indexes. Use a :doc:`migration
  96. </topics/migrations>` to add the index using :class:`.RunSQL`.
  97. .. admonition:: MySQL and MariaDB
  98. The ``condition`` argument is ignored with MySQL and MariaDB as neither
  99. supports conditional indexes.