新功能初探 | MySQL 8.0 Multi-Valued Indexes功能簡述

顧名思義,索引上對於同一個Primary key, 能夠創建多個二級索引項,實際上已經對array類型的基礎功能作了支持,並基於array來構建二級索引。
這意味着該二級索引的記錄數能夠是多於彙集索引記錄數的,於是該索引不能夠用於一般意義的查詢,只能經過特定的接口函數來使用,下面的例子裏會說明。json

範例

摘錄自官方文檔數組

root@test 04:08:50>show create table customers\G                                                                                                                                  
  `id` bigint(20) NOT NULL AUTO_INCREMENT,
  `modified` datetime DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
  `custinfo` json DEFAULT NULL,
  PRIMARY KEY (`id`),
  KEY `zips` ((cast(json_extract(`custinfo`,_latin1'$.zip') as unsigned array)))
) ENGINE=InnoDB AUTO_INCREMENT=6 DEFAULT CHARSET=latin1
1 row in set (0.00 sec)

root@test 04:08:53>select * from customers;
+----+---------------------+-------------------------------------------------------------------+
| id | modified            | custinfo                                                          |
+----+---------------------+-------------------------------------------------------------------+
|  1 | 2019-08-14 16:08:50 | {"user": "Jack", "user_id": 37, "zipcode": [94582, 94536]}        |
|  2 | 2019-08-14 16:08:50 | {"user": "Jill", "user_id": 22, "zipcode": [94568, 94507, 94582]} |
|  3 | 2019-08-14 16:08:50 | {"user": "Bob", "user_id": 31, "zipcode": [94477, 94536]}         |
|  4 | 2019-08-14 16:08:50 | {"user": "Mary", "user_id": 72, "zipcode": [94536]}               |
|  5 | 2019-08-14 16:08:50 | {"user": "Ted", "user_id": 56, "zipcode": [94507, 94582]}         |
+----+---------------------+-------------------------------------------------------------------+
5 rows in set (0.00 sec)

經過以下三個函數member of, json_contains, json_overlaps能夠使用到該索引函數

root@test 04:09:00>SELECT * FROM customers WHERE 94507 MEMBER OF(custinfo->'$.zipcode');
+----+---------------------+-------------------------------------------------------------------+
| id | modified            | custinfo                                                          |
+----+---------------------+-------------------------------------------------------------------+
|  2 | 2019-08-14 16:08:50 | {"user": "Jill", "user_id": 22, "zipcode": [94568, 94507, 94582]} |
|  5 | 2019-08-14 16:08:50 | {"user": "Ted", "user_id": 56, "zipcode": [94507, 94582]}         |
+----+---------------------+-------------------------------------------------------------------+
2 rows in set (0.00 sec)

root@test 04:09:41>SELECT * FROM customers  WHERE JSON_CONTAINS(custinfo->'$.zipcode', CAST('[94507,94582]' AS JSON));
+----+---------------------+-------------------------------------------------------------------+
| id | modified            | custinfo                                                          |
+----+---------------------+-------------------------------------------------------------------+
|  2 | 2019-08-14 16:08:50 | {"user": "Jill", "user_id": 22, "zipcode": [94568, 94507, 94582]} |
|  5 | 2019-08-14 16:08:50 | {"user": "Ted", "user_id": 56, "zipcode": [94507, 94582]}         |
+----+---------------------+-------------------------------------------------------------------+
2 rows in set (0.00 sec)

root@test 04:09:54>SELECT * FROM customers   WHERE JSON_OVERLAPS(custinfo->'$.zipcode', CAST('[94507,94582]' AS JSON));
+----+---------------------+-------------------------------------------------------------------+
| id | modified            | custinfo                                                          |
+----+---------------------+-------------------------------------------------------------------+
|  1 | 2019-08-14 16:08:50 | {"user": "Jack", "user_id": 37, "zipcode": [94582, 94536]}        |
|  2 | 2019-08-14 16:08:50 | {"user": "Jill", "user_id": 22, "zipcode": [94568, 94507, 94582]} |
|  5 | 2019-08-14 16:08:50 | {"user": "Ted", "user_id": 56, "zipcode": [94507, 94582]}         |
+----+---------------------+-------------------------------------------------------------------+
3 rows in set (0.00 sec)

接口函數

multi-value index是functional index的一種實現,列的定義是一個虛擬列,值是從json column上取出來的數組。ui

數組上存在相同值的話,會只存儲一個到索引上。支持的類型:DECIMAL, INTEGER, DATETIME,VARCHAR/CHAR。另外index上只能有一個multi-value column。
下面簡單介紹下相關的接口函數指針

數組最大容量:code

入口函數:
ha_innobase::mv_key_capacityorm

插入記錄:排序

入口函數:
row_ins_sec_index_multi_value_entry
經過類Multi_value_entry_builder_insert來構建tuple, 而後調用正常的接口函數row_ins_sec_index_entry插入到二級索引中。
已經解析好,排序並去重的數據存儲在結構struct multi_value_data , 指針在dfield_t::data中. multi_value_data結構也是multi-value具體值的內存表現索引

刪除記錄:接口

入口函數:
row_upd_del_multi_sec_index_entry
基於類Multi_value_entry_builder_normal構建tuple, 並依次從索引中刪除

更新記錄

入口函數:
row_upd_multi_sec_index_entry
因爲可能不是全部的二級索引記錄都須要更新,須要計算出diff,找出要更新的記錄calc_row_difference --> innobase_get_multi_value_and_diff, 設置一個須要更新的bitmap

事務回滾

相關函數:

row_undo_ins_remove_multi_sec
row_undo_mod_upd_del_multi_sec
row_undo_mod_del_mark_multi_sec

回滾的時候經過trx_undo_rec_get_multi_value從undo log中獲取multi-value column的值,經過接口Multi_value_logger::read來構建並存儲到field data中

記錄undo log

函數: trx_undo_store_multi_value
經過Multi_value_logger::log將multi-value的信息存儲到Undo log中. 'Multi_value_logger'是一個輔助類,用於記錄multi-value column的值以及如何讀出來

purge 二級索引記錄

入口函數:

row_purge_del_mark
row_purge_upd_exist_or_extern_func
    |--> row_purge_remove_multi_sec_if_poss

本文做者: Roin123

原文連接

本文爲雲棲社區原創內容,未經容許不得轉載。

相關文章
相關標籤/搜索