本文主要简单介绍下8.0.17新引入的功能multi-valued index, 顾名思义,索引上对于同一个Primary key, 可以建立多个二级索引项,实际上已经对array类型的基础功能做了支持 (感觉官方未来一定会推出类似pg的array 列类型), 并基于array来构建二级索引,这意味着该二级索引的记录数可以是多于聚集索引记录数的,因而该索引不可以用于通常意义的查询,只能通过特定的接口函数来使用,下面的例子里会说明。

本文不对代码做深入了解,仅仅记录下相关的入口函数,便于以后工作遇到时能快速查阅。在最后附上了对应worklog的连接,感兴趣的朋友可以直接阅读worklog去了解他是如何实现的。

范例

摘录自官方文档

  1. root@test 04:08:50>show create table customers\G
  2. *************************** 1. row ***************************
  3. Table: customers
  4. Create Table: CREATE TABLE `customers` (
  5. `id` bigint(20) NOT NULL AUTO_INCREMENT,
  6. `modified` datetime DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
  7. `custinfo` json DEFAULT NULL,
  8. PRIMARY KEY (`id`),
  9. KEY `zips` ((cast(json_extract(`custinfo`,_latin1'$.zip') as unsigned array)))
  10. ) ENGINE=InnoDB AUTO_INCREMENT=6 DEFAULT CHARSET=latin1
  11. 1 row in set (0.00 sec)
  12. root@test 04:08:53>select * from customers;
  13. +----+---------------------+-------------------------------------------------------------------+
  14. | id | modified | custinfo |
  15. +----+---------------------+-------------------------------------------------------------------+
  16. | 1 | 2019-08-14 16:08:50 | {"user": "Jack", "user_id": 37, "zipcode": [94582, 94536]} |
  17. | 2 | 2019-08-14 16:08:50 | {"user": "Jill", "user_id": 22, "zipcode": [94568, 94507, 94582]} |
  18. | 3 | 2019-08-14 16:08:50 | {"user": "Bob", "user_id": 31, "zipcode": [94477, 94536]} |
  19. | 4 | 2019-08-14 16:08:50 | {"user": "Mary", "user_id": 72, "zipcode": [94536]} |
  20. | 5 | 2019-08-14 16:08:50 | {"user": "Ted", "user_id": 56, "zipcode": [94507, 94582]} |
  21. +----+---------------------+-------------------------------------------------------------------+
  22. 5 rows in set (0.00 sec)

通过如下三个函数member of, json_contains, json_overlaps可以使用到该索引

  1. root@test 04:09:00>SELECT * FROM customers WHERE 94507 MEMBER OF(custinfo->'$.zipcode');
  2. +----+---------------------+-------------------------------------------------------------------+
  3. | id | modified | custinfo |
  4. +----+---------------------+-------------------------------------------------------------------+
  5. | 2 | 2019-08-14 16:08:50 | {"user": "Jill", "user_id": 22, "zipcode": [94568, 94507, 94582]} |
  6. | 5 | 2019-08-14 16:08:50 | {"user": "Ted", "user_id": 56, "zipcode": [94507, 94582]} |
  7. +----+---------------------+-------------------------------------------------------------------+
  8. 2 rows in set (0.00 sec)
  9. root@test 04:09:41>SELECT * FROM customers WHERE JSON_CONTAINS(custinfo->'$.zipcode', CAST('[94507,94582]' AS JSON));
  10. +----+---------------------+-------------------------------------------------------------------+
  11. | id | modified | custinfo |
  12. +----+---------------------+-------------------------------------------------------------------+
  13. | 2 | 2019-08-14 16:08:50 | {"user": "Jill", "user_id": 22, "zipcode": [94568, 94507, 94582]} |
  14. | 5 | 2019-08-14 16:08:50 | {"user": "Ted", "user_id": 56, "zipcode": [94507, 94582]} |
  15. +----+---------------------+-------------------------------------------------------------------+
  16. 2 rows in set (0.00 sec)
  17. root@test 04:09:54>SELECT * FROM customers WHERE JSON_OVERLAPS(custinfo->'$.zipcode', CAST('[94507,94582]' AS JSON));
  18. +----+---------------------+-------------------------------------------------------------------+
  19. | id | modified | custinfo |
  20. +----+---------------------+-------------------------------------------------------------------+
  21. | 1 | 2019-08-14 16:08:50 | {"user": "Jack", "user_id": 37, "zipcode": [94582, 94536]} |
  22. | 2 | 2019-08-14 16:08:50 | {"user": "Jill", "user_id": 22, "zipcode": [94568, 94507, 94582]} |
  23. | 5 | 2019-08-14 16:08:50 | {"user": "Ted", "user_id": 56, "zipcode": [94507, 94582]} |
  24. +----+---------------------+-------------------------------------------------------------------+
  25. 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

事务回滚 相关函数:

  1. row_undo_ins_remove_multi_sec
  2. row_undo_mod_upd_del_multi_sec
  3. 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 二级索引记录 入口函数:

  1. row_purge_del_mark
  2. row_purge_upd_exist_or_extern_func
  3. |--> row_purge_remove_multi_sec_if_poss

参考文档

WL#10604: Create multi-value index

WL#8763: support multi-value functional index for InnoDB

WL#8955: Add support for multi-valued indexes

官方文档