{"title":"Tablespaces","version":"4.2","locale":"en","docname":"topics/db/tablespaces","url":"/en/4.2/topics/db/tablespaces/","canonical":"https://djangodocs.dev/en/4.2/topics/db/tablespaces/","summary":"A common paradigm for optimizing performance in database systems is the use of tablespaces to organize disk layout. Warning Django does not create the tablespaces…","html":"<h1>Tablespaces<a class=\"heading-anchor\" href=\"#tablespaces\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h1>\n<p>A common paradigm for optimizing performance in database systems is the use of\n<a class=\"reference external\" href=\"https://en.wikipedia.org/wiki/Tablespace\">tablespaces</a> to organize disk layout.</p>\n<aside class=\"admonition admonition-warning\" role=\"note\">\n<p class=\"admonition-title\">Warning</p>\n<p>Django does not create the tablespaces for you. Please refer to your\ndatabase engine’s documentation for details on creating and managing\ntablespaces.</p>\n</aside>\n<section id=\"declaring-tablespaces-for-tables\">\n<h2>Declaring tablespaces for tables<a class=\"heading-anchor\" href=\"#declaring-tablespaces-for-tables\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h2>\n<p>A tablespace can be specified for the table generated by a model by supplying\nthe <a class=\"reference internal\" href=\"/en/4.2/ref/models/options/#django.db.models.Options.db_tablespace\" title=\"django.db.models.Options.db_tablespace\"><code class=\"xref py py-attr docutils literal notranslate\"><span class=\"pre\">db_tablespace</span></code></a> option inside the model’s\n<code class=\"docutils literal notranslate\"><span class=\"pre\">class</span> <span class=\"pre\">Meta</span></code>. This option also affects tables automatically created for\n<a class=\"reference internal\" href=\"/en/4.2/ref/models/fields/#django.db.models.ManyToManyField\" title=\"django.db.models.ManyToManyField\"><code class=\"xref py py-class docutils literal notranslate\"><span class=\"pre\">ManyToManyField</span></code></a>s in the model.</p>\n<p>You can use the <a class=\"reference internal\" href=\"/en/4.2/ref/settings/#std-setting-DEFAULT_TABLESPACE\"><code class=\"xref std std-setting docutils literal notranslate\"><span class=\"pre\">DEFAULT_TABLESPACE</span></code></a> setting to specify a default value\nfor <a class=\"reference internal\" href=\"/en/4.2/ref/models/options/#django.db.models.Options.db_tablespace\" title=\"django.db.models.Options.db_tablespace\"><code class=\"xref py py-attr docutils literal notranslate\"><span class=\"pre\">db_tablespace</span></code></a>. This is useful for setting\na tablespace for the built-in Django apps and other applications whose code you\ncannot control.</p>\n</section>\n<section id=\"declaring-tablespaces-for-indexes\">\n<h2>Declaring tablespaces for indexes<a class=\"heading-anchor\" href=\"#declaring-tablespaces-for-indexes\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h2>\n<p>You can pass the <a class=\"reference internal\" href=\"/en/4.2/ref/models/indexes/#django.db.models.Index.db_tablespace\" title=\"django.db.models.Index.db_tablespace\"><code class=\"xref py py-attr docutils literal notranslate\"><span class=\"pre\">db_tablespace</span></code></a> option to an\n<code class=\"docutils literal notranslate\"><span class=\"pre\">Index</span></code> constructor to specify the name of a tablespace to use for the index.\nFor single field indexes, you can pass the\n<a class=\"reference internal\" href=\"/en/4.2/ref/models/fields/#django.db.models.Field.db_tablespace\" title=\"django.db.models.Field.db_tablespace\"><code class=\"xref py py-attr docutils literal notranslate\"><span class=\"pre\">db_tablespace</span></code></a> option to a <code class=\"docutils literal notranslate\"><span class=\"pre\">Field</span></code> constructor\nto specify an alternate tablespace for the field’s column index. If the column\ndoesn’t have an index, the option is ignored.</p>\n<p>You can use the <a class=\"reference internal\" href=\"/en/4.2/ref/settings/#std-setting-DEFAULT_INDEX_TABLESPACE\"><code class=\"xref std std-setting docutils literal notranslate\"><span class=\"pre\">DEFAULT_INDEX_TABLESPACE</span></code></a> setting to specify\na default value for <a class=\"reference internal\" href=\"/en/4.2/ref/models/fields/#django.db.models.Field.db_tablespace\" title=\"django.db.models.Field.db_tablespace\"><code class=\"xref py py-attr docutils literal notranslate\"><span class=\"pre\">db_tablespace</span></code></a>.</p>\n<p>If <a class=\"reference internal\" href=\"/en/4.2/ref/models/fields/#django.db.models.Field.db_tablespace\" title=\"django.db.models.Field.db_tablespace\"><code class=\"xref py py-attr docutils literal notranslate\"><span class=\"pre\">db_tablespace</span></code></a> isn’t specified and you didn’t\nset <a class=\"reference internal\" href=\"/en/4.2/ref/settings/#std-setting-DEFAULT_INDEX_TABLESPACE\"><code class=\"xref std std-setting docutils literal notranslate\"><span class=\"pre\">DEFAULT_INDEX_TABLESPACE</span></code></a>, the index is created in the same\ntablespace as the tables.</p>\n</section>\n<section id=\"an-example\">\n<h2>An example<a class=\"heading-anchor\" href=\"#an-example\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h2>\n<div class=\"code-block\" data-language=\"python\"><div class=\"code-block-toolbar\"><span class=\"code-block-language\">Python</span><button type=\"button\" class=\"copy-button\" data-copy hidden><span class=\"copy-button-label\">Copy</span></button></div><pre role=\"group\" tabindex=\"0\" aria-label=\"Python code\"><code><span class=\"k\">class</span><span class=\"w\"> </span><span class=\"nc\">TablespaceExample</span><span class=\"p\">(</span><span class=\"n\">models</span><span class=\"o\">.</span><span class=\"n\">Model</span><span class=\"p\">):</span>\n    <span class=\"n\">name</span> <span class=\"o\">=</span> <span class=\"n\">models</span><span class=\"o\">.</span><span class=\"n\">CharField</span><span class=\"p\">(</span><span class=\"n\">max_length</span><span class=\"o\">=</span><span class=\"mi\">30</span><span class=\"p\">,</span> <span class=\"n\">db_index</span><span class=\"o\">=</span><span class=\"kc\">True</span><span class=\"p\">,</span> <span class=\"n\">db_tablespace</span><span class=\"o\">=</span><span class=\"s2\">&quot;indexes&quot;</span><span class=\"p\">)</span>\n    <span class=\"n\">data</span> <span class=\"o\">=</span> <span class=\"n\">models</span><span class=\"o\">.</span><span class=\"n\">CharField</span><span class=\"p\">(</span><span class=\"n\">max_length</span><span class=\"o\">=</span><span class=\"mi\">255</span><span class=\"p\">,</span> <span class=\"n\">db_index</span><span class=\"o\">=</span><span class=\"kc\">True</span><span class=\"p\">)</span>\n    <span class=\"n\">shortcut</span> <span class=\"o\">=</span> <span class=\"n\">models</span><span class=\"o\">.</span><span class=\"n\">CharField</span><span class=\"p\">(</span><span class=\"n\">max_length</span><span class=\"o\">=</span><span class=\"mi\">7</span><span class=\"p\">)</span>\n    <span class=\"n\">edges</span> <span class=\"o\">=</span> <span class=\"n\">models</span><span class=\"o\">.</span><span class=\"n\">ManyToManyField</span><span class=\"p\">(</span><span class=\"n\">to</span><span class=\"o\">=</span><span class=\"s2\">&quot;self&quot;</span><span class=\"p\">,</span> <span class=\"n\">db_tablespace</span><span class=\"o\">=</span><span class=\"s2\">&quot;indexes&quot;</span><span class=\"p\">)</span>\n\n    <span class=\"k\">class</span><span class=\"w\"> </span><span class=\"nc\">Meta</span><span class=\"p\">:</span>\n        <span class=\"n\">db_tablespace</span> <span class=\"o\">=</span> <span class=\"s2\">&quot;tables&quot;</span>\n        <span class=\"n\">indexes</span> <span class=\"o\">=</span> <span class=\"p\">[</span><span class=\"n\">models</span><span class=\"o\">.</span><span class=\"n\">Index</span><span class=\"p\">(</span><span class=\"n\">fields</span><span class=\"o\">=</span><span class=\"p\">[</span><span class=\"s2\">&quot;shortcut&quot;</span><span class=\"p\">],</span> <span class=\"n\">db_tablespace</span><span class=\"o\">=</span><span class=\"s2\">&quot;other_indexes&quot;</span><span class=\"p\">)]</span>\n</code></pre></div>\n<p>In this example, the tables generated by the <code class=\"docutils literal notranslate\"><span class=\"pre\">TablespaceExample</span></code> model (i.e.\nthe model table and the many-to-many table) would be stored in the <code class=\"docutils literal notranslate\"><span class=\"pre\">tables</span></code>\ntablespace. The index for the name field and the indexes on the many-to-many\ntable would be stored in the <code class=\"docutils literal notranslate\"><span class=\"pre\">indexes</span></code> tablespace. The <code class=\"docutils literal notranslate\"><span class=\"pre\">data</span></code> field would\nalso generate an index, but no tablespace for it is specified, so it would be\nstored in the model tablespace <code class=\"docutils literal notranslate\"><span class=\"pre\">tables</span></code> by default. The index for the\n<code class=\"docutils literal notranslate\"><span class=\"pre\">shortcut</span></code> field would be stored in the <code class=\"docutils literal notranslate\"><span class=\"pre\">other_indexes</span></code> tablespace.</p>\n</section>\n<section id=\"database-support\">\n<h2>Database support<a class=\"heading-anchor\" href=\"#database-support\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h2>\n<p>PostgreSQL and Oracle support tablespaces. SQLite, MariaDB and MySQL don’t.</p>\n<p>When you use a backend that lacks support for tablespaces, Django ignores all\ntablespace-related options.</p>\n</section>","rootId":"tablespaces","toc":[{"title":"Declaring tablespaces for tables","anchor":"declaring-tablespaces-for-tables","children":[]},{"title":"Declaring tablespaces for indexes","anchor":"declaring-tablespaces-for-indexes","children":[]},{"title":"An example","anchor":"an-example","children":[]},{"title":"Database support","anchor":"database-support","children":[]}],"breadcrumbs":[{"docname":"topics/index","title":"Using Django","url":"/en/4.2/topics/"},{"docname":"topics/db/index","title":"Models and databases","url":"/en/4.2/topics/db/"}],"prev":{"docname":"topics/db/multi-db","title":"Multiple databases","url":"/en/4.2/topics/db/multi-db/"},"next":{"docname":"topics/db/optimization","title":"Database access optimization","url":"/en/4.2/topics/db/optimization/"},"formats":{"html":"/en/4.2/topics/db/tablespaces/","markdown":"/en/4.2/topics/db/tablespaces.md","json":"/en/4.2/topics/db/tablespaces.json"},"source":"https://github.com/django/django/blob/stable/4.2.x/docs/topics/db/tablespaces.txt","official":"https://docs.djangoproject.com/en/4.2/topics/db/tablespaces/","inVersions":["dev","6.1","6.0","5.2","5.1","5.0","4.2","4.1","4.0","3.2","3.1","3.0","2.2","2.1","2.0","1.11","1.10","1.9","1.8"],"inLocales":["en","zh-hans","fr","ja","id","it","pt-br","ko","es","el","pl"]}