mysql官方手冊對 Using Per-Table Tablespaces 的說明
By default, all InnoDB tables and indexes are stored in the system tablespace. As an alternative, you can store each InnoDB
table and its indexes in its own file. This feature is called “multiple tablespaces” because each table that is created when this setting is in effect has its own tablespace.
Advantages of Per-Table Tablespaces
-
You can reclaim disk space when truncating or dropping a table. For tables created when file-per-table mode is turned off, truncating or dropping them creates free space internally in the ibdata files. That free space can only be used for new
InnoDB
data. -
The
TRUNCATE TABLE
.ibd
files. -
You can store specific tables on separate storage devices, for I/O optimization, space management, or backup purposes.
-
You can run
OPTIMIZE TABLE
to compact or recreate a tablespace. When you run anOPTIMIZE TABLE
,InnoDB
will create a new.ibd
InnoDB
removes the old.ibd
file and replaces it with the new.ibd
file. If the previous.ibd
file had grown significantly but actual data only accounted for a portion of its size, runningOPTIMIZE TABLE
allows you to reclaim the unused space. -
You can move individual
InnoDB
tables rather than entire databases. -
You can back up or restore a single table quickly, without interrupting the use of other
InnoDB
tables, using the MySQL Enterprise Backup product. See Backing Up and Restoring a Single.ibd
File for the procedure and restrictions. -
If
innodb_file_per_table
is disabled, there is one shared tablespace (the system tablespace) for tables, the data dictionary, and undo logs. This single tablespace has a 64TB size limit. Ifinnodb_file_per_table
is enabled, each table has its own tablespace, each with a 64TB size limit. See Section E.10.3, “Limits on Table Size”for related information.
Enabling and Disabling Multiple Tablespaces
To enable multiple tablespaces, start the server with the --innodb_file_per_table
option. For example, add a line to the [mysqld]
section of my.cnf
:
[mysqld] innodb_file_per_table
With multiple tablespaces enabled, InnoDB
stores each newly created table in its own
file in the appropriate database directory. Unlike the tbl_name
.ibdMyISAM
storage engine, with its separate
andtbl_name
.MYD
files for indexes and data, tbl_name
.MYIInnoDB
stores the data and the indexes together in a single .ibd
file. The
file is still created as usual.tbl_name
.frm
InnoDB
always needs the shared tablespace because it puts its internal data dictionary and undo logs there. The.ibd
files are not sufficient for InnoDB
to operate.
When a table is moved out of the system tablespace into its own .ibd
file, the data files that make up the system tablespace remain the same size. The space formerly occupied by the table can be reused for new InnoDB
data, but is not reclaimed for use by the operating system. When moving large InnoDB
tables out of the system tablespace, where disk space is limited, you might prefer to turn oninnodb_file_per_table
and then recreate the entire instance using the mysqldumpcommand.(Innodb table這樣使用起來非常不方便)