{"title":"表空间（Tablespaces）","version":"3.2","locale":"zh-hans","docname":"topics/db/tablespaces","url":"/zh-hans/3.2/topics/db/tablespaces/","canonical":"https://djangodocs.dev/zh-hans/3.2/topics/db/tablespaces/","summary":"优化数据库表现的常见方式是利用表空间来组织磁盘布局。表空间可被指定给特定模型生成的数据表，通过在该模型的 class Meta 中提供 db_tablespace  选项。","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 class=\"reference external\" href=\"https://en.wikipedia.org/wiki/Tablespace\">表空间</a> 来组织磁盘布局。</p>\n<aside class=\"admonition admonition-warning\" role=\"note\">\n<p class=\"admonition-title\">Warning</p>\n<p>Django 没有为你创建表空间。请参考你使用的数据库引擎的文档了解如何创建和管理表空间。</p>\n</aside>\n<section id=\"declaring-tablespaces-for-tables\">\n<h2>为表格申明表空间<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>表空间可被指定给特定模型生成的数据表，通过在该模型的 <code class=\"docutils literal notranslate\">class Meta</code> 中提供 <a class=\"reference internal\" href=\"/zh-hans/3.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\">db_tablespace</code></a>  选项。该选项也影响此模型中为 <a class=\"reference internal\" href=\"/zh-hans/3.2/ref/models/fields/#django.db.models.ManyToManyField\" title=\"django.db.models.ManyToManyField\"><code class=\"xref py py-class docutils literal notranslate\">ManyToManyField</code></a> 自动创建的数据表。</p>\n<p>你可以用 <a class=\"reference internal\" href=\"/zh-hans/3.2/ref/settings/#std-setting-DEFAULT_TABLESPACE\"><code class=\"xref std std-setting docutils literal notranslate\">DEFAULT_TABLESPACE</code></a> 配置为 <a class=\"reference internal\" href=\"/zh-hans/3.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\">db_tablespace</code></a> 指定一个默认值。这在配置内置的 Django 应用或其它你无法修改代码的应用的表空间时非常有用。</p>\n</section>\n<section id=\"declaring-tablespaces-for-indexes\">\n<h2>为索引申明表空间<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>你可以为 <code class=\"docutils literal notranslate\">Index</code> 构造器传入 <a class=\"reference internal\" href=\"/zh-hans/3.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\">db_tablespace</code></a> 选项指定索引使用的表空间名。对于单个字段的索引，你可以向 <code class=\"docutils literal notranslate\">Field</code> 构造器传入 <a class=\"reference internal\" href=\"/zh-hans/3.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\">db_tablespace</code></a> 选项，为字段列索引指定可选的表空间。若此列没有索引，会忽略该选项。</p>\n<p>你可以用 <a class=\"reference internal\" href=\"/zh-hans/3.2/ref/settings/#std-setting-DEFAULT_INDEX_TABLESPACE\"><code class=\"xref std std-setting docutils literal notranslate\">DEFAULT_INDEX_TABLESPACE</code></a> 配置项为 <a class=\"reference internal\" href=\"/zh-hans/3.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\">db_tablespace</code></a> 指定一个默认值。</p>\n<p>若未指定 <a class=\"reference internal\" href=\"/zh-hans/3.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\">db_tablespace</code></a> 和 <a class=\"reference internal\" href=\"/zh-hans/3.2/ref/settings/#std-setting-DEFAULT_INDEX_TABLESPACE\"><code class=\"xref std std-setting docutils literal notranslate\">DEFAULT_INDEX_TABLESPACE</code></a>，索引会在与数据表相同的表空间中创建。</p>\n</section>\n<section id=\"an-example\">\n<h2>一个例子<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=\"nc\">TablespaceExample</span><span class=\"p\">(</span>models<span class=\"o\">.</span>Model<span class=\"p\">):</span>\n    name <span class=\"o\">=</span> models<span class=\"o\">.</span>CharField<span class=\"p\">(</span>max_length<span class=\"o\">=</span><span class=\"mi\">30</span><span class=\"p\">,</span> db_index<span class=\"o\">=</span><span class=\"kc\">True</span><span class=\"p\">,</span> db_tablespace<span class=\"o\">=</span><span class=\"s2\">&quot;indexes&quot;</span><span class=\"p\">)</span>\n    data <span class=\"o\">=</span> models<span class=\"o\">.</span>CharField<span class=\"p\">(</span>max_length<span class=\"o\">=</span><span class=\"mi\">255</span><span class=\"p\">,</span> db_index<span class=\"o\">=</span><span class=\"kc\">True</span><span class=\"p\">)</span>\n    shortcut <span class=\"o\">=</span> models<span class=\"o\">.</span>CharField<span class=\"p\">(</span>max_length<span class=\"o\">=</span><span class=\"mi\">7</span><span class=\"p\">)</span>\n    edges <span class=\"o\">=</span> models<span class=\"o\">.</span>ManyToManyField<span class=\"p\">(</span>to<span class=\"o\">=</span><span class=\"s2\">&quot;self&quot;</span><span class=\"p\">,</span> db_tablespace<span class=\"o\">=</span><span class=\"s2\">&quot;indexes&quot;</span><span class=\"p\">)</span>\n\n    <span class=\"k\">class</span> <span class=\"nc\">Meta</span><span class=\"p\">:</span>\n        db_tablespace <span class=\"o\">=</span> <span class=\"s2\">&quot;tables&quot;</span>\n        indexes <span class=\"o\">=</span> <span class=\"p\">[</span>models<span class=\"o\">.</span>Index<span class=\"p\">(</span>fields<span class=\"o\">=</span><span class=\"p\">[</span><span class=\"s1\">&#39;shortcut&#39;</span><span class=\"p\">],</span> db_tablespace<span class=\"o\">=</span><span class=\"s1\">&#39;other_indexes&#39;</span><span class=\"p\">)]</span>\n</code></pre></div>\n<p>本例中， <code class=\"docutils literal notranslate\">TablespaceExample</code> 模型（即模型表和多对多表）创建的表会被存在 <code class=\"docutils literal notranslate\">tables</code> 表空间。名称字段和多对多表的索引会被存在该 <code class=\"docutils literal notranslate\">indexes</code> 表空间中。 <code class=\"docutils literal notranslate\">data</code> 字段也会生成一个索引，但因为并未为其指定表空间，所以它会被默认存入模型的表空间 <code class=\"docutils literal notranslate\">tables</code> 中。 <code class=\"docutils literal notranslate\">shortcut</code> 字段的索引会被存入 <code class=\"docutils literal notranslate\">other_indexes</code> 表空间。</p>\n</section>\n<section id=\"database-support\">\n<h2>数据库支持<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 和 Oracle 支持表空间。而 SQLite， MariaDB 和 MySQL 不支持。</p>\n<p>若你使用的后端不支持表空间，Django 会忽略所有与表空间相关的选项。</p>\n</section>","rootId":"tablespaces","toc":[{"title":"为表格申明表空间","anchor":"declaring-tablespaces-for-tables","children":[]},{"title":"为索引申明表空间","anchor":"declaring-tablespaces-for-indexes","children":[]},{"title":"一个例子","anchor":"an-example","children":[]},{"title":"数据库支持","anchor":"database-support","children":[]}],"breadcrumbs":[{"docname":"topics/index","title":"使用 Django","url":"/zh-hans/3.2/topics/"},{"docname":"topics/db/index","title":"模型和数据库","url":"/zh-hans/3.2/topics/db/"}],"prev":{"docname":"topics/db/multi-db","title":"多数据库","url":"/zh-hans/3.2/topics/db/multi-db/"},"next":{"docname":"topics/db/optimization","title":"数据库访问优化","url":"/zh-hans/3.2/topics/db/optimization/"},"formats":{"html":"/zh-hans/3.2/topics/db/tablespaces/","markdown":"/zh-hans/3.2/topics/db/tablespaces.md","json":"/zh-hans/3.2/topics/db/tablespaces.json"},"source":"https://github.com/django/django/blob/stable/3.2.x/docs/topics/db/tablespaces.txt","official":"https://docs.djangoproject.com/zh-hans/3.2/topics/db/tablespaces/","inVersions":["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"],"inLocales":["en","zh-hans","fr","ja","id","it","pt-br","ko","es","el","pl"]}