MySQL中的自適應哈希索引


眾所周知,InnoDB使用的索引結構是B+樹,但其實它還支持另一種索引:自適應哈希索引。

哈希表是數組+鏈表的形式。通過哈希函數計算每個節點數據中鍵所對應的哈希桶位置,如果出現哈希沖突,就使用拉鏈法來解決。更多內容可以參考 百度百科-哈希表

從以上可以知道,哈希表查找最優情況下是查找一次.而InnoDB使用的是B+樹,最優情況下的查找次數根據層數決定。因此為了提高查詢效率,InnoDB便允許使用自適應哈希來提高性能。

可以通過參數 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
============================

可以看到自適應哈希索引大小,每秒的使用情況。

注意從哈希表的特性來看,自適應哈希索引只能用於等值查詢,范圍或者大小是不允許的。

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


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM