AI教程网 - 未来以来,拥抱AI;新手入门,从AI教程网开始......


Django2.0手册 AI君 135℃

A common paradigm for optimizing performance in database systems is the use of
tablespaces to organize disk layout.


Django does not create the tablespaces for you. Please refer to your
database engine’s documentation for details on creating and managing

Declaring tablespaces for tables¶

A tablespace can be specified for the table generated by a model by supplying
the db_tablespace option inside the model’s
class Meta. This option also affects tables automatically created for
ManyToManyFields in the model.

You can use the DEFAULT_TABLESPACE setting to specify a default value
for db_tablespace. This is useful for setting
a tablespace for the built-in Django apps and other applications whose code you
cannot control.

Declaring tablespaces for indexes¶

You can pass the db_tablespace option to an
Index constructor to specify the name of a tablespace to use for the index.
For single field indexes, you can pass the
db_tablespace option to a Field constructor
to specify an alternate tablespace for the field’s column index. If the column
doesn’t have an index, the option is ignored.

You can use the DEFAULT_INDEX_TABLESPACE setting to specify
a default value for db_tablespace.

If db_tablespace isn’t specified and you didn’t
set DEFAULT_INDEX_TABLESPACE, the index is created in the same
tablespace as the tables.


class TablespaceExample(models.Model):
    name = models.CharField(max_length=30, db_index=True, db_tablespace="indexes")
    data = models.CharField(max_length=255, db_index=True)
    shortcut = models.CharField(max_length=7)
    edges = models.ManyToManyField(to="self", db_tablespace="indexes")

    class Meta:
        db_tablespace = "tables"
        indexes = [models.Index(fields=['shortcut'], db_tablespace='other_indexes')]

In this example, the tables generated by the TablespaceExample model (i.e.
the model table and the many-to-many table) would be stored in the tables
tablespace. The index for the name field and the indexes on the many-to-many
table would be stored in the indexes tablespace. The data field would
also generate an index, but no tablespace for it is specified, so it would be
stored in the model tablespace tables by default. The index for the
shortcut field would be stored in the other_indexes tablespace.

Database support¶

PostgreSQL and Oracle support tablespaces. SQLite and MySQL don’t.

When you use a backend that lacks support for tablespaces, Django ignores all
tablespace-related options.

转载请注明 » Django2.0手册:Tablespaces

喜欢 (0)or分享 (0)