MySQL(七):InnoDB 自適應Hash索引(Adaptive Hash Index)

1、簡述

哈希(hash)查找非常快,一般情況下時間複雜度爲 O(1),一般僅需要一次查找就能定位。而 B+ 樹的查找次數,取決於 B+ 樹的高度,B+ 樹的高度一般爲 34層,因此便需要查詢34次。

InnoDB 存儲引擎通過觀察表上索引頁的查詢,如果發現建立哈希索引可以提升查詢效率,則會自動建立哈希索引,稱之爲自適應哈希索引(Adaptive Hash Index)。

AHI 是基於緩衝池的B+樹構造的索引,因此速度非常快,而且不需要對整張表建立索引。InnoDB 存儲引擎會自動根據訪問的頻率和模式來自動爲某些熱點頁建立哈希索引。

考慮到不同系統的差異,有些系統開啓自適應哈希索引可能會導致性能提升不明顯,而且爲監控索引頁查詢次數增加了多餘的性能損耗, MySQL5.7 更改了 AHI 實現機制,每個 AHI 都分配了特定分區,每個索引都綁定到特定分區,並且每個分區均受單獨的鎖存器保護。分區由 innodb_adaptive_hash_index_parts 變量控制。默認情況下,innodb_adaptive_hash_index_parts 變量值爲8,最大值爲 512。

自適應哈希索引功能由 innodb_adaptive_hash_index 變量啓用,或者在服務器啓動時由–skip-innodb-adaptive-hash-index關閉。

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

2、AHI(Adaptive Hash index)創建條件及注意事項

1、重複訪問某一特定查詢模式達到一定數量纔會創建,如WHERE a = XXX,WHERE a = xxx and b = xxx。

2、AHI 只支持等值查詢=和IN,不支持LIKE, REGEXP等。

3、AHI 存在內存中,佔用緩衝池資源(Buffer Pool)。

4、AHI 無法人爲干預,只能配置開關:set global innodb_adaptive_hash_index=off/on。

3、AHI(Adaptive Hash index)監控

3.1、通過 show engine innodb status 命令查看AHI狀態

-------------------------------------
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 1 buffer(s)
Hash table size 34679, node heap has 3 buffer(s)
0.00 hash searches/s, 0.00 non-hash searches/s

3.2、通過 information_schema.innodb_metrics 來監控AHI運行狀態

該表蒐集了AHI 查詢次數,更新次數等信息,可以很好的監控其運行狀態,在某些負載下,AHI並不適合打開,關閉AHI可以避免額外的維護開銷。

mysql> set global innodb_monitor_enable = module_adaptive_hash;
Query OK, 0 rows affected (0.00 sec)

mysql> SELECT STATUS, NAME, subsystem FROM information_schema.INNODB_METRICS WHERE subsystem LIKE '%adaptive_hash%';
+---------+------------------------------------------+---------------------+
| STATUS  | NAME                                     | subsystem           |
+---------+------------------------------------------+---------------------+
| enabled | adaptive_hash_searches                   | adaptive_hash_index |
| enabled | adaptive_hash_searches_btree             | adaptive_hash_index |
| enabled | adaptive_hash_pages_added                | adaptive_hash_index |
| enabled | adaptive_hash_pages_removed              | adaptive_hash_index |
| enabled | adaptive_hash_rows_added                 | adaptive_hash_index |
| enabled | adaptive_hash_rows_removed               | adaptive_hash_index |
| enabled | adaptive_hash_rows_deleted_no_hash_entry | adaptive_hash_index |
| enabled | adaptive_hash_rows_updated               | adaptive_hash_index |
+---------+------------------------------------------+---------------------+
8 rows in set (0.00 sec)

4、參考文獻

  1. 《高性能MySQL(第3版)》
  2. 《MySQL技術內幕:InnoDB存儲引擎(第2版)》
  3. 《MySQL源碼庫》
  4. 《MySQL參考手冊》
  5. 《MySQL實戰45講》
  6. 《數據庫內核月報》
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章