00:00

MySQL Storage Engines

Last updated:

A storage engine is the part of MySQL that actually stores, reads and writes table data. MySQL's design is unusual: engines are pluggable, and each table can use a different one. The engine decides whether a table supports transactions, how it locks rows, how it survives a crash and where the data lives.

1. What is a Storage Engine?

Pluggable storage engine architectureEvery table goes through the same SQL layer, but each table can use a different storage engine: InnoDB stores data in .ibd files with a redo log, MyISAM in .MYD and .MYI files, MEMORY in RAM only, and CSV in a text file.SQL layer (same for every table)parser · optimizer · executor · cachesstorage engine API (handler interface)InnoDBdefault · ACID.ibd + redo logMyISAMno transactions.MYD + .MYIMEMORYRAM onlylost on restartCSVplain text.CSV file
Figure 1: the SQL layer talks to every engine through the same interface, so each table can choose its own engine.

When you run SELECT or UPDATE, the SQL layer works out what to do and then asks the table's engine to fetch or change the rows. Since MySQL 5.5 the default engine is InnoDB, and for almost every application it is the right choice.

2. Listing the Available Engines

SHOW ENGINES lists the engines compiled into your server and marks the default:

query.sql
SHOW ENGINES;

Result: 11 rows (Comment column omitted)

EngineSupportTransactionsXASavepoints
ndbclusterNONULLNULLNULL
CSVYESNONONO
ARCHIVEYESNONONO
BLACKHOLEYESNONONO
ndbinfoNONULLNULLNULL
MRG_MYISAMYESNONONO
FEDERATEDNONULLNULLNULL
MyISAMYESNONONO
PERFORMANCE_SCHEMAYESNONONO
InnoDBDEFAULTYESYESYES
MEMORYYESNONONO
Support = DEFAULT marks the engine used when CREATE TABLE doesn't name one. NO means the server was built without the engine, so it cannot be turned on; DISABLED means it is present but switched off.

3. InnoDB: the Default Engine

  • ACID transactions with COMMIT, ROLLBACK and savepoints.
  • Row-level locking and MVCC: readers don't block writers, and two sessions can update different rows of the same table at once.
  • Foreign keys with cascading actions.
  • Crash recovery via the redo log (write-ahead logging) and doublewrite buffer.
  • Clustered primary key: rows are stored in primary-key order, so lookups by primary key are very fast.
  • Buffer pool: caches data and index pages in memory.
  • Full-text and spatial indexes, compression and encryption at rest.

4. MyISAM: the Older Engine

MyISAM was the default before MySQL 5.5. It is simple and compact but has serious limits: no transactions, no foreign keys, table-level locks and weak crash safety. Today it is kept mainly for old applications.

5. Seeing the Difference: Transactions

Create the same table twice, once with each engine, and try to roll back an insert:

rollback-test.sql
CREATE DATABASE enginedemo;
USE enginedemo;
CREATE TABLE pay_innodb (id INT PRIMARY KEY, amount INT) ENGINE = InnoDB;
CREATE TABLE pay_myisam (id INT PRIMARY KEY, amount INT) ENGINE = MyISAM;

START TRANSACTION;
INSERT INTO pay_innodb VALUES (1, 500);
INSERT INTO pay_myisam VALUES (1, 500);
ROLLBACK;
SHOW WARNINGS;
Output
Query OK, 1 row affected
Query OK, 0 rows affected
Query OK, 0 rows affected
Query OK, 0 rows affected
Query OK, 0 rows affected
Query OK, 1 row affected
Query OK, 1 row affected, 1 warning
Query OK, 0 rows affected, 1 warning

Result: 1 row

LevelCodeMessage
Warning1196Some non-transactional changed tables couldn't be rolled back

The MyISAM insert already raised a warning that mixing engines in one transaction is deprecated, and SHOW WARNINGS after the ROLLBACK confirms that part of the transaction could not be undone. Count the rows:

query.sql
SELECT (SELECT COUNT(*) FROM pay_innodb) AS innodb_rows,
       (SELECT COUNT(*) FROM pay_myisam) AS myisam_rows;

Result: 1 row

innodb_rowsmyisam_rows
01

The InnoDB insert was undone; the MyISAM insert stayed. In a payment system that half-finished change could mean money recorded twice.

6. Seeing the Difference: Foreign Keys

MyISAM silently ignores a foreign key definition, so it cannot protect relationships:

myisam-fk.sql
CREATE TABLE dept (id INT PRIMARY KEY) ENGINE = MyISAM;
CREATE TABLE emp  (id INT PRIMARY KEY, dept_id INT,
                   FOREIGN KEY (dept_id) REFERENCES dept(id)) ENGINE = MyISAM;
INSERT INTO emp VALUES (1, 99);   -- department 99 doesn't exist, but MyISAM accepts it
SELECT * FROM emp;

Result: 1 row

iddept_id
199

With InnoDB the same insert is refused:

innodb-fk.sql
CREATE TABLE dept2 (id INT PRIMARY KEY) ENGINE = InnoDB;
CREATE TABLE emp2  (id INT PRIMARY KEY, dept_id INT,
                    FOREIGN KEY (dept_id) REFERENCES dept2(id)) ENGINE = InnoDB;
INSERT INTO emp2 VALUES (1, 99);
Output
Query OK, 0 rows affected
Query OK, 0 rows affected
ERROR 1452 (23000): Cannot add or update a child row: a foreign key constraint fails (`enginedemo`.`emp2`, CONSTRAINT `emp2_ibfk_1` FOREIGN KEY (`dept_id`) REFERENCES `dept2` (`id`))

7. Other Engines

EngineHow it stores dataGood forWatch out
MEMORYEntirely in RAM; hash indexes by defaultSmall lookup tables, temporary resultsAll rows are lost on restart; no TEXT/BLOB; table locks
CSVPlain comma-separated text fileExchanging data with spreadsheetsNo indexes, columns must be NOT NULL
ARCHIVECompressed rows; INSERT and SELECT onlyLogs and audit history kept for yearsNo UPDATE/DELETE, no indexes except AUTO_INCREMENT
BLACKHOLEAccepts writes and stores nothingReplication relays, testingSELECT always returns no rows
MERGE (MRG_MyISAM)A view over identical MyISAM tablesLegacy log tablesMyISAM's limits apply
FEDERATEDRows live on a remote MySQL serverRare cross-server accessDisabled by default; slow
NDBMySQL NDB Cluster, distributed in memoryTelecom-grade high availabilitySeparate product (MySQL Cluster)
other-engines.sql
CREATE TABLE visitors_now (session_id CHAR(8) PRIMARY KEY, page VARCHAR(40)) ENGINE = MEMORY;
INSERT INTO visitors_now VALUES ('a1b2c3d4', '/home'), ('e5f6g7h8', '/cart');

CREATE TABLE audit_log (id INT AUTO_INCREMENT PRIMARY KEY, msg VARCHAR(100)) ENGINE = ARCHIVE;
INSERT INTO audit_log (msg) VALUES ('user 7 logged in');
UPDATE audit_log SET msg = 'changed' WHERE id = 1;   -- ARCHIVE is append-only
Output
Query OK, 0 rows affected
Query OK, 2 rows affected
Records: 2  Duplicates: 0  Warnings: 0
Query OK, 0 rows affected
Query OK, 1 row affected
ERROR 1031 (HY000): Table storage engine for 'audit_log' doesn't have this option

8. InnoDB vs MyISAM vs MEMORY

FeatureInnoDBMyISAMMEMORY
Transactions (COMMIT/ROLLBACK)YesNoNo
LockingRow-levelTable-levelTable-level
Foreign keysYesNo (ignored)No
Crash recoveryAutomaticMay need REPAIR TABLEData lost on restart
MVCC (consistent reads without locks)YesNoNo
Full-text indexesYesYesNo
Data cached inBuffer pool (data + indexes)Key cache (indexes only)RAM (everything)
Best forAlmost everythingLegacy read-mostly tablesSmall, temporary, rebuildable data

9. Checking and Changing a Table's Engine

See which engine each table uses:

query.sql
SELECT table_name, engine
FROM information_schema.tables
WHERE table_schema = 'enginedemo'
ORDER BY table_name;

Result: 8 rows

TABLE_NAMEENGINE
audit_logARCHIVE
deptMyISAM
dept2InnoDB
empMyISAM
emp2InnoDB
pay_innodbInnoDB
pay_myisamMyISAM
visitors_nowMEMORY

Convert a table with ALTER TABLE ... ENGINE. MySQL rebuilds the table, so on a large table schedule it for a quiet time:

convert.sql
ALTER TABLE pay_myisam ENGINE = InnoDB;
SELECT table_name, engine FROM information_schema.tables
WHERE table_schema = 'enginedemo' AND table_name = 'pay_myisam';

Result: 1 row

TABLE_NAMEENGINE
pay_myisamInnoDB

The server-wide default comes from the default_storage_engine variable:

query.sql
SELECT @@default_storage_engine AS default_engine;

Result: 1 row

default_engine
InnoDB

10. How to Choose

  • Use InnoDB unless you have a specific, measured reason not to. It is the only general-purpose engine with transactions and foreign keys.
  • Use MEMORY only for small data you can rebuild after a restart; for caching, a dedicated cache such as Redis is often better.
  • Use ARCHIVE for write-once history you rarely read.
  • Convert old MyISAM tables to InnoDB when you can; test first, especially if they use full-text search or COUNT(*) on huge tables.

11. Practice Exercises

Exercise 1: List all tables in the enginedemo database that do not use InnoDB.
solution.sql
SELECT table_name, engine
FROM information_schema.tables
WHERE table_schema = 'enginedemo' AND engine <> 'InnoDB';

Result: 4 rows

TABLE_NAMEENGINE
audit_logARCHIVE
deptMyISAM
empMyISAM
visitors_nowMEMORY
Exercise 2: Which engines on this server support transactions?
solution.sql
SELECT engine FROM information_schema.engines WHERE transactions = 'YES';

Result: 1 row

engine
InnoDB

12. Summary

  • A storage engine stores and retrieves table data; MySQL lets each table choose its engine.
  • InnoDB (default) gives transactions, row locks, foreign keys and crash recovery.
  • MyISAM lacks transactions and foreign keys and locks whole tables.
  • MEMORY, CSV, ARCHIVE and BLACKHOLE serve special cases.
  • Check engines with SHOW ENGINES or information_schema.tables; change one with ALTER TABLE ... ENGINE = ....

Reference: Alternative Storage Engines – MySQL Reference Manual
https://dev.mysql.com/doc/refman/9.7/en/storage-engines.html