接着上篇文章 Mysql InnoDB&MyISAM 支持Hash么 ,我们知道InnoDB是不支持HASH的,但是有个 Adaptive Hash
的概念让InnoDB在
原文如下 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%";
本文原创链接:
注意:本文来自网友投稿。本站无法对本文内容的真实性、完整性、及时性、原创性提供任何保证,请您自行验证核实并承担相关的风险与后果!
CoLaBug.com遵循[CC BY-SA 4.0]分享并保持客观立场,本站不承担此类作品侵权行为的直接责任及连带责任。您有版权、意见、投诉等问题,请通过[eMail]联系我们处理,如需商业授权请联系原作者/原网站。