顾名思义,索引上对于同一个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== "点击并拖拽以移动")
分享到:
相关推荐
Topological Spaces: Including a Treatment of Multi-Valued Functions, Vector Spaces and Convexity Topological Spaces: Including a Treatment of Multi-Valued Functions, Vector Spaces and Convexity ...
2020年1月13日,MySQL官方发布了今年的第一个8.0版本-8.0.19,自2018年4月19日发布第一个稳定版本8.0.11到如今已经历了近两年共9个小版本的迭代,产品稳定性持续增强。极具吸引力的是官方在8.0版本上对MGR bug 的...
《Complex-valued_matrix_derivatives.pdf》是一本深入探讨这一主题的专著,它不仅详细地推导了与复数矩阵变量相关的标量、向量和矩阵函数的导数公式,还为读者提供了丰富的实际例子以展示这些工具如何用来分析和...
多值随机微分方程(Multi-valued Stochastic Differential Equations,MSDEs)作为一类扩展的随机微分方程模型,更贴近实际问题中的复杂性,其中系统的输出并非单一值,而是一个值的集合。本文将深入探讨受小参数...
在本文《不使用数字的语言变量多值认知地图计算》中,作者Dmitry Maximov探讨了一个扩展模糊认知图(Fuzzy Cognitive Maps, FCMs)的概念——多值认知地图(Multi-Valued Cognitive Maps),其中所有的变量和权重...
多值神经元的CR-梯度学习算法的收敛性,徐东坡,梁爽,基于CR微分理论,我们提出一个CR梯度学习算法用于训练多值神经元模型。在训练样本是$k$-可分的条件下,我们证明了所提出的新学习算法�
从名字来看,这个库似乎专注于SQL和UDTFS(User-Defined Table-Valued Functions),可能是一个扩展SQL功能的工具,尤其是关于自定义表值函数,这在数据分析或数据库操作中非常有用。 总的来说,"stonewave-sql-...
### Chan Active Contours without Edges for Vector-Valued Images #### 概述 本文献《Active Contours without Edges for Vector-Valued Images》由Tony F. Chan、B. Yezrielev Sandberg 和 Luminita A. Vese ...
标题“Multi-Valued Neural Network Trained by Differential Evolution for Synthesizing Multiple-Valued Functions”指出了文章的两个关键点:多值神经网络(Multi-Valued Neural Network)和差分进化算法...
在本文中,林作铨和贾肇聪提出了一种新的四值逻辑系统,该系统具有直观的语义特性。直观语义在这里指的是逻辑系统能够捕捉到任何判断依据所拥有的信息是支持还是反对某一结论的两面性。该逻辑系统中的联词(逻辑...
In logic, a three-valued logic (also trivalent, ternary, trinary logic, or trilean,[citation needed] sometimes abbreviated 3VL) is any of several many-valued logic systems in which there are three ...
在内容中提到的关键词“Lagrangestability”(拉格朗日稳定性)、“Complex-valued neural networks”(复值神经网络)、“Time delays”(时滞)以及“Matrix measure”(矩阵测度)是研究复值时滞神经网络稳定性时...
在当今的科技领域,复数矩阵的求导是数学和工程学研究中的一项关键技能。这本书《复数矩阵的求导》是来自Are Hjørungnes教授的一本原创作品,作为一本系统性介绍复矩阵求导理论的教材,它对研究中遇到的含有复数...
《Complex-Valued Neural Networks》是由Akira Hirose编著的一本专业书籍,专注于探讨复值神经网络(Complex-Valued Neural Networks,简称CVNNs)的理论与应用。复值神经网络是一种特殊的神经网络模型,它在神经元...
Interval-Valued Intuitionistic Fuzzy Sets (Krassimir T. Atanassov)
Using the method of maximizing deviations to multiple attribute decision making under interval-valued intuitionistic fuzzy environment,卫贵武,,With respect to multiple attribute decision making ...
Kernel methods are among the most popular techniques in machine learning. From a regularization perspec- tive they play a central role in regularization theory as they provide a natural choice for the...