MySQL5.6ではinnodb_file_per_tableがDefaultでテーブル毎にテーブルスペース(ファイル)が作成されますが、MySQL5.7.6 DMRからは、CREATE TABLESPACEステートメントによって、
複数テーブルで共有出来るテーブルスペースが作成出来るようになりました。また、Defaultデータディレクトリーとは別のパスにテーブルスペースを作成出来るので、
負荷が高いテーブルなどをSSDなどに配置するなど柔軟に対応することが可能になります。Oracle(テーブルスペース)やMS SQL(ファイルグループ)に関しては、
以前から同様に指定出来ますが、オープンソースデータベースのMySQLは5.7になり、更にそれらの商用データベースと同様の機能も利用出来る汎用性を備えたデータベースになってきました。
MySQL5.7はまだ5.7.7RCですが、更に新たな機能が追加されているので適宜可能な範囲で、こちらにてご紹介したいとお思います。

参照: 13.1.15 CREATE TABLESPACE Syntax
CREATE TABLESPACE is supported with InnoDB as of MySQL 5.7.6.
A general tablespace is a shared tablespace, similar to the system tablespace.
It can hold multiple tables, and supports all table row formats.
General tablespaces can also be created in a location relative to or independent of the MySQL data directory.

Tablespaceの作成


root@localhost [USER01]> CREATE TABLESPACE U_TABLESPACE01 ADD DATAFILE '/home/mysql/user_tablespace01.ibd' Engine=InnoDB;
Query OK, 0 rows affected (0.01 sec)

root@localhost [USER01]> CREATE TABLESPACE U_TABLESPACE02_8K ADD DATAFILE '/home/mysql/user_tablespace02_8k.ibd' FILE_BLOCK_S8192 Engine=InnoDB;
Query OK, 0 rows affected (0.01 sec)

テーブルスペースを指定してテーブルを作成

root@localhost [USER01]> CREATE TABLE `T_USER01` (
    -> `id` int(11) NOT NULL AUTO_INCREMENT,
    -> `text` varchar(100) DEFAULT NULL,
    ->  PRIMARY KEY (`id`)
    ->  ) TABLESPACE = U_TABLESPACE01 ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8mb4;
Query OK, 0 rows affected (0.01 sec)

root@localhost [USER01]> CREATE TABLE `T_USER02_8K` (
    -> `id` int(11) NOT NULL AUTO_INCREMENT,
    -> `text` varchar(100) DEFAULT NULL,
    -> PRIMARY KEY (`id`)
    -> ) TABLESPACE = U_TABLESPACE02_8K ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8mb4 ROW_FORMAT=COMPRESSED KEY_BLOC=8;
Query OK, 0 rows affected (0.00 sec)

root@localhost [USER01]> SELECT * FROM information_schema.INNODB_SYS_TABLESPACES where NAME like 'U_%';
+-------+-------------------+------+-------------+------------+-----------+---------------+------------+
| SPACE | NAME              | FLAG | FILE_FORMAT | ROW_FORMAT | PAGE_SIZE | ZIP_PAGE_SIZE | SPACE_TYPE |
+-------+-------------------+------+-------------+------------+-----------+---------------+------------+
|   165 | U_TABLESPACE01    | 2048 | Any         | Any        |     16384 |             0 | General    |
|   166 | U_TABLESPACE02_8K | 2089 | Barracuda   | Compressed |     16384 |          8192 | General    |
+-------+-------------------+------+-------------+------------+-----------+---------------+------------+
2 rows in set (0.00 sec)

tablespace2

Defaultデータディレクトリーとは別にテーブルスペースが作成されている事を確認


root@localhost [USER01]> show variables like 'datadir';
+---------------+------------------------+
| Variable_name | Value                  |
+---------------+------------------------+
| datadir       | /usr/local/mysql/data/ |
+---------------+------------------------+
1 row in set (0.00 sec)

root@localhost [USER01]> system ls /home/mysql/
user_tablespace01.ibd  user_tablespace02_8k.ibd
root@localhost [USER01]> 

先程、T_USER01を作成してテーブルスペースに追加でテーブルを作成


root@localhost [USER01]> CREATE TABLE `T_USER02` (
    -> `id` int(11) NOT NULL AUTO_INCREMENT,
    -> `text` varchar(100) DEFAULT NULL,
    -> PRIMARY KEY (`id`)
    -> ) TABLESPACE = U_TABLESPACE01 ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8mb4;
Query OK, 0 rows affected (0.00 sec)

root@localhost [USER01]> show tables;
+------------------+
| Tables_in_USER01 |
+------------------+
| T_USER01         |
| T_USER02         |
| T_USER02_8K      |
+------------------+
3 rows in set (0.00 sec)

root@localhost [USER01]> SELECT * FROM information_schema.INNODB_SYS_TABLES where NAME LIKE 'USER%';
+----------+--------------------+------+--------+-------+-------------+------------+---------------+------------+
| TABLE_ID | NAME               | FLAG | N_COLS | SPACE | FILE_FORMAT | ROW_FORMAT | ZIP_PAGE_SIZE | SPACE_TYPE |
+----------+--------------------+------+--------+-------+-------------+------------+---------------+------------+
|      171 | USER01/T_USER01    |  129 |      5 |   165 | Antelope    | Compact    |             0 | General    |
|      173 | USER01/T_USER02    |  129 |      5 |   165 | Antelope    | Compact    |             0 | General    |
|      172 | USER01/T_USER02_8K |  169 |      5 |   166 | Barracuda   | Compressed |          8192 | General    |
+----------+--------------------+------+--------+-------+-------------+------------+---------------+------------+
3 rows in set (0.00 sec)

root@localhost [USER01]> 

tablespace_innodb

既存のテーブルを共通テーブルスペースへ移動する場合はAlter Tableコマンドにて対応

root@localhost [USER01]> CREATE TABLE `T_USER03` (
    -> `id` int(11) NOT NULL AUTO_INCREMENT,
    -> `text` varchar(100) DEFAULT NULL,
    -> PRIMARY KEY (`id`)
    -> ) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8mb4;
Query OK, 0 rows affected (0.03 sec)

root@localhost [USER01]> SELECT * FROM information_schema.INNODB_SYS_TABLES where NAME LIKE 'USER%';
+----------+--------------------+------+--------+-------+-------------+------------+---------------+------------+
| TABLE_ID | NAME               | FLAG | N_COLS | SPACE | FILE_FORMAT | ROW_FORMAT | ZIP_PAGE_SIZE | SPACE_TYPE |
+----------+--------------------+------+--------+-------+-------------+------------+---------------+------------+
|      171 | USER01/T_USER01    |  129 |      5 |   165 | Antelope    | Compact    |             0 | General    |
|      173 | USER01/T_USER02    |  129 |      5 |   165 | Antelope    | Compact    |             0 | General    |
|      172 | USER01/T_USER02_8K |  169 |      5 |   166 | Barracuda   | Compressed |          8192 | General    |
|      174 | USER01/T_USER03    |    1 |      5 |   167 | Antelope    | Compact    |             0 | Single     |
+----------+--------------------+------+--------+-------+-------------+------------+---------------+------------+
4 rows in set (0.00 sec)

root@localhost [USER01]> ALTER TABLE T_USER03 TABLESPACE U_TABLESPACE01;
Query OK, 0 rows affected (0.03 sec)
Records: 0  Duplicates: 0  Warnings: 0

root@localhost [USER01]> SELECT * FROM information_schema.INNODB_SYS_TABLES where NAME LIKE 'USER%';
+----------+--------------------+------+--------+-------+-------------+------------+---------------+------------+
| TABLE_ID | NAME               | FLAG | N_COLS | SPACE | FILE_FORMAT | ROW_FORMAT | ZIP_PAGE_SIZE | SPACE_TYPE |
+----------+--------------------+------+--------+-------+-------------+------------+---------------+------------+
|      171 | USER01/T_USER01    |  129 |      5 |   165 | Antelope    | Compact    |             0 | General    |
|      173 | USER01/T_USER02    |  129 |      5 |   165 | Antelope    | Compact    |             0 | General    |
|      172 | USER01/T_USER02_8K |  169 |      5 |   166 | Barracuda   | Compressed |          8192 | General    |
|      175 | USER01/T_USER03    |  129 |      5 |   165 | Antelope    | Compact    |             0 | General    |
+----------+--------------------+------+--------+-------+-------------+------------+---------------+------------+
4 rows in set (0.00 sec)

root@localhost [USER01]> 

tablespace_alter

メモ:
5.7でサポートされる以下のページサイズも対応していますが、圧縮機能はサポートされていません。
64K 64K (65536) Compression is not supported
32K 32K (32768) Compression is not supported

Tablespace Row Formatのサポートについて
General tablespaces support all table row formats (REDUNDANT, COMPACT, DYNAMIC, COMPRESSED)
with the caveat that compressed and uncompressed tables cannot exist in the same general tablespace due to different physical page sizes.

Comments are closed.

Post Navigation