{"title":"Bordsytor","version":"5.2","locale":"sv","docname":"topics/db/tablespaces","url":"/sv/5.2/topics/db/tablespaces/","canonical":"https://djangodocs.dev/sv/5.2/topics/db/tablespaces/","summary":"En vanlig metod för att optimera prestanda i databassystem är att använda ”tablespaces” för att organisera disklayouten. Varning Django skapar inte tablespaces åt…","html":"<h1>Bordsytor<a class=\"heading-anchor\" href=\"#tablespaces\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h1>\n<p>En vanlig metod för att optimera prestanda i databassystem är att använda ”tablespaces” för att organisera disklayouten.</p>\n<aside class=\"admonition admonition-warning\" role=\"note\">\n<p class=\"admonition-title\">Varning</p>\n<p>Django skapar inte tablespaces åt dig. Se dokumentationen för din databasmotor för detaljer om hur du skapar och hanterar tablespaces.</p>\n</aside>\n<section id=\"declaring-tablespaces-for-tables\">\n<h2>Deklarera tablespaces för tabeller<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>Ett tablespace kan anges för den tabell som genereras av en modell genom att ange alternativet <a class=\"reference internal\" href=\"/sv/5.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> i modellens <code class=\"docutils literal notranslate\"><span class=\"pre\">class</span> <span class=\"pre\">Meta</span></code>. Detta alternativ påverkar även tabeller som skapas automatiskt för <a class=\"reference internal\" href=\"/sv/5.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> i modellen.</p>\n<p>Du kan använda inställningen <a class=\"reference internal\" href=\"/sv/5.2/ref/settings/#std-setting-DEFAULT_TABLESPACE\"><code class=\"xref std std-setting docutils literal notranslate\"><span class=\"pre\">DEFAULT_TABLESPACE</span></code></a> för att ange ett standardvärde för <a class=\"reference internal\" href=\"/sv/5.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>. Detta är användbart för att ställa in ett tablespace för de inbyggda Django-apparna och andra applikationer vars kod du inte kan kontrollera.</p>\n</section>\n<section id=\"declaring-tablespaces-for-indexes\">\n<h2>Deklarera tablespaces för index<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>Du kan skicka alternativet <a class=\"reference internal\" href=\"/sv/5.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> till en <code class=\"docutils literal notranslate\"><span class=\"pre\">Index</span></code>-konstruktör för att ange namnet på ett tablespace som ska användas för indexet. För index för enstaka fält kan du skicka alternativet <a class=\"reference internal\" href=\"/sv/5.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> till en <code class=\"docutils literal notranslate\"><span class=\"pre\">Field</span></code>-konstruktör för att ange ett alternativt tablespace för fältets kolumnindex. Om kolumnen inte har något index ignoreras alternativet.</p>\n<p>Du kan använda inställningen <a class=\"reference internal\" href=\"/sv/5.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> för att ange ett standardvärde för <a class=\"reference internal\" href=\"/sv/5.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>Om <a class=\"reference internal\" href=\"/sv/5.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> inte anges och du inte har angett <a class=\"reference internal\" href=\"/sv/5.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>, skapas indexet i samma tablespace som tabellerna.</p>\n</section>\n<section id=\"an-example\">\n<h2>Ett exempel<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>I det här exemplet lagras de tabeller som genereras av modellen <code class=\"docutils literal notranslate\"><span class=\"pre\">TablespaceExample</span></code> (dvs. modelltabellen och tabellen many-to-many) i tablespace <code class=\"docutils literal notranslate\"><span class=\"pre\">tables</span></code>. Indexet för fältet name och indexen för tabellen many-to-many lagras i tablespace <code class=\"docutils literal notranslate\"><span class=\"pre\">indexes</span></code>. Fältet <code class=\"docutils literal notranslate\"><span class=\"pre\">data</span></code> skulle också generera ett index, men inget tablespace för det har angetts, så det skulle lagras i modellens tablespace <code class=\"docutils literal notranslate\"><span class=\"pre\">tables</span></code> som standard. Indexet för fältet <code class=\"docutils literal notranslate\"><span class=\"pre\">shortcut</span></code> skulle lagras i tablespace <code class=\"docutils literal notranslate\"><span class=\"pre\">other_indexes</span></code>.</p>\n</section>\n<section id=\"database-support\">\n<h2>Stöd för databaser<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 och Oracle stöder tablespaces. SQLite, MariaDB och MySQL gör det inte.</p>\n<p>När du använder en backend som saknar stöd för tablespaces ignorerar Django alla tablespace-relaterade alternativ.</p>\n</section>","rootId":"tablespaces","toc":[{"title":"Deklarera tablespaces för tabeller","anchor":"declaring-tablespaces-for-tables","children":[]},{"title":"Deklarera tablespaces för index","anchor":"declaring-tablespaces-for-indexes","children":[]},{"title":"Ett exempel","anchor":"an-example","children":[]},{"title":"Stöd för databaser","anchor":"database-support","children":[]}],"breadcrumbs":[{"docname":"topics/index","title":"Använda Django","url":"/sv/5.2/topics/"},{"docname":"topics/db/index","title":"Modeller och databaser","url":"/sv/5.2/topics/db/"}],"prev":{"docname":"topics/db/multi-db","title":"Flera databaser","url":"/sv/5.2/topics/db/multi-db/"},"next":{"docname":"topics/db/optimization","title":"Optimering av databasåtkomst","url":"/sv/5.2/topics/db/optimization/"},"formats":{"html":"/sv/5.2/topics/db/tablespaces/","markdown":"/sv/5.2/topics/db/tablespaces.md","json":"/sv/5.2/topics/db/tablespaces.json"},"source":"https://github.com/django/django/blob/stable/5.2.x/docs/topics/db/tablespaces.txt","official":"https://docs.djangoproject.com/sv/5.2/topics/db/tablespaces/","inVersions":["6.1","6.0","5.2"],"inLocales":["en","sv","zh-hans","ga","fr","ja","id","it","pt-br","ko","es","el","pl"]}