MySQL Storage Engines
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?
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:
SHOW ENGINES;
Result: 11 rows (Comment column omitted)
| Engine | Support | Transactions | XA | Savepoints |
|---|---|---|---|---|
| ndbcluster | NO | NULL | NULL | NULL |
| CSV | YES | NO | NO | NO |
| ARCHIVE | YES | NO | NO | NO |
| BLACKHOLE | YES | NO | NO | NO |
| ndbinfo | NO | NULL | NULL | NULL |
| MRG_MYISAM | YES | NO | NO | NO |
| FEDERATED | NO | NULL | NULL | NULL |
| MyISAM | YES | NO | NO | NO |
| PERFORMANCE_SCHEMA | YES | NO | NO | NO |
| InnoDB | DEFAULT | YES | YES | YES |
| MEMORY | YES | NO | NO | NO |
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,ROLLBACKand 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:
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;
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
| Level | Code | Message |
|---|---|---|
| Warning | 1196 | Some 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:
SELECT (SELECT COUNT(*) FROM pay_innodb) AS innodb_rows,
(SELECT COUNT(*) FROM pay_myisam) AS myisam_rows;
Result: 1 row
| innodb_rows | myisam_rows |
|---|---|
| 0 | 1 |
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:
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
| id | dept_id |
|---|---|
| 1 | 99 |
With InnoDB the same insert is refused:
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);
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
| Engine | How it stores data | Good for | Watch out |
|---|---|---|---|
| MEMORY | Entirely in RAM; hash indexes by default | Small lookup tables, temporary results | All rows are lost on restart; no TEXT/BLOB; table locks |
| CSV | Plain comma-separated text file | Exchanging data with spreadsheets | No indexes, columns must be NOT NULL |
| ARCHIVE | Compressed rows; INSERT and SELECT only | Logs and audit history kept for years | No UPDATE/DELETE, no indexes except AUTO_INCREMENT |
| BLACKHOLE | Accepts writes and stores nothing | Replication relays, testing | SELECT always returns no rows |
| MERGE (MRG_MyISAM) | A view over identical MyISAM tables | Legacy log tables | MyISAM's limits apply |
| FEDERATED | Rows live on a remote MySQL server | Rare cross-server access | Disabled by default; slow |
| NDB | MySQL NDB Cluster, distributed in memory | Telecom-grade high availability | Separate product (MySQL Cluster) |
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
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
| Feature | InnoDB | MyISAM | MEMORY |
|---|---|---|---|
| Transactions (COMMIT/ROLLBACK) | Yes | No | No |
| Locking | Row-level | Table-level | Table-level |
| Foreign keys | Yes | No (ignored) | No |
| Crash recovery | Automatic | May need REPAIR TABLE | Data lost on restart |
| MVCC (consistent reads without locks) | Yes | No | No |
| Full-text indexes | Yes | Yes | No |
| Data cached in | Buffer pool (data + indexes) | Key cache (indexes only) | RAM (everything) |
| Best for | Almost everything | Legacy read-mostly tables | Small, temporary, rebuildable data |
9. Checking and Changing a Table's Engine
See which engine each table uses:
SELECT table_name, engine
FROM information_schema.tables
WHERE table_schema = 'enginedemo'
ORDER BY table_name;
Result: 8 rows
| TABLE_NAME | ENGINE |
|---|---|
| audit_log | ARCHIVE |
| dept | MyISAM |
| dept2 | InnoDB |
| emp | MyISAM |
| emp2 | InnoDB |
| pay_innodb | InnoDB |
| pay_myisam | MyISAM |
| visitors_now | MEMORY |
Convert a table with ALTER TABLE ... ENGINE. MySQL rebuilds the table, so on a large table schedule it for a quiet time:
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_NAME | ENGINE |
|---|---|
| pay_myisam | InnoDB |
The server-wide default comes from the default_storage_engine variable:
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.
SELECT table_name, engine
FROM information_schema.tables
WHERE table_schema = 'enginedemo' AND engine <> 'InnoDB';
Result: 4 rows
| TABLE_NAME | ENGINE |
|---|---|
| audit_log | ARCHIVE |
| dept | MyISAM |
| emp | MyISAM |
| visitors_now | MEMORY |
Exercise 2: Which engines on this server support transactions?
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 ENGINESorinformation_schema.tables; change one withALTER TABLE ... ENGINE = ....
Reference: Alternative Storage Engines – MySQL Reference Manual
https://dev.mysql.com/doc/refman/9.7/en/storage-engines.html