2
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

MySQLのストレージエンジンについて整理する

Last updated at Posted at 2022-06-16

はじめに

  • MySQLのストレージエンジンには様々な種類がある
  • それぞれのストレージエンジンには得手/不手がある
  • そのため、動作している環境のユースケースに適したストレージエンジンを選択することで、意図した領域でのパフォーマンスを発揮することができる

前準備

# 確認用にDBを作成
mysql> create database test_database;
Query OK, 1 row affected (0.02 sec)

# DBが作成されたことを確認
mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| sys                |
| test               |
| test_database      |
+--------------------+
6 rows in set (0.01 sec)

# 使用するDBの変更
mysql> use test_database;
Database changed

# 確認用にテーブルを作成
mysql> create table test_table (id int, name varchar(10));
Query OK, 0 rows affected (0.05 sec)

# テーブルが作成されたことを確認
mysql> show tables;
+-------------------------+
| Tables_in_test_database |
+-------------------------+
| test_table              |
+-------------------------+
1 row in set (0.01 sec)

ストレージエンジンの確認

# ストレージエンジンの一覧
# InnoDBがデフォルトで使用されていることが分かる
mysql> show engines;
+--------------------+---------+----------------------------------------------------------------+--------------+------+------------+
| Engine             | Support | Comment                                                        | Transactions | XA   | Savepoints |
+--------------------+---------+----------------------------------------------------------------+--------------+------+------------+
| InnoDB             | DEFAULT | Supports transactions, row-level locking, and foreign keys     | YES          | YES  | YES        |
| MRG_MYISAM         | YES     | Collection of identical MyISAM tables                          | NO           | NO   | NO         |
| MEMORY             | YES     | Hash based, stored in memory, useful for temporary tables      | NO           | NO   | NO         |
| BLACKHOLE          | YES     | /dev/null storage engine (anything you write to it disappears) | NO           | NO   | NO         |
| MyISAM             | YES     | MyISAM storage engine                                          | NO           | NO   | NO         |
| CSV                | YES     | CSV storage engine                                             | NO           | NO   | NO         |
| ARCHIVE            | YES     | Archive storage engine                                         | NO           | NO   | NO         |
| PERFORMANCE_SCHEMA | YES     | Performance Schema                                             | NO           | NO   | NO         |
| FEDERATED          | NO      | Federated MySQL storage engine                                 | NULL         | NULL | NULL       |
+--------------------+---------+----------------------------------------------------------------+--------------+------+------------+
9 rows in set (0.00 sec)

# 作成したテーブルのステータスを確認
# ストレージエンジンがInnoDBになっていることが確認できる(2カラム目)
mysql> show table status;
+------------+--------+---------+------------+------+----------------+-------------+-----------------+--------------+-----------+----------------+---------------------+-------------+------------+--------------------+----------+----------------+---------+
| Name       | Engine | Version | Row_format | Rows | Avg_row_length | Data_length | Max_data_length | Index_length | Data_free | Auto_increment | Create_time         | Update_time | Check_time | Collation          | Checksum | Create_options | Comment |
+------------+--------+---------+------------+------+----------------+-------------+-----------------+--------------+-----------+----------------+---------------------+-------------+------------+--------------------+----------+----------------+---------+
| test_table | InnoDB |      10 | Dynamic    |    0 |              0 |       16384 |               0 |            0 |         0 |           NULL | 2022-06-16 15:53:18 | NULL        | NULL       | utf8mb4_unicode_ci |     NULL |                |         |
+------------+--------+---------+------------+------+----------------+-------------+-----------------+--------------+-----------+----------------+---------------------+-------------+------------+--------------------+----------+----------------+---------+
1 row in set (0.00 sec)

ストレージエンジンの変更

# ストレージエンジンの変更
# InnoDB -> MyISAM
mysql> alter table test_table engine=MyISAM;
Query OK, 0 rows affected (0.12 sec)
Records: 0  Duplicates: 0  Warnings: 0

# ストレージエンジンがInnoDBからMyISAMに変更されていることが分かる
mysql> show table status;
+------------+--------+---------+------------+------+----------------+-------------+-----------------+--------------+-----------+----------------+---------------------+---------------------+------------+--------------------+----------+----------------+---------+
| Name       | Engine | Version | Row_format | Rows | Avg_row_length | Data_length | Max_data_length | Index_length | Data_free | Auto_increment | Create_time         | Update_time         | Check_time | Collation          | Checksum | Create_options | Comment |
+------------+--------+---------+------------+------+----------------+-------------+-----------------+--------------+-----------+----------------+---------------------+---------------------+------------+--------------------+----------+----------------+---------+
| test_table | MyISAM |      10 | Dynamic    |    0 |              0 |           0 | 281474976710655 |         1024 |         0 |           NULL | 2022-06-16 16:14:55 | 2022-06-16 16:14:55 | NULL       | utf8mb4_unicode_ci |     NULL |                |         |
+------------+--------+---------+------------+------+----------------+-------------+-----------------+--------------+-----------+----------------+---------------------+---------------------+------------+--------------------+----------+----------------+---------+
1 row in set (0.03 sec)

ストレージエンジンの種類

InnoDB

  • MySQL 5.5以降のバージョンにおけるデフォルトのストレージエンジン
  • ロック機能やデータ保護機能、クラッシュ時の修復に優れている
  • フィンランドのInnobase社が開発した
  • PostgreSQLのストレージエンジンを参考に開発された
  • メモリ管理機能がInnoDB内部で完結し、OSのメモリ管理から切り離されている
  • InnoDBをフォークして作られたストレージエンジンとして、Percona社のXtraDB等が存在する

MRG_MYISAM

  • DBの分散に使用される
  • 同一のMyISAMのテーブルの集合を1つのテーブルとして扱うことができる

MEMORY

  • メモリ上にデータを保存する(ディスク上に書き込まない)ため、軽快に動作するが、OSがクラッシュした際はデータが失われる
  • XtraDB等、高性能なキャッシュ機構を持つストレージエンジンの登場により、あまり使用されなくなった

BLACKHOLE

  • 書き込まれたデータを受け入れるが、データを保存せずにそのまま破棄する
  • 書き込み処理自体は実施されたとしてログに残る
  • 検証用等に使用される

MyISAM

  • MySQL 5.5より前のバージョンにおけるデフォルトのストレージエンジン
  • ISAM(Indexed Sequential Access Method、索引付き順次アクセス方式)をベースに様々な拡張がなされている
  • ロック機能やデータ保護機能、クラッシュ時の修復が弱い代わりに軽快に動作する
  • キャッシュ管理機能が無く、OSにキャッシュ管理を委ねている
  • MyISAMから派生したストレージエンジンとしてAria(旧Maria)等が存在する

CSV

  • テーブルデータをCSV形式として扱うことができる

ARCHIVE

  • テーブルデータを圧縮してディスク上での利用容量を減らすことを目的としたストレージエンジン
  • 基本的にインデックスを使用できず、使用できるSQLの種類も制限されている(INSERTSELECTのみ可)

PERFORMANCE_SCHEMA

  • パフォーマンスモニタリングのためのストレージエンジン
  • クエリチューニングに活用できる

FEDERATED

  • リモートのテーブルに接続し、あたかもローカルでテーブルデータを持っているかのように振る舞う
2
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?