MySQL Storage Engines

Summary: in this tutorial, you will learn various MySQL storage engines. It is essential to understand the features of each storage engine in MySQL so that you can use them effectively to maximize the performance of your databases.

Introduction to MySQL Storage Engines

In MySQL, a storage engine is a software component responsible for managing how data is stored, retrieved, and manipulated within tables. A storage engine also determines the underlying structure and features of the tables.

MySQL supports multiple storage engines, each has its own set of features. To find the available storage engines on your MySQL server, you can use the following query:

SELECT 
  engine, 
  support 
FROM 
  information_schema.engines 
ORDER BY 
  engine;Code language: SQL (Structured Query Language) (sql)

Output:

+--------------------+---------+
| engine             | support |
+--------------------+---------+
| ARCHIVE            | YES     |
| BLACKHOLE          | YES     |
| CSV                | YES     |
| FEDERATED          | NO      |
| InnoDB             | DEFAULT |
| MEMORY             | YES     |
| MRG_MYISAM         | YES     |
| MyISAM             | YES     |
| ndbcluster         | NO      |
| ndbinfo            | NO      |
| PERFORMANCE_SCHEMA | YES     |
+--------------------+---------+
11 rows in set (0.02 sec)
Code language: plaintext (plaintext)

The query returns 11 storage engines in the engine column and whether it is supported or not in the support column.

If the support column is YES, it means that the corresponding storage engine is supported, or NO otherwise.

If the value in the support column is DEFAULT, which means that the storage engine is supported and used as the default.

Alternatively, you can use the SHOW ENGINES statement to list all available storage engines:

SHOW ENGINES;

Output:

+--------------------+---------+----------------------------------------------------------------+--------------+------+------------+
| Engine             | Support | Comment                                                        | Transactions | XA   | Savepoints |
+--------------------+---------+----------------------------------------------------------------+--------------+------+------------+
| MEMORY             | YES     | Hash based, stored in memory, useful for temporary tables      | NO           | NO   | NO         |
| MRG_MYISAM         | YES     | Collection of identical MyISAM tables                          | NO           | NO   | NO         |
| CSV                | YES     | CSV storage engine                                             | NO           | NO   | NO         |
| FEDERATED          | NO      | Federated MySQL storage engine                                 | NULL         | NULL | NULL       |
| PERFORMANCE_SCHEMA | YES     | Performance Schema                                             | NO           | NO   | NO         |
| MyISAM             | YES     | MyISAM storage engine                                          | NO           | NO   | NO         |
| InnoDB             | DEFAULT | Supports transactions, row-level locking, and foreign keys     | YES          | YES  | YES        |
| ndbinfo            | NO      | MySQL Cluster system information storage engine                | NULL         | NULL | NULL       |
| BLACKHOLE          | YES     | /dev/null storage engine (anything you write to it disappears) | NO           | NO   | NO         |
| ARCHIVE            | YES     | Archive storage engine                                         | NO           | NO   | NO         |
| ndbcluster         | NO      | Clustered, fault-tolerant tables                               | NULL         | NULL | NULL       |
+--------------------+---------+----------------------------------------------------------------+--------------+------+------------+
11 rows in set (0.00 sec)Code language: PHP (php)

Note that MySQL 5.5 or later uses InnoDB as the default storage engine.

To specify a storage engine when creating a new table, you use the ENGINE clause in the CREATE TABLE statement:

CREATE TABLE table_name(
    column_list
) ENGINE = engine_name;Code language: SQL (Structured Query Language) (sql)

If you omit the ENGINE clause, MySQL will use the default storage engine for creating the table.

MySQL storage engine features

The following table compares the features of the storage engine in MySQL:

FeatureMyISAMMEMORYCSVARCHIVEBLACKHOLEMERGEFEDERATEDInnoDB
TransactionalNoNoNoNoNoNoNoYes
ACID ComplianceNoNoNoNoNoNoNoYes
Table-level lockingYesNoYesYesYesNoYesYes
Full-text searchYesNoNoNoNoNoNoYes
Foreign key constraintsNoNoNoNoNoNoNoYes
Crash recoveryNoNoNoNoNoNoNoYes
External data accessNoNoYesYesNoNoYesNo
Temporary tablesYesYesNoNoNoNoNoYes
Default storage engineNoNoNoNoNoNoNoYes

InnoDB

The InnoDB tables fully support ACID-compliant transactions. They are also optimal for performance. InnoDB table supports foreign keys, commit, rollback, and roll-forward operations. The size of an InnoDB table can be up to 64TB.

Like MyISAM, the InnoDB tables are portable between different platforms and operating systems. MySQL also checks and repairs InnoDB tables, if necessary, at startup.

MyISAM

The MyISAM extends the former ISAM storage engine. The MyISAM tables are optimized for compression and speed. MyISAM tables are also portable between platforms and operating systems.

The size of the MyISAM table can be up to 256TB, which is huge. In addition, MyISAM tables can be compressed into read-only tables to save space. At startup, MySQL checks MyISAM tables for corruption and even repairs them in case of errors. The drawback of the MyISAM tables is that they are not transaction-safe.

Before version 5.5, MySQL used MyISAM as the default storage engine. From version 5.5, MySQL uses InnoDB as the default storage engine.

MERGE

A MERGE table is a virtual table that combines multiple MyISAM tables that have the same structure as one table. The MERGE storage engine is also known as the MRG_MyISAM engine. The MERGE tables do not have indexes. Instead, they use indexes of the component tables.

If you use DROP TABLE statement on a MERGE table, MySQL removes only the MERGE table and does not delete the underlying tables.

The MERGE tables allow you to speed up performance when joining multiple tables. MySQL only allows you to perform SELECT, DELETE, UPDATE, and INSERT operations on the MERGE tables.

Memory

The MEMORY tables store data entirely in memory and use hash indexes so that they are faster than MyISAM tables.

The lifetime of the data of the MEMORY tables depends on the uptime of the database server. The memory storage engine was formerly known as HEAP.

Archive

The ARCHIVE storage engine allows you to store a large number of records for archiving purposes in a compressed format to save disk space. The ARCHIVE storage engine compresses a record when it is inserted and decompresses as it is read.

The ARCHIVE tables only allow INSERT and SELECT statements. The ARCHIVE tables do not support indexes, so it is required a full table scanning for reading rows.

CSV

The CSV storage engine stores data in comma-separated values (CSV) file format. A CSV table brings a convenient way to migrate data into non-SQL applications such as spreadsheet software.

CSV table does not support NULL data type. In addition, the read operation requires a full table scan.

BLACKHOLE

The BLACKHOLE storage engine doesn’t store the table data. It means that the data that you send to a BLACKHOLE table is discarded.

Therefore, The BLACKHOLE tables can be useful in a replication scenario where you want to capture data changes on the master server without storing that data on the local server.

FEDERATED

The FEDERATED storage engine allows you to manage data from a remote MySQL server without using the cluster or replication technology.

The local federated table stores no data. When you query data from a local federated table, the data is pulled automatically from the remote federated tables.

Summary

  • A storage engine determines how MySQL stores, retrieves, and manipulates table data.
  • MySQL uses InnoDB as the default storage engine.
  • Use the ENGINE clause in the CREATE STATEMENT to explicitly instruct MySQL to use a specific storage engine other than the default storage engine.
  • Each storage engine has it is own pros and cons, therefore choosing the right storage engine is critical for your application.
Was this tutorial helpful?