MySQL (InnoDB)
MySQL separates the SQL processing layer from the storage engine that actually stores the data, and InnoDB has been the default storage engine since 5.5. It supports transactions, row locks, crash recovery, and foreign keys. In the articles on this site, "MySQL" means MySQL with the InnoDB engine. MariaDB also uses InnoDB, but its details and defaults can differ. The server's threads and memory areas are covered in Memory and thread structure, and connections, prepared statements, and cursors in Connections, cursors, and SQL cache.
Key specs
| Item | Value |
|---|---|
| Storage unit | Page, 16KB by default. Choose 4, 8, 16, 32, or 64KB with innodb_page_size (only when creating the instance) |
| Table storage | Clustered index. Rows are stored in the B+Tree leaves in primary key order |
| Without a primary key | The first UNIQUE NOT NULL index, or if there is none, a hidden 6-byte row ID |
| What a secondary index points to | The primary key value |
| Table file | One .ibd file per table (innodb_file_per_table, on by default) |
| Buffer pool | innodb_buffer_pool_size, 128MB by default |
| Row format | DYNAMIC (default since 5.7), COMPACT, REDUNDANT, COMPRESSED |
| Large value handling | Off-page storage |
| Maximum row size | A little under half a page (about 8KB with 16KB pages). BLOB and TEXT are excluded because they go off-page |
| Concurrency control | MVCC. Old values are rebuilt from the undo log |
| Default isolation level | Repeatable Read. Next-key locks on range searches |
| Logs | Redo log, undo log, doublewrite buffer |
Hidden columns of a row
InnoDB adds some columns to every row of the clustered index.
| Column | Size | Content |
|---|---|---|
DB_TRX_ID | 6 bytes | ID of the transaction that last changed this row |
DB_ROLL_PTR | 7 bytes | Location of the undo log record that holds the previous value |
DB_ROW_ID | 6 bytes | Row ID, used only when there is no primary key |
When another transaction needs to read the row as of an earlier point in time, InnoDB follows DB_ROLL_PTR back through the undo log and rebuilds the value at that point. If a long-open transaction exists, purge cannot delete undo, and the undo tablespace grows.
Effects of the clustered index
- A lookup by primary key walks down the index and reaches the row directly. A lookup by secondary index gets the primary key value from the secondary index and then traverses the clustered index once more.
- Every secondary index contains the primary key value, so a long primary key makes every secondary index larger.
- If the primary key is random (such as a UUID), inserts scatter across the leaves and page splits are frequent. With an increasing value, inserts only append at the right end.
Settings and checks
SELECT VERSION();
SHOW VARIABLES LIKE 'innodb_page_size';
SHOW VARIABLES LIKE 'innodb_default_row_format';
SHOW TABLE STATUS LIKE 'orders'; -- Row_format, Data_length, Index_length
SHOW ENGINE INNODB STATUS; -- buffer pool, locks, transactions, redo state
Comparison with other DBMSs
- PostgreSQL and Oracle use heap tables by default, and secondary indexes point to the row address. SQL Server lets you choose between a heap and a clustered index.
- DDL commits automatically after each statement and cannot be rolled back. Since 8.0 the data dictionary is stored in InnoDB tables, so the DDL itself is processed atomically.
In the articles
NOTE Defaults can vary by version. This document is written for MySQL 8.0 and later.
Related pages
- MySQL memory and thread structure
- MySQL Connections, Cursors, and SQL Cache
- DBMS Architecture at a Glance
- InnoDB Buffer Pool
- InnoDB Off-Page Storage
- InnoDB redo log
- InnoDB Undo Log
- InnoDB doublewrite buffer
- innodb_flush_log_at_trx_commit
- InnoDB Gap Locks and Next-Key Locks
- Operating system cache and direct I/O
- PostgreSQL
- Oracle Database
- SQL Server / Azure SQL