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