The labs on this site need JavaScript to run. The same content is written out below.
Learn › Database › Storage and recovery
Why does reading one row read a whole 16KB page?
Why it exists — If every read and change of a row went directly to the disk, a frequently used row would have to be read from the same place again and again, and every change would have to be written straight to that place in the data file. InnoDB's buffer pool takes this repeated disk work into one place in memory, so that the more often data is used, the fewer trips to the disk it needs.
Where it's used today — The most direct use is setting innodb_buffer_pool_size when you configure a MySQL server. On a server that runs only the database, a large share of the memory is given to the buffer pool. If frequently used data cannot all stay in the buffer pool, even the same query reads the disk more and becomes slower, so when you look for the cause of a slow query, also check how many times data was read from disk.
InnoDB reads and writes data on disk in blocks called pages (16KB by default), not row by row. A page that has been read is kept in memory in the buffer pool, and later reads come from memory instead of going to disk. When a row is changed, only the page in memory is changed, and it is written to disk later.
Checked against real execution · All fourteen combinations were run on MySQL 8.4.11 in a dedicated container (128MB buffer pool). One page on the screen was scaled up to a group of real pages and innodb_old_blocks_time was set to 0; the six combinations with no eviction were also run with the SQL on the screen as it is
-- item table: 16 rows. One row is about 3.5KB, so 4 rows fit in one 16KB page -- Page A: rows 1-4 Page B: rows 5-8 -- Page C: rows 9-12 Page D: rows 13-16 -- At the start, the buffer pool holds no pages of this table SELECT name FROM item WHERE id = 1; START TRANSACTION; UPDATE item SET name = 'changed' WHERE id = 1; COMMIT; SELECT name FROM item WHERE id = 2; SELECT name FROM item WHERE id = 1;
This is a model drawn at a reduced size, with a buffer pool of only 2 to 4 slots and the simple rule "evict the page that has gone unused the longest." A real buffer pool cannot be made smaller than 5MB (320 pages), so there is no buffer pool with 3 slots. So I ran it in two ways on MySQL 8.4.11 (a dedicated container; the buffer pool has the default size of 128MB, which is 8192 pages of 16KB) and compared the results. First, the six combinations with no eviction were run with the SQL on the screen as it is. I created a 16-row table in which one row is about 3.5KB, confirmed that each leaf page holds 4 rows, restarted the server to empty the buffer pool, and ran the queries in the same order as on the screen. Reads of the data file of that table increased only for a page that was read for the first time (3 reads for the first query, including the upper pages of the index, and 1 read for each new page after that), and there were 0 reads when another row or the same row on a page already loaded was read. Right after UPDATE and COMMIT, there were 0 file reads and 0 file writes and there was 1 dirty page, and the data file was written by itself within 2 to 5 seconds. Second, all fourteen combinations were run with the sizes scaled up by the same ratio. One page on the screen became a group of K real leaf pages (the first part of one table), one slot became room for K pages, and K was set a little smaller than the number of pages in the buffer pool divided by the number of slots (3,660 pages for 2 slots, 2,473 for 3 slots, and 1,857 for 4 slots). One query on the screen became a SELECT that reads that group to the end, and the change to row 1 became an UPDATE of row 1 in the first group followed by COMMIT. For each query I measured the number of pages read from the file and the number of pages of each group left in the buffer pool, and the number of disk reads, the queries that did not go to disk, and the evicted groups and their order matched the screen in all fourteen combinations. In the three combinations in which the changed group is evicted, the changed page never left the buffer pool without being written. Either it left after a background flush had written it, or that one page stayed in the buffer pool even after the other pages of the same group were evicted and it was written after the queries finished (over several runs, which of the two happened differed from combination to combination). A write at the very moment of eviction (what the step on the screen shows) could not be singled out in this run. In one run, a measurement right after UPDATE and COMMIT showed that a background flush had already written the page within those few milliseconds; such a run is not the situation on the screen, so it is run again. Two server settings were changed for this run: innodb_old_blocks_time was set to 0 (the default is 1000) and innodb_lru_scan_depth to 100 (the default is 1024). Real InnoDB divides the list into a new sublist and an old sublist, inserts a newly read page between them (by default at the point 3/8 from the tail), and moves it to the new sublist only if it is read again after innodb_old_blocks_time has passed since it was first read (MySQL 8.4 Reference Manual, 17.5.1 Buffer Pool). With this value set to 0, a page moves to the head of the list every time it is read, which is the same as the simple rule on the screen. When the same sequences were run without a pause with the default value, none of the twelve combinations matched the screen. A group that had just been read stayed only in the old sublist and was evicted as soon as the next group was read, and a group that was read again was also read from disk (for example, with 3 slots and the order 1 → 5 → 9 → 13 → 1, the screen evicts A and B, but with the default value A, B, C, and D were evicted one after another). So the order on the screen is the order of InnoDB with this safeguard turned off. innodb_lru_scan_depth is the number of free pages that the page cleaner keeps available in advance; with the default value, 1/8 of the pool is always free and the boundaries between groups become blurred, so it was set to the smallest value. Also, a real query reads the upper pages of the index in addition to the page that holds the row and sometimes reads the following pages ahead, so a few dozen more pages than K were read when one group was read.
Previous lab: Undo and redo logs · Next lab: Replication lag and CAP
Struck-through text was removed from the first version of the code; highlighted text was added.