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

ItemValue
Storage unitPage, 16KB by default. Choose 4, 8, 16, 32, or 64KB with innodb_page_size (only when creating the instance)
Table storageClustered index. Rows are stored in the B+Tree leaves in primary key order
Without a primary keyThe first UNIQUE NOT NULL index, or if there is none, a hidden 6-byte row ID
What a secondary index points toThe primary key value
Table fileOne .ibd file per table (innodb_file_per_table, on by default)
Buffer poolinnodb_buffer_pool_size, 128MB by default
Row formatDYNAMIC (default since 5.7), COMPACT, REDUNDANT, COMPRESSED
Large value handlingOff-page storage
Maximum row sizeA little under half a page (about 8KB with 16KB pages). BLOB and TEXT are excluded because they go off-page
Concurrency controlMVCC. Old values are rebuilt from the undo log
Default isolation levelRepeatable Read. Next-key locks on range searches
LogsRedo log, undo log, doublewrite buffer

Hidden columns of a row

InnoDB adds some columns to every row of the clustered index.

ColumnSizeContent
DB_TRX_ID6 bytesID of the transaction that last changed this row
DB_ROLL_PTR7 bytesLocation of the undo log record that holds the previous value
DB_ROW_ID6 bytesRow 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