tablespaces.txt 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. ===========
  2. Tablespaces
  3. ===========
  4. A common paradigm for optimizing performance in database systems is the use of
  5. `tablespaces`_ to organize disk layout.
  6. .. _`tablespaces`: https://en.wikipedia.org/wiki/Tablespace
  7. .. warning::
  8. Django does not create the tablespaces for you. Please refer to your
  9. database engine's documentation for details on creating and managing
  10. tablespaces.
  11. Declaring tablespaces for tables
  12. ================================
  13. A tablespace can be specified for the table generated by a model by supplying
  14. the :attr:`~django.db.models.Options.db_tablespace` option inside the model's
  15. ``class Meta``. This option also affects tables automatically created for
  16. :class:`~django.db.models.ManyToManyField`\ s in the model.
  17. You can use the :setting:`DEFAULT_TABLESPACE` setting to specify a default value
  18. for :attr:`~django.db.models.Options.db_tablespace`. This is useful for setting
  19. a tablespace for the built-in Django apps and other applications whose code you
  20. cannot control.
  21. Declaring tablespaces for indexes
  22. =================================
  23. You can pass the :attr:`~django.db.models.Index.db_tablespace` option to an
  24. ``Index`` constructor to specify the name of a tablespace to use for the index.
  25. For single field indexes, you can pass the
  26. :attr:`~django.db.models.Field.db_tablespace` option to a ``Field`` constructor
  27. to specify an alternate tablespace for the field's column index. If the column
  28. doesn't have an index, the option is ignored.
  29. You can use the :setting:`DEFAULT_INDEX_TABLESPACE` setting to specify
  30. a default value for :attr:`~django.db.models.Field.db_tablespace`.
  31. If :attr:`~django.db.models.Field.db_tablespace` isn't specified and you didn't
  32. set :setting:`DEFAULT_INDEX_TABLESPACE`, the index is created in the same
  33. tablespace as the tables.
  34. An example
  35. ==========
  36. .. code-block:: python
  37. class TablespaceExample(models.Model):
  38. name = models.CharField(max_length=30, db_index=True, db_tablespace="indexes")
  39. data = models.CharField(max_length=255, db_index=True)
  40. shortcut = models.CharField(max_length=7)
  41. edges = models.ManyToManyField(to="self", db_tablespace="indexes")
  42. class Meta:
  43. db_tablespace = "tables"
  44. indexes = [models.Index(fields=["shortcut"], db_tablespace="other_indexes")]
  45. In this example, the tables generated by the ``TablespaceExample`` model (i.e.
  46. the model table and the many-to-many table) would be stored in the ``tables``
  47. tablespace. The index for the name field and the indexes on the many-to-many
  48. table would be stored in the ``indexes`` tablespace. The ``data`` field would
  49. also generate an index, but no tablespace for it is specified, so it would be
  50. stored in the model tablespace ``tables`` by default. The index for the
  51. ``shortcut`` field would be stored in the ``other_indexes`` tablespace.
  52. Database support
  53. ================
  54. PostgreSQL and Oracle support tablespaces. SQLite, MariaDB and MySQL don't.
  55. When you use a backend that lacks support for tablespaces, Django ignores all
  56. tablespace-related options.