An optimization for InnoDB tables that can speed up lookups using =
and IN
operators, by constructing a hash index in memory. MySQL monitors index searches for InnoDB tables, and if queries could benefit from a hash index, it builds one automatically for index pages that are frequently accessed. In a sense, the adaptive hash index configures MySQL at runtime to take advantage of ample main memory, coming closer to the architecture of main-memory databases. This feature is controlled by the innodb_adaptive_hash_index
configuration option. Because this feature benefits some workloads and not others, and the memory used for the hash index is reserved in the buffer pool, typically you should benchmark with this feature both enabled and disabled.
The hash index is always built based on an existing InnoDB secondary index, which is organized as a B-tree structure. MySQL can build a hash index on a prefix of any length of the key defined for the B-tree, depending on the pattern of searches against the index. A hash index can be partial; the whole B-tree index does not need to be cached in the buffer pool.
The feature known as the adaptive hash index (AHI) lets InnoDB
perform more like an in-memory database on systems with appropriate combinations of workload and ample memory for the buffer pool, without sacrificing any transactional features or reliability. This feature is enabled by the innodb_adaptive_hash_index
option, or turned off by the --skip-innodb_adaptive_hash_index
at server startup.
If a table fits almost entirely in main memory, a hash index can speed up queries by enabling direct lookup of any element, turning the index value into a sort of pointer. InnoDB
has a mechanism that monitors index searches. IfInnoDB
notices that queries could benefit from building a hash index, it does so automatically.
With some workloads, the speedup from hash index lookups greatly outweighs the extra work to monitor index lookups and maintain the hash index structure. Sometimes, the read/write lock that guards access to the adaptive hash index can become a source of contention under heavy workloads, such as multiple concurrent joins. Queries with LIKE
operators and %
wildcards also tend not to benefit from the AHI. For workloads where the adaptive hash index is not needed, turning it off reduces unnecessary performance overhead. Because it is difficult to predict in advance whether this feature is appropriate for a particular system, consider running benchmarks with it both enabled and disabled, using a realistic workload.
You can monitor the use of the adaptive hash index and the contention for its use in the SEMAPHORES
section of the output of the SHOW ENGINE INNODB STATUS
command. If you see many threads waiting on an RW-latch created in btr0sea.c
, then it might be useful to disable adaptive hash indexing.
innodb_adaptive_hash_index
This variable is enabled by default. As of MySQL 5.5, You can modify this parameter using the SET GLOBAL
statement, without restarting the server. Changing the setting requires the SUPER
privilege. You can also use --skip-innodb_adaptive_hash_index
at server startup to disable it.
Disabling the adaptive hash index empties the hash table immediately. Normal operations can continue while the hash table is emptied, and executing queries that were using the hash table access the index B-trees directly instead. When the adaptive hash index is re-enabled, the hash table is populated again during normal operation.
参考:
http://dev.mysql.com/doc/innodb/1.1/en/glossary.html#glos_adaptive_hash_index
http://dev.mysql.com/doc/refman/5.5/en/innodb-table-and-index.html#innodb-adaptive-hash
http://dev.mysql.com/doc/refman/5.5/en/innodb-parameters.html#sysvar_innodb_adaptive_hash_index