MySQL InnoDB中的Adaptive Hash

接着上篇文章Mysql InnoDB&MyISAM 支持Hash么,我们知道InnoDB是不支持HASH的,但是有个Adaptive Hash的概念让InnoDB在

原文如下adaptive hash index:

adaptive hash index

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 B-tree index on the table. 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.

In MySQL 5.6 and higher, another way to take advantage of fast single-value lookups with InnoDB tables is to use the InnoDB memcached plugin. See Section 15.20, “InnoDB memcached Plugin” for details.

See Also B-tree, buffer pool, hash index, memcached, page, secondary index.

翻译:

自适应HASH,是一种通过在内存中构建HASH索引,实现对使用InnoDB引擎的表进行=IN查询操作加速的优化. MySQL监控InnoDB表的数据搜索操作,当发现如果使用HASH索引查询某条数据能产生性能效益时,那么MySQL会自动的对这个频繁查询的数据在index page上建立一个索引. 从某种意义上说,自适应哈希索引是一种利用充足的内存资源在运行期产生的特殊的使用了HASH结构的加速索引,它的将InnoDB频繁访问的数据缓存在主内存中,让数据更加靠近IO性能好的内存并远离IO性能差的磁盘,这个特性由innodb_adaptive_hash_index配置选项控制。由于该特性只对某些工作负载(=,IN)有好处,而对其他工作负载没有好处,而且用于散列索引的内存保留在缓冲池中,无法体现MySQL随机查询的实际性能,因此通常您应该启用和禁用该特性进行基准测试.

哈希索引总是基于InnoDB表中现有的B-tree索引构建的,MySQL可以在为B-tree定义的键的任意长度的前缀上构建散列索引,这取决于针对索引的搜索模式.哈希索引可以是部分索引;整个B-tree索引不需要缓存在缓冲池中。

在MySQL 5.6及更高版本中,利用InnoDB表快速单值查找的另一种方式是使用InnoDB memcached插件.

个人解读:

  • Adaptive Hash实际上是在在运行期依据实际的数据查询情况动态产生的,它产生后存放在内存中,以便充分利用内存的随机IO优势
  • Adaptive Hash只保存这张表的热点查询数据,不是对整个BTREE索引或者数据进行全部缓存,这样太费内存而且不现实
  • Adaptive Hash的保存在内存中的设计决定的它缓存的数据总量有限,如果你的机器内存足够那么使用innodb_adaptive_hash_index打开Adaptive Hash并分配内存到合适的大小会有性能收益.
  • 如果MySQL服务重启或者崩溃,那么Adaptive Hash在MySQL服务重新正常工作后需要重新动态的创建,以前的热点数据会消失.
  • Adaptive Hash实际上是对BTREE索引做缓存优化,加速单值等值查询.为了节约内存,可以对BTREE索引的部分索引内容做HASH而不是非得要做全部内容的HASH.

下面是查询MySQL有关Adaptive Hash的属性配置的演示

mysql> show variables like "%innodb_adaptive%";

innodb adaptice index

本文原创链接:

你可能感兴趣的:(mysql)