Mysql innodb caches: Difference between revisions
No edit summary |
|||
(11 intermediate revisions by the same user not shown) | |||
Line 17: | Line 17: | ||
== Basics == | == Basics == | ||
<blockquote> | <blockquote> | ||
InnoDB is a database that operates purely in memory, with writes backed to disk.<br> | |||
Everytime something is read/written to the database, if the page containing the row is not loaded into the buffer pool | |||
it is read into memory, then it is operated on there. Nothing happens in InnoDB without being first loaded into memory. | |||
The cost of a query is based on how much memory must be loaded into memory with a cold cache. | |||
* Every modification records an entry in the transaction log (can be used in replication) | * Every modification records an entry in the transaction log (can be used in replication) | ||
Line 29: | Line 32: | ||
` | | ` | | ||
+----------------------------------------------------+ | | +----------------------------------------------------+ | | ||
| buffer pool | | buffer pool | | | ||
+----------------------------------------------------+ | | +----------------------------------------------------+ | | ||
| | . | | | | . | | ||
Line 37: | Line 40: | ||
| | | | | | | | | | ||
+------------+ | | | | +------------+ | | | | ||
| log buffer | | | | | | log buffer | | | | | ||
+------+-----+ | | | | +------+-----+ | | | | ||
| | | | | | | | | | ||
Line 44: | Line 47: | ||
| +------+------+ | | | | +------+------+ | | | ||
| |double-write | | | | | |double-write | | | | ||
| | buffer | | | buffer | | | | ||
\ / +-------------+ | | Disk | \ / +-------------+ | | Disk | ||
` | | | | ` | | | | ||
+------+------+ +-------------+ | | | +------+------+ +-------------+ | | | ||
| transaction | | datastore | | | | | transaction | | datastore | | | | ||
| log | | (filesystem)| | | | | log | | (filesystem) | | | | ||
+-------------+ +-------------+ | | | +-------------+ +-------------+ | | | ||
| | | | | | | | ||
Line 58: | Line 61: | ||
== Disabling == | == Disabling == | ||
<blockquote> | <blockquote> | ||
You can't disable the buffer-pool.<br> | |||
InnoDB operates entirely in memory, which means you cannot entirely clear the cache.<br> | |||
Depending on your configuration, restarting the server may purge the cache, but it is best to check. | |||
When timing your queries, be mindful of the ordering of the indexes.<br> | |||
A full-table scan may look fast if it only needs to select the first N rows from the database,<br> | |||
but much later rows will surface the slowdown. | |||
Even still, I have not had much success with recreating datasets with "cold-cache" queries<br> | |||
while using the minimum possible pool size. | |||
<syntaxhighlight lang="mysql"> | <syntaxhighlight lang="mysql"> | ||
# bytes per chunk | # bytes per chunk | ||
Line 74: | Line 89: | ||
== Introspection == | == Introspection == | ||
<blockquote> | <blockquote> | ||
queryable: | |||
<syntaxhighlight lang="mysql"> | <syntaxhighlight lang="mysql"> | ||
select count(*) from information_schema.innodb_buffer_page; # pages in buffer pool (memory) (16KB/page ?) | select count(*) from information_schema.innodb_buffer_page; # pages in buffer pool (memory) (16KB/page ?) | ||
SHOW INNODB_BUFFER_POOL_STATS; # general info | SHOW INNODB_BUFFER_POOL_STATS; # general info | ||
# memory allocation by item. | |||
SELECT * FROM innodb_buffer_stats_by_schema; | |||
SELECT * FROM innodb_buffer_stats_by_table; | |||
</syntaxhighlight> | </syntaxhighlight> | ||
There are also cli tools. | |||
{| class="wikitable" | |||
|- | |||
| [[innotop]] | |||
|- | |||
|} | |||
</blockquote><!-- Introspection --> | </blockquote><!-- Introspection --> | ||
</blockquote><!-- General --> | </blockquote><!-- General --> | ||
= Components = | = Components = | ||
Line 90: | Line 115: | ||
=== Adaptive Hash Index === | === Adaptive Hash Index === | ||
<blockquote> | <blockquote> | ||
The AdaptiveHashIndex, when enabled is designed to make innodb behave like an in-memory database | The AdaptiveHashIndex, when enabled is designed to make innodb behave like an in-memory database.<br> | ||
This is only one component of caching, and in my experience disabling it has not been sufficient to reproduce "cold cache" queries. | |||
<syntaxhighlight lang="mysql"> | <syntaxhighlight lang="mysql"> | ||
# interactive | # interactive | ||
Line 115: | Line 139: | ||
== Tablespace == | == Tablespace == | ||
<blockquote> | |||
=== Overview === | |||
<blockquote> | <blockquote> | ||
The tablespace is a virtual filesystem that manages the database-file reads/writes.<br> | The tablespace is a virtual filesystem that manages the database-file reads/writes.<br> | ||
Line 126: | Line 152: | ||
a ~4KB cache of table statistics | a ~4KB cache of table statistics | ||
</syntaxhighlight> | </syntaxhighlight> | ||
</blockquote><!-- Overview --> | |||
=== Double-Write Buffer === | === Double-Write Buffer === | ||
Line 149: | Line 175: | ||
</blockquote><!-- InnoDB: Data Dictionary --> | </blockquote><!-- InnoDB: Data Dictionary --> | ||
</blockquote><!-- Tablespace --> | </blockquote><!-- Tablespace --> | ||
== Transaction Log == | == Transaction Log == |
Latest revision as of 16:09, 24 September 2022
This page documents the various caches used by MySQLs innodb engine.
Documentation
General
Basics
InnoDB is a database that operates purely in memory, with writes backed to disk.
Everytime something is read/written to the database, if the page containing the row is not loaded into the buffer pool it is read into memory, then it is operated on there. Nothing happens in InnoDB without being first loaded into memory. The cost of a query is based on how much memory must be loaded into memory with a cold cache.
- Every modification records an entry in the transaction log (can be used in replication)
- Reads/Writes are abstracted by the buffer-pool, which serves as a cache.
- Writes are recorded twice (one after the other) for atomicity.
SQL REQUEST | \ / ---+ ` | +----------------------------------------------------+ | | buffer pool | | +----------------------------------------------------+ | | | . | | | / \ | Memory | | | | | | | | | | | | +------------+ | | | | log buffer | | | | +------+-----+ | | | | | | | | | | ---+ | | | ---+ | +------+------+ | | | |double-write | | | | | buffer | | | \ / +-------------+ | | Disk ` | | | +------+------+ +-------------+ | | | transaction | | datastore | | | | log | | (filesystem) | | | +-------------+ +-------------+ | | | | | +----------------+ ---+Disabling
You can't disable the buffer-pool.
InnoDB operates entirely in memory, which means you cannot entirely clear the cache.
Depending on your configuration, restarting the server may purge the cache, but it is best to check.When timing your queries, be mindful of the ordering of the indexes.
A full-table scan may look fast if it only needs to select the first N rows from the database,
but much later rows will surface the slowdown.Even still, I have not had much success with recreating datasets with "cold-cache" queries
while using the minimum possible pool size.
# bytes per chunk show variables like 'innodb_buffer_pool_chunk_size'; # number of pools show variables like 'innodb_buffer_pool_instances'; # bytes allowed for buffer pool # (must be a multiple of (chunk_size * pool_instances) OR VALUE WILL BE IGNORED) # (dynamic as of mysql-5.7.5) show variables like 'innodb_buffer_pool_size';Introspection
queryable:
select count(*) from information_schema.innodb_buffer_page; # pages in buffer pool (memory) (16KB/page ?) SHOW INNODB_BUFFER_POOL_STATS; # general info # memory allocation by item. SELECT * FROM innodb_buffer_stats_by_schema; SELECT * FROM innodb_buffer_stats_by_table;There are also cli tools.
innotop
Components
Buffer Pool
Adaptive Hash Index
The AdaptiveHashIndex, when enabled is designed to make innodb behave like an in-memory database.
This is only one component of caching, and in my experience disabling it has not been sufficient to reproduce "cold cache" queries.# interactive SHOW VARIABLES LIKE 'innodb_adaptive_hash_index'; SET GLOBAL innodb_adaptive_hash_index=OFF; # CLI mysqld --skip-innodb-adaptive-hash-indexYou could further scale back buffering by configuring mysql with a small buffer_pool_size (untested)
# my.cnf [mysqld] innodb_buffer_pool_size=1M key_buffer_size=8 query_cache_type=0Tablespace
Overview
The tablespace is a virtual filesystem that manages the database-file reads/writes.
Misc items stored in the tablespace:
- transactions: | open transactions keep state in the tablespace (I suspect related to MVCC) - per-table data-dictionary: | a ~4KB cache of table statisticsDouble-Write Buffer
When the buffer-pool (memory) is flushed,
it is written to the double-write buffer,
and only afterwards written to the datastore.This is used to ensure write atomicity.
Generally, the double write buffer keeps a rolling buffer of the most recent 100 written pages worth of data.
- If a write to the double-write buffer is interrupted (ex. crash)
when the database is brought back online, the (old) copy from the datastore is used.- If a write to the double-write buffer succeeds, but it's write the datastore is interrupted,
when the database is brought back online, the buffer's successful write is copied into the datastore.Data Dictionary
Caches table metadata (ex. indexes, tables, columns)
https://dev.mysql.com/doc/refman/5.7/en/innodb-data-dictionary.html
Transaction Log
InnoDB attempts to reduce the performance hit of random I/O by appending writes to a log
and periodically flushing the log (grouping sequential writes together).
This happens in two stages:
- writes to the logfile are buffered in memory
- the logfile itself is periodically flushed and written to the datastore
Tuning
# size of in-memory buffer (before writing to log files) innodb_log_buffer_size = # size of log-file (before writing to datastore) innodb_log_file_size =Monitoring
SHOW INNODB STATUS SHOW VARIABLES LIKE innodb_os_log_written
Plugins/Extension Components
Memcached Plugin
If this is enabled, you can use very fast get/set key-value operations instead of SQL queries (which require parsing, optimization).
The memcached instance is embedded in mysql, no separate process required.This is not used to improve the BufferPool's AdaptiveHashTable for regular SQL queries,
it exposes an entirely different interface.