The InnoDB file format
How MySQL's storage engine lays a table out on disk, from the tablespace down to one column's bytes. Every example here is read from the sample tablespace, a real file written by MySQL 8.4.13.
Files: tablespaces
InnoDB keeps data in tablespaces. With innodb_file_per_table on, which is the default, each table gets its own, a file named <table>.ibd in the database's directory, holding the table and all its indexes. The system tablespace (ibdata1) holds the change buffer and, in older setups, tables too; mysql.ibd holds the data dictionary itself; undo logs have tablespaces of their own; and CREATE TABLESPACE makes a general tablespace that several tables can share. They all use the same page format.
To copy a table's file safely from a running server, use FLUSH TABLES t FOR EXPORT, copy t.ibd while that session stays open, then UNLOCK TABLES. Otherwise some changes may still be only in memory and in the redo log. (How export works.)
Pages
A tablespace is an array of pages, numbered from 0. Pages are 16 KB unless the instance was created with another innodb_page_size (4, 8, 32 or 64 KB), and every integer in them is big-endian. Every page starts with the same 38-byte FIL header and ends with an 8-byte trailer:
| Offset | Field | What it is for |
|---|---|---|
| 0 | Checksum | CRC-32C of the page (innodb_checksum_algorithm=crc32), checked when the page is read. |
| 4 | Page number | This page's own number: its offset in the file divided by the page size. |
| 8 | Previous page | On INDEX pages, the left sibling at the same level of the B+tree. |
| 12 | Next page | On INDEX pages, the right sibling at the same level of the B+tree. |
| 16 | LSN | The log sequence number of the last change to this page: how far into the redo log it is up to date. |
| 24 | Page type | What the page holds. |
| 26 | Flush LSN | Only used on page 0 of the system tablespace; zero elsewhere. |
| 34 | Space id | The tablespace this page belongs to. |
| size − 8 | Trailer | An old-style checksum and the low four bytes of the LSN again. If the two LSN copies differ, the page was only partly written (a "torn page"), which the doublewrite buffer exists to repair. |
The checksum is CRC-32C, computed over the header without its first four bytes and the LSN, and over the body without the trailer. The viewer checks it on every page. The page type at offset 24 says how to read the rest.
Extents, segments and the first pages
Space is handed out in extents of 1 MB: 64 pages of 16 KB (256 of 4 KB; 64 pages for 32 and 64 KB pages). Page 0, the space header (FSP_HDR), records the tablespace's size, its flags and lists of free extents, followed by an extent descriptor for each of the first 256 extents: which segment owns it, and two bits per page, one of them "free". After 256 extents another descriptor page (XDES) describes the next 256, and so on.
A segment is the set of pages one part of an index grows into. Every index has two: one for its leaf pages and one for the rest, so that a range scan over the leaves reads pages that sit close together. A new segment first takes single pages, up to 32, from shared fragment extents, and only then whole extents of its own: a tiny index does not cost a megabyte. Segments are described by inodes, kept on INODE pages (page 2 to start with); the root page of each index stores where its two inodes are.
A table's file starts the same way every time: page 0 is the space header, page 1 the change buffer bitmap, page 2 the inodes, page 3 the root of the SDI, page 4 the root of the clustered index and page 5 onwards the roots of the secondary indexes. Here are the first pages of the sample, tracks.ibd:
Allocated, never written 519Space header / extent descriptors 1Change buffer bitmap 1Segment inodes 1SDI (table definition) 1Clustered index, non-leaf 1Clustered index leaf (rows) 20Secondary index, non-leaf 2Secondary index leaf 12Off-page column (LOB) 18
Of its 576 pages, 519 have never been written: once the clustered index needed more than its 32 fragment pages, it reserved whole extents, and the file grew to hold them. That is why a small table's .ibd is mostly zeroes.
INDEX pages
Every index is a B+tree, and its pages, leaves and non-leaves alike, have the type INDEX. After the FIL header comes the 56-byte INDEX header:
Then come two system records, the infimum (smaller than any key) and the supremum (larger than any), and after them the user records, packed upwards from there in the order they were inserted. The records are not kept sorted on the page: each one points to the next in key order, so the list starts at the infimum and ends at the supremum. Free space is in the middle. At the bottom, growing back from the trailer, is the page directory: two-byte slots, each pointing to a record that "owns" the four to eight records before it, so a search binary-searches the slots and then walks a few steps along the list.
Pages at the same level are linked to their neighbours through the FIL header's previous and next fields, so a range scan moves from leaf to leaf without going back up the tree. Leaves are level 0; the root is the highest level and never moves, which is why its page number can be stored in the data dictionary.
Records
InnoDB has two record formats. COMPACT is used by the COMPACT, DYNAMIC (the default) and COMPRESSED row formats; REDUNDANT is the original. A record has an origin, the address that "next" pointers lead to. The data follows the origin; the header precedes it and is read backwards:
- The 5-byte header: four flag bits (deleted, minimum record of a non-leaf level, and two for instant ALTER), the number of records this one owns in the directory, the heap number (the record's slot in the page's heap, in insertion order), the record type (ordinary, node pointer, infimum, supremum) and the offset of the next record, relative to this one.
- The null bitmap: one bit per nullable column. A NULL takes no other space at all.
- A length for each variable-length column that is not NULL: one byte, or two when the column can be longer than 255 bytes and the value is longer than 127. The second byte's flag says the value is stored off the page.
The fields themselves are in index order. In the clustered index, a leaf record is the whole row: the primary key, then two hidden columns InnoDB adds to every row, DB_TRX_ID (6 bytes: the transaction that last changed it) and DB_ROLL_PTR (7 bytes: where in the undo log its previous version is), then every other column. A table with no primary key also gets a hidden 6-byte DB_ROW_ID to cluster on. Secondary index records hold the indexed columns and then the primary key; non-leaf records hold a key and a 4-byte child page number.
Here is a real row from page 7 of the sample, at offset 128; lyrics is NULL, so it takes no bytes, only a bit:
| Offset | Part | Bytes |
|---|---|---|
| 120 | length of composer: 9 | 09 |
| 121 | length of name: 11 | 0b |
| 122 | null bitmap (1 byte) | 08 |
| 123 | record header: flags 0, owns 0, heap no 2, ordinary, next 195 | 00 00 10 00 43 |
| 128 | id (int unsigned): 1 | 00 00 00 01 |
| 132 | DB_TRX_ID (6-byte system column): 1813 | 00 00 00 00 07 15 |
| 138 | DB_ROLL_PTR (7-byte system column): insert, rollback segment 2, undo page 264, offset 272 | 82 00 00 01 08 01 10 |
| 145 | album_id (smallint): 8 | 80 08 |
| 147 | name (varchar(200)): Night Sky 1 | 4e 69 67 68 74 20 53 6b 79 20 31 |
| 158 | composer (varchar(120)): M. Okafor | 4d 2e 20 4f 6b 61 66 6f 72 |
| 167 | genre (enum('rock','jazz','blues','classical','pop','metal')): jazz | 02 |
| 168 | ms (int): 127919 | 80 01 f3 af |
| 172 | price (decimal(5,2)): 1.29 | 80 01 1d |
| 175 | released (date): 1965-02-07 | 8f 5a 47 |
| 178 | added (datetime(3)): 2024-01-01 00:01:01.001 | 99 b2 42 00 41 00 0a |
| 185 | rating (tinyint): 1 | 81 |
| 186 | explicit (bit(1)): 0 | 00 |
REDUNDANT records have a 6-byte header and, instead of a null bitmap and lengths, the end offset of every field, NULL ones included. They take more room, but they can be read without knowing the table's columns.
How values are stored
InnoDB stores values so that comparing the bytes of two keys gives the same order as comparing the values, which is why signed numbers have their sign bit flipped.
| Type | Stored as |
|---|---|
| TINYINT … BIGINT | 1–8 bytes, big-endian. Signed values have the sign bit flipped, so that −1 (7f ff ff ff) sorts before 0 (80 00 00 00) when the bytes are compared. |
| FLOAT, DOUBLE | 4 or 8 bytes, IEEE 754, little-endian: the one place InnoDB keeps the machine's own byte order. |
| DECIMAL(p,s) | Groups of nine digits in four bytes, with fewer bytes for the leftover digits on each side. The first bit is flipped and a negative number has every byte inverted, so this too sorts by its bytes. |
| DATE | 3 bytes: year × 512 + month × 32 + day, with the top bit flipped. |
| DATETIME(n) | 5 bytes of packed fields (year and month together, then day, hour, minute, second), plus 0–3 bytes of fractional seconds. |
| TIMESTAMP(n) | 4 bytes of seconds since 1970 (UTC), plus 0–3 bytes of fraction. |
| TIME(n) | 3 bytes of packed hours, minutes and seconds, offset so that negative times sort first, plus 0–3 bytes of fraction. |
| CHAR(n) | Padded with spaces to its full width; but in a multi-byte character set (utf8mb4) it is variable-length in COMPACT and DYNAMIC rows, at least n bytes. |
| VARCHAR, VARBINARY | Just the bytes; the length is in the record header. |
| TEXT, BLOB, JSON | Like VARCHAR when short; when the row is too big, stored on other pages behind a 20-byte pointer. JSON is in MySQL's binary JSON format, not text. |
| ENUM, SET | The 1-based member number in 1 or 2 bytes; for SET, a bitmask of 1–8 bytes. |
Off-page columns
A record must fit in about half a page, so that every page holds at least two. When a row is too big, the longest TEXT, BLOB, JSON or long VARCHAR values move off the page, longest first, until it fits. In DYNAMIC rows such a value leaves only a 20-byte pointer behind (space id, page number, version and length); COMPACT and REDUNDANT rows keep the first 768 bytes in the record, then the pointer.
Since MySQL 8.0 the value goes to a LOB: a LOB_FIRST page with a header, an index of up to ten entries saying which page holds each chunk, and the first chunk; then LOB_DATA pages for the rest, and LOB_INDEX pages for more entries when a value is very long. Because chunks are indexed, an update to part of a large JSON document rewrites only the chunks it touches. Off-page pages belong to the clustered index's leaf segment. The sample's long lyrics are stored this way; 18 of its pages are LOB pages.
The SDI: a file that describes itself
Since MySQL 8.0, each .ibd carries the Serialized Dictionary Information: the data dictionary's own description of the table (its columns, types, indexes and their root pages) and of the tablespace, as JSON compressed with zlib, in a B+tree of its own whose root is page 3. That is how this site can name every column without being told anything. MySQL ships a tool, ibd2sdi, that prints it. Files from MySQL 5.7 and earlier have no SDI; their definition is in a separate .frm file. In a COMPRESSED table the SDI pages are compressed too.
Instant ADD and DROP COLUMN
Since MySQL 8.0.29, ALTER TABLE … ADD COLUMN and DROP COLUMN can be instant: no row is rewritten. Instead the table gets a new row version. Records written afterwards set a flag in their header and store their version in one byte before the null bitmap. An older record keeps its old shape: it does not hold a column added later (reading it gives the default, stored in the SDI), and it still holds the bytes of a column dropped later, which the SDI keeps as a hidden column named like !hidden!_dropped_v2_p4_gone.
Deleted rows
A DELETE does not remove a record at once. It sets the record's deleted flag, because other transactions may still need to see the row (MVCC). Later the purge thread unlinks it from the list and puts it on the page's free list, adding its size to the page's garbage count. Its bytes are not cleared: they stay on the page, readable, until a new record reuses the space or the page is reorganised. The viewer shows them under each page. That is also why "deleted" data can sometimes be recovered from an .ibd file.