tablespaces.txt 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  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`: http://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.Field.db_tablespace` option to a
  24. ``Field`` constructor to specify an alternate tablespace for the ``Field``’s
  25. column index. If no index would be created for the column, the option is
  26. ignored.
  27. You can use the :setting:`DEFAULT_INDEX_TABLESPACE` setting to specify
  28. a default value for :attr:`~django.db.models.Field.db_tablespace`.
  29. If :attr:`~django.db.models.Field.db_tablespace` isn't specified and you didn't
  30. set :setting:`DEFAULT_INDEX_TABLESPACE`, the index is created in the same
  31. tablespace as the tables.
  32. An example
  33. ----------
  34. .. code-block:: python
  35. class TablespaceExample(models.Model):
  36. name = models.CharField(max_length=30, db_index=True, db_tablespace="indexes")
  37. data = models.CharField(max_length=255, db_index=True)
  38. edges = models.ManyToManyField(to="self", db_tablespace="indexes")
  39. class Meta:
  40. db_tablespace = "tables"
  41. In this example, the tables generated by the ``TablespaceExample`` model (i.e.
  42. the model table and the many-to-many table) would be stored in the ``tables``
  43. tablespace. The index for the name field and the indexes on the many-to-many
  44. table would be stored in the ``indexes`` tablespace. The ``data`` field would
  45. also generate an index, but no tablespace for it is specified, so it would be
  46. stored in the model tablespace ``tables`` by default.
  47. Database support
  48. ----------------
  49. PostgreSQL and Oracle support tablespaces. SQLite and MySQL don't.
  50. When you use a backend that lacks support for tablespaces, Django ignores all
  51. tablespace-related options.