`
大涛学长
  • 浏览: 105757 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

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

阅读更多
顾名思义,索引上对于同一个Primary key, 可以建立多个二级索引项,实际上已经对array类型的基础功能做了支持,并基于array来构建二级索引。 
这意味着该二级索引的记录数可以是多于聚集索引记录数的,因而该索引不可以用于通常意义的查询,只能通过特定的接口函数来使用,下面的例子里会说明。

> 关注公众号“阿里数据库技术”,回复“MySQL”获取相关文档。

### 范例

摘录自官方文档 
\*请左右滑动阅览

```
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)

```

![](data:image/gif;base64,R0lGODlhAQABAPABAP///wAAACH5BAEKAAAALAAAAAABAAEAAAICRAEAOw==)![](data:image/gif;base64,R0lGODlhAQABAPABAP///wAAACH5BAEKAAAALAAAAAABAAEAAAICRAEAOw== "点击并拖拽以移动")

通过如下三个函数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)
```

![](data:image/gif;base64,R0lGODlhAQABAPABAP///wAAACH5BAEKAAAALAAAAAABAAEAAAICRAEAOw==)![](data:image/gif;base64,R0lGODlhAQABAPABAP///wAAACH5BAEKAAAALAAAAAABAAEAAAICRAEAOw== "点击并拖拽以移动")

接口函数
----

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
```

![](data:image/gif;base64,R0lGODlhAQABAPABAP///wAAACH5BAEKAAAALAAAAAABAAEAAAICRAEAOw==)![](data:image/gif;base64,R0lGODlhAQABAPABAP///wAAACH5BAEKAAAALAAAAAABAAEAAAICRAEAOw== "点击并拖拽以移动")

回滚的时候通过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
```

![](data:image/gif;base64,R0lGODlhAQABAPABAP///wAAACH5BAEKAAAALAAAAAABAAEAAAICRAEAOw==)![](data:image/gif;base64,R0lGODlhAQABAPABAP///wAAACH5BAEKAAAALAAAAAABAAEAAAICRAEAOw== "点击并拖拽以移动")
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics