本文主要简单介绍下8.0.17新引入的功能multi-valued index, 顾名思义,索引上对于同一个Primary key, 能够创建多个二级索引项,实际上已经对array类型的基础功能作了支持 (感受官方将来必定会推出相似pg的array 列类型), 并基于array来构建二级索引,这意味着该二级索引的记录数能够是多于汇集索引记录数的,于是该索引不能够用于一般意义的查询,只能经过特定的接口函数来使用,下面的例子里会说明。json
本文不对代码作深刻了解,仅仅记录下相关的入口函数,便于之后工做遇到时能快速查阅。在最后附上了对应worklog的链接,感兴趣的朋友能够直接阅读worklog去了解他是如何实现的。数组
范例
摘录自官方文档函数
root@test 04:08:50>show create table customersG 1. row **ui
Table: customers
Create Table: CREATE TABLE customers
(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可使用到该索引code
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)orm
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上取出来的数组
数组上存在相同值的话,会只存储一个到索引上。支持的类型:DECIMAL, INTEGER, DATETIME,VARCHAR/CHAR。另外index上只能有一个multi-value column。接口
下面简单介绍下相关的接口函数
数组最大容量:
入口函数: ha_innobase::mv_key_capacity
插入记录:
入口函数 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
参考文档WL#10604: Create multi-value indexWL#8763: support multi-value functional index for InnoDBWL#8955: Add support for multi-valued indexes官方文档