MySQL中的自适应哈希索引

众所周知,InnoDB使用的索引结构是B+树,但其实它还支持另外一种索引:自适应哈希索引。node

哈希表是数组+链表的形式。经过哈希函数计算每一个节点数据中键所对应的哈希桶位置,若是出现哈希冲突,就使用拉链法来解决。更多内容能够参考 百度百科-哈希表mysql

从以上能够知道,哈希表查找最优状况下是查找一次.而InnoDB使用的是B+树,最优状况下的查找次数根据层数决定。所以为了提升查询效率,InnoDB便容许使用自适应哈希来提升性能。sql

能够经过参数 innodb_adaptive_hash_index 来决定是否开启。默认是打开的。数组

 
mysql> show variables like "innodb_adaptive_hash_index";
+----------------------------+-------+
| Variable_name              | Value |
+----------------------------+-------+
| innodb_adaptive_hash_index | ON    |
+----------------------------+-------+

存储引擎会自动对个索引页上的查询进行监控,若是可以经过使用自适应哈希索引来提升查询效率,其便会自动建立自适应哈希索引,不须要开发人员或运维人员进行任何设置操做。运维

自适应哈希索引是对innodb的缓冲池的B+树页进行建立,不是对整张表建立,所以速度很快。函数

 

能够经过查看innodb的status来查看自适应哈希索引的使用状况。性能

mysql> show engine innodb  status \G
*************************** 1. row ***************************
  Type: InnoDB
  Name: 
Status: 
=====================================
2019-03-07 23:37:23 0x7f1f2d34c700 INNODB MONITOR OUTPUT
=====================================
Per second averages calculated from the last 6 seconds
------------------------------------------------------
INSERT BUFFER AND ADAPTIVE HASH INDEX
-------------------------------------
Ibuf: size 1, free list len 0, seg size 2, 0 merges
merged operations:
 insert 0, delete mark 0, delete 0
discarded operations:
 insert 0, delete mark 0, delete 0
Hash table size 34679, node heap has 0 buffer(s)
Hash table size 34679, node heap has 0 buffer(s)
Hash table size 34679, node heap has 0 buffer(s)
Hash table size 34679, node heap has 0 buffer(s)
Hash table size 34679, node heap has 0 buffer(s)
Hash table size 34679, node heap has 0 buffer(s)
Hash table size 34679, node heap has 0 buffer(s)
Hash table size 34679, node heap has 0 buffer(s)
0.00 hash searches/s, 0.00 non-hash searches/s
-------------------------------
END OF INNODB MONITOR OUTPUT
============================

能够看到自适应哈希索引大小,每秒的使用状况。spa

注意从哈希表的特性来看,自适应哈希索引只能用于等值查询,范围或者大小是不容许的。code

等着查询: select * from xx where name = "xxx";blog

相关文章
相关标签/搜索