<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>Tablespaces — Django 1.4.20 documentation</title> <link rel="stylesheet" href="../../_static/default.css" type="text/css" /> <link rel="stylesheet" href="../../_static/pygments.css" type="text/css" /> <script type="text/javascript"> var DOCUMENTATION_OPTIONS = { URL_ROOT: '../../', VERSION: '1.4.20', COLLAPSE_INDEX: false, FILE_SUFFIX: '.html', HAS_SOURCE: true }; </script> <script type="text/javascript" src="../../_static/jquery.js"></script> <script type="text/javascript" src="../../_static/underscore.js"></script> <script type="text/javascript" src="../../_static/doctools.js"></script> <link rel="top" title="Django 1.4.20 documentation" href="../../index.html" /> <link rel="up" title="Models and databases" href="index.html" /> <link rel="next" title="Database access optimization" href="optimization.html" /> <link rel="prev" title="Multiple databases" href="multi-db.html" /> <script type="text/javascript" src="../../templatebuiltins.js"></script> <script type="text/javascript"> (function($) { if (!django_template_builtins) { // templatebuiltins.js missing, do nothing. return; } $(document).ready(function() { // Hyperlink Django template tags and filters var base = "../../ref/templates/builtins.html"; if (base == "#") { // Special case for builtins.html itself base = ""; } // Tags are keywords, class '.k' $("div.highlight\\-html\\+django span.k").each(function(i, elem) { var tagname = $(elem).text(); if ($.inArray(tagname, django_template_builtins.ttags) != -1) { var fragment = tagname.replace(/_/, '-'); $(elem).html("<a href='" + base + "#" + fragment + "'>" + tagname + "</a>"); } }); // Filters are functions, class '.nf' $("div.highlight\\-html\\+django span.nf").each(function(i, elem) { var filtername = $(elem).text(); if ($.inArray(filtername, django_template_builtins.tfilters) != -1) { var fragment = filtername.replace(/_/, '-'); $(elem).html("<a href='" + base + "#" + fragment + "'>" + filtername + "</a>"); } }); }); })(jQuery); </script> </head> <body> <div class="document"> <div id="custom-doc" class="yui-t6"> <div id="hd"> <h1><a href="../../index.html">Django 1.4.20 documentation</a></h1> <div id="global-nav"> <a title="Home page" href="../../index.html">Home</a> | <a title="Table of contents" href="../../contents.html">Table of contents</a> | <a title="Global index" href="../../genindex.html">Index</a> | <a title="Module index" href="../../py-modindex.html">Modules</a> </div> <div class="nav"> « <a href="multi-db.html" title="Multiple databases">previous</a> | <a href="../index.html" title="Using Django" accesskey="U">up</a> | <a href="optimization.html" title="Database access optimization">next</a> »</div> </div> <div id="bd"> <div id="yui-main"> <div class="yui-b"> <div class="yui-g" id="topics-db-tablespaces"> <div class="section" id="s-tablespaces"> <span id="tablespaces"></span><h1>Tablespaces<a class="headerlink" href="#tablespaces" title="Permalink to this headline">¶</a></h1> <p>A common paradigm for optimizing performance in database systems is the use of <a class="reference external" href="http://en.wikipedia.org/wiki/Tablespace">tablespaces</a> to organize disk layout.</p> <div class="admonition warning"> <p class="first admonition-title">Warning</p> <p class="last">Django does not create the tablespaces for you. Please refer to your database engine’s documentation for details on creating and managing tablespaces.</p> </div> <div class="section" id="s-declaring-tablespaces-for-tables"> <span id="declaring-tablespaces-for-tables"></span><h2>Declaring tablespaces for tables<a class="headerlink" href="#declaring-tablespaces-for-tables" title="Permalink to this headline">¶</a></h2> <p>A tablespace can be specified for the table generated by a model by supplying the <a class="reference internal" href="../../ref/models/options.html#django.db.models.Options.db_tablespace" title="django.db.models.Options.db_tablespace"><tt class="xref py py-attr docutils literal"><span class="pre">db_tablespace</span></tt></a> option inside the model’s <tt class="docutils literal"><span class="pre">class</span> <span class="pre">Meta</span></tt>. This option also affects tables automatically created for <a class="reference internal" href="../../ref/models/fields.html#django.db.models.ManyToManyField" title="django.db.models.ManyToManyField"><tt class="xref py py-class docutils literal"><span class="pre">ManyToManyField</span></tt></a>s in the model.</p> <p>You can use the <a class="reference internal" href="../../ref/settings.html#std:setting-DEFAULT_TABLESPACE"><tt class="xref std std-setting docutils literal"><span class="pre">DEFAULT_TABLESPACE</span></tt></a> setting to specify a default value for <a class="reference internal" href="../../ref/models/options.html#django.db.models.Options.db_tablespace" title="django.db.models.Options.db_tablespace"><tt class="xref py py-attr docutils literal"><span class="pre">db_tablespace</span></tt></a>. This is useful for setting a tablespace for the built-in Django apps and other applications whose code you cannot control.</p> </div> <div class="section" id="s-declaring-tablespaces-for-indexes"> <span id="declaring-tablespaces-for-indexes"></span><h2>Declaring tablespaces for indexes<a class="headerlink" href="#declaring-tablespaces-for-indexes" title="Permalink to this headline">¶</a></h2> <p>You can pass the <a class="reference internal" href="../../ref/models/fields.html#django.db.models.Field.db_tablespace" title="django.db.models.Field.db_tablespace"><tt class="xref py py-attr docutils literal"><span class="pre">db_tablespace</span></tt></a> option to a <tt class="docutils literal"><span class="pre">Field</span></tt> constructor to specify an alternate tablespace for the <tt class="docutils literal"><span class="pre">Field</span></tt>‘s column index. If no index would be created for the column, the option is ignored.</p> <p>You can use the <a class="reference internal" href="../../ref/settings.html#std:setting-DEFAULT_INDEX_TABLESPACE"><tt class="xref std std-setting docutils literal"><span class="pre">DEFAULT_INDEX_TABLESPACE</span></tt></a> setting to specify a default value for <a class="reference internal" href="../../ref/models/fields.html#django.db.models.Field.db_tablespace" title="django.db.models.Field.db_tablespace"><tt class="xref py py-attr docutils literal"><span class="pre">db_tablespace</span></tt></a>.</p> <p>If <a class="reference internal" href="../../ref/models/fields.html#django.db.models.Field.db_tablespace" title="django.db.models.Field.db_tablespace"><tt class="xref py py-attr docutils literal"><span class="pre">db_tablespace</span></tt></a> isn’t specified and you didn’t set <a class="reference internal" href="../../ref/settings.html#std:setting-DEFAULT_INDEX_TABLESPACE"><tt class="xref std std-setting docutils literal"><span class="pre">DEFAULT_INDEX_TABLESPACE</span></tt></a>, the index is created in the same tablespace as the tables.</p> </div> <div class="section" id="s-an-example"> <span id="an-example"></span><h2>An example<a class="headerlink" href="#an-example" title="Permalink to this headline">¶</a></h2> <div class="highlight-python"><div class="highlight"><pre><span class="k">class</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> <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="bp">True</span><span class="p">,</span> <span class="n">db_tablespace</span><span class="o">=</span><span class="s">"indexes"</span><span class="p">)</span> <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="bp">True</span><span class="p">)</span> <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="s">"self"</span><span class="p">,</span> <span class="n">db_tablespace</span><span class="o">=</span><span class="s">"indexes"</span><span class="p">)</span> <span class="k">class</span> <span class="nc">Meta</span><span class="p">:</span> <span class="n">db_tablespace</span> <span class="o">=</span> <span class="s">"tables"</span> </pre></div> </div> <p>In this example, the tables generated by the <tt class="docutils literal"><span class="pre">TablespaceExample</span></tt> model (i.e. the model table and the many-to-many table) would be stored in the <tt class="docutils literal"><span class="pre">tables</span></tt> tablespace. The index for the name field and the indexes on the many-to-many table would be stored in the <tt class="docutils literal"><span class="pre">indexes</span></tt> tablespace. The <tt class="docutils literal"><span class="pre">data</span></tt> field would also generate an index, but no tablespace for it is specified, so it would be stored in the model tablespace <tt class="docutils literal"><span class="pre">tables</span></tt> by default.</p> </div> <div class="section" id="s-database-support"> <span id="database-support"></span><h2>Database support<a class="headerlink" href="#database-support" title="Permalink to this headline">¶</a></h2> <p>PostgreSQL and Oracle support tablespaces. SQLite and MySQL don’t.</p> <p>When you use a backend that lacks support for tablespaces, Django ignores all tablespace-related options.</p> <div class="versionchanged"> <span class="title">Changed in Django 1.4:</span> Since Django 1.4, the PostgreSQL backend supports tablespaces.</div> </div> </div> </div> </div> </div> <div class="yui-b" id="sidebar"> <div class="sphinxsidebar"> <div class="sphinxsidebarwrapper"> <h3><a href="../../contents.html">Table Of Contents</a></h3> <ul> <li><a class="reference internal" href="#">Tablespaces</a><ul> <li><a class="reference internal" href="#declaring-tablespaces-for-tables">Declaring tablespaces for tables</a></li> <li><a class="reference internal" href="#declaring-tablespaces-for-indexes">Declaring tablespaces for indexes</a></li> <li><a class="reference internal" href="#an-example">An example</a></li> <li><a class="reference internal" href="#database-support">Database support</a></li> </ul> </li> </ul> <h3>Browse</h3> <ul> <li>Prev: <a href="multi-db.html">Multiple databases</a></li> <li>Next: <a href="optimization.html">Database access optimization</a></li> </ul> <h3>You are here:</h3> <ul> <li> <a href="../../index.html">Django 1.4.20 documentation</a> <ul><li><a href="../index.html">Using Django</a> <ul><li><a href="index.html">Models and databases</a> <ul><li>Tablespaces</li></ul> </li></ul></li></ul> </li> </ul> <h3>This Page</h3> <ul class="this-page-menu"> <li><a href="../../_sources/topics/db/tablespaces.txt" rel="nofollow">Show Source</a></li> </ul> <div id="searchbox" style="display: none"> <h3>Quick search</h3> <form class="search" action="../../search.html" method="get"> <input type="text" name="q" /> <input type="submit" value="Go" /> <input type="hidden" name="check_keywords" value="yes" /> <input type="hidden" name="area" value="default" /> </form> <p class="searchtip" style="font-size: 90%"> Enter search terms or a module, class or function name. </p> </div> <script type="text/javascript">$('#searchbox').show(0);</script> </div> </div> <h3>Last update:</h3> <p class="topless">Mar 19, 2015</p> </div> </div> <div id="ft"> <div class="nav"> « <a href="multi-db.html" title="Multiple databases">previous</a> | <a href="../index.html" title="Using Django" accesskey="U">up</a> | <a href="optimization.html" title="Database access optimization">next</a> »</div> </div> </div> <div class="clearer"></div> </div> </body> </html>