skip to content

In MySQL's InnoDB, what does a table's ROW_FORMAT control, and why does the DYNAMIC default matter for tables with long columns?

level: middleimportance: should knowfreq 30%

answer

  1. four formats, one default
  2. what stays in the B-tree page
  3. 768-byte prefix versus 20-byte pointer
  4. 767 or 3072-byte index prefixes
  5. Row size too large (> 8126)

basics

~20 s

ROW_FORMAT sets how InnoDB lays rows out in pages. DYNAMIC, the default, moves long variable-length values wholly off-page behind a 20-byte pointer and allows 3072-byte index prefixes; COMPACT and REDUNDANT keep 768-byte prefixes in-page and cap prefixes at 767 bytes.

solid answer

~50 s

InnoDB supports four row formats: `REDUNDANT`, `COMPACT`, `DYNAMIC` and `COMPRESSED`, and `innodb_default_row_format` is `DYNAMIC`. The format decides how a row is encoded and, above all, what happens to long `VARCHAR`, `VARBINARY`, `TEXT` and `BLOB` values. `COMPACT` and `REDUNDANT` keep the first 768 bytes in the clustered index record and push the rest to overflow pages, so a table with many long columns fills its B-tree pages with data and holds few rows per page. `DYNAMIC` stores a long value entirely off-page with only a 20-byte pointer in the record, and allows index key prefixes up to 3072 bytes instead of 767. `COMPRESSED` adds page compression on top and needs a file-per-table or general tablespace. Rows must still fit in under half a page, about 8 KB with 16 KB pages, or `CREATE TABLE` fails with error 1118.

code

sql · 5 lines
sql
SELECT NAME, ROW_FORMAT
FROM INFORMATION_SCHEMA.INNODB_TABLES
WHERE ROW_FORMAT IN ('Compact', 'Redundant');

ALTER TABLE shop.article ROW_FORMAT=DYNAMIC;

go deeper

for a junior

Recall that InnoDB has row formats, that DYNAMIC is the default, and that very wide rows can fail with a Row size too large error.

for a middle

Explain the off-page rules: 768-byte prefixes for COMPACT and REDUNDANT, a 20-byte pointer for DYNAMIC, and the 767 versus 3072-byte index prefix limits.

for a senior

Diagnose format surprises in production: tables silently converted by a rebuild, replicas or imports failing on mismatched defaults, and error 1118 on wide CHAR tables.

for a principal

Weigh whether COMPRESSED earns its CPU and buffer-pool cost on a large read-mostly table, and whether to pin ROW_FORMAT explicitly across a fleet.

## What a row format is InnoDB stores each table as a B-tree of fixed-size pages, 16 KB by default (`innodb_page_size`, fixed when the instance is initialised). The **row format** is the on-page encoding of each record: its header, how NULLs are marked, how fixed and variable-length columns are laid out and, most importantly, how **long variable-length values** are handled. You set it per table with the `ROW_FORMAT` table option; without one, `innodb_default_row_format` applies, and its default is `DYNAMIC`. Long values that do not fit go to **overflow pages**, singly linked lists of separately allocated pages, and the column is then called **off-page**. How much of such a value stays in the B-tree record is the main difference between formats. ## The four formats | Format | Long-value handling | Max index key prefix | Notes | |---|---|---|---| | `REDUNDANT` | First 768 bytes in-page, rest off-page | 767 bytes | Oldest format, kept for compatibility | | `COMPACT` | First 768 bytes in-page, rest off-page | 767 bytes | About 20% smaller rows than `REDUNDANT` | | `DYNAMIC` | Whole value off-page, 20-byte pointer in-page | 3072 bytes | The default; `TEXT`/`BLOB` of 40 bytes or less stay inline | | `COMPRESSED` | Like `DYNAMIC`, plus compressed pages | 3072 bytes | Needs file-per-table or general tablespace; `KEY_BLOCK_SIZE` controls page size | `COMPACT` uses a 5-byte record header, a NULL bit vector and length bytes for variable columns, saving about a fifth of the space of `REDUNDANT` at some CPU cost. `DYNAMIC` keeps that layout but changes the long-value rule: InnoDB moves the longest columns off-page until the record fits, and moves each such value *entirely*, on the reasoning that once part of a value is off-page it is cheaper to fetch the whole of it from there. Short columns stay in the B-tree node, so the clustered index keeps many rows per page. ## Why the default matters 1. **Rows per page.** Under `COMPACT`, a table with several long text columns keeps 768 bytes of each in the clustered record. Pages fill with data rather than keys, fewer rows fit per page, and range scans read more pages. `DYNAMIC` avoids that. 2. **Index prefix length.** With `utf8mb4` a character can take 4 bytes, so a 767-byte limit allows an index on only the first 191 characters of a column. `DYNAMIC` and `COMPRESSED` allow 3072 bytes. 3. **Silent changes on rebuild.** A table created without an explicit `ROW_FORMAT` follows the variable. An `ALTER TABLE` that rebuilds the table, or `OPTIMIZE TABLE`, silently converts it to whatever `innodb_default_row_format` says at that moment. 4. **Replication and import mismatches.** If the source runs `DYNAMIC` and a replica `COMPACT`, a statement creating a long index prefix succeeds on the source and fails on the replica; importing a tablespace without an explicit format fails on a schema mismatch when the two servers' defaults differ. ## The row size limits Two limits apply, and they produce different errors: - **The server's 65,535-byte row limit** counts every column's maximum size, except that each `BLOB` or `TEXT` column contributes only 9 to 12 bytes because its content is stored separately. - **InnoDB's in-page limit** is slightly less than half a page: just under 8 KB for 16 KB pages. When even after moving columns off-page a record cannot fit, `CREATE TABLE` fails with `ERROR 1118 (42000): Row size too large (> 8126)`, often seen on a table of many wide `CHAR` columns. The fix is to turn some of them into `TEXT` or `BLOB` or narrow them. ## Choosing and checking - Leave new tables on `DYNAMIC` unless you have measured a reason to compress. - Use `COMPRESSED` only for large, read-mostly tables where disk saving outweighs the CPU and buffer-pool cost of keeping compressed and uncompressed page copies; it cannot be set as `innodb_default_row_format` and is not supported in the system tablespace. - Check a table with `SHOW TABLE STATUS` (`Row_format`) or `INFORMATION_SCHEMA.INNODB_TABLES` (`ROW_FORMAT`). - Old tables still on `COMPACT` or `REDUNDANT`, for example from a long chain of upgrades, convert with `ALTER TABLE t ROW_FORMAT=DYNAMIC`, which rebuilds the table. The general idea of slotted pages and overflow storage is shared by most relational engines; what is InnoDB's own is the four formats, their 768-byte and 20-byte rules, the 767 and 3072-byte prefix limits and the half-page row ceiling.

  • Why can a table created without an explicit ROW_FORMAT change format after an ALTER TABLE?
    Such a table follows `innodb_default_row_format`. Any operation that rebuilds the table, an `ALTER TABLE` using `ALGORITHM=COPY` or a rebuilding `INPLACE` change, or `OPTIMIZE TABLE`, re-creates it with the variable's current value. If someone changed the global default from `DYNAMIC` to `COMPACT`, the next rebuild silently converts the table. Declaring `ROW_FORMAT` explicitly pins it.
  • Can you set innodb_default_row_format to COMPRESSED?
    No. `COMPRESSED` is not supported in the system tablespace, so it cannot be the default; `SET GLOBAL innodb_default_row_format=COMPRESSED` fails with error 1231. It must be named explicitly in `CREATE TABLE` or `ALTER TABLE`, for a table in a file-per-table or general tablespace.

  • MySQL 8.4 creates InnoDB tables as COMPACT unless told otherwise.
  • DYNAMIC keeps the first 768 bytes of every long column in the page.
  • TEXT and BLOB columns count their full length toward the 65,535-byte row limit.
  • COMPRESSED can be set as innodb_default_row_format for every new table.
  • Index key prefixes are capped at 767 bytes in every InnoDB table.