文档

向量检索

更新时间:

AnalyticDB MySQL的向量检索功能可以帮助您实现非结构化数据的近似检索。本文主要介绍向量检索功能以及如何创建并使用向量索引。

前提条件

集群的内核版本需为3.1.4.0及以上版本。

说明
  • 内核版本为3.1.5.16、3.1.6.8、3.1.8.6及以上版本的集群向量索引功能相对稳定。

  • 若您的集群不是上述列举的稳定版本,建议您先将参数CSTORE_PROJECT_PUSH_DOWNCSTORE_PPD_TOP_N_ENABLE设置为false,再使用向量索引功能。

  • 如何查看集群内核版本,请参见如何查看实例版本信息。如需升级内核版本,请联系技术支持。

背景信息

功能介绍

您可以通过AI算法提取非结构化数据的特征进行数据编码,形成一个特征向量,将特征向量存储在AnalyticDB MySQL中。使用特征向量标识非结构化数据,向量间的距离用于衡量非结构化数据之间的相似度。AnalyticDB MySQL集群提供高效的向量检索功能,可应用于图片检索、声纹匹配、人脸识别、文本检索等场景中。

产品架构

image.png

产品优势

  • 向量数据的高维度、高性能和高召回率。

    以人脸512维向量为例,AnalyticDB MySQL向量检索提供百亿向量100 QPS、50毫秒响应时间约束下99%的数据召回率和两亿向量1000 QPS、1秒响应时间约束下99%的数据召回率。

  • 结构化和非结构数据的融合查询

    支持KNN和RNN融合查询,例如:比较一批向量与另外一批向量的相似度。

  • 实时更新

    支持高并发的实时写入和实时更新,数据写入后即可查询。

  • 实时检索

    MPP查询架构提供毫秒级实时检索性能,提升检索效率。

  • 开箱即用

    支持标准的SQL语句,简化开发流程,不需要额外安装其他复杂的配置。

基本概念

特征向量

向量是一种将实体和应用代数化的表示。向量将实体间的关系抽象成向量空间中的距离,距离的远近代表相似程度。例如:身高、年龄、性别、地域等。在AnalyticDB for MySQL中,特征向量的数据类型为数组,仅支持固定长度数组。 只支持:array <float>array <byte>array <smallint>三种数据类型。

向量检索

在特征向量数据集合中进行快速搜索和匹配的方法。

向量索引

特定类型的索引。

距离计算

特定类型的自定义函数,每个距离计算公式对应一个自定义函数。例如:L2_DISTANCE

KNN

KNN(K-Nearest Neighbor)算法用于查找特征向量数据集合中离查询点最近的 K 个点。

RNN

RNN(Radius Nearest Neighbor)算法用于查找特征向量数据集合中查询点在某半径范围内的所有点。

创建向量索引

语法

您可以在创建表时同步创建向量索引。定义为:

ann index [index_name] (column_name)] [algorithm=HNSW_PQ ] [distancemeasure=SquaredL2]

参数说明

  • ann index:向量索引关键字。

  • index_name:索引名。索引的命名规则,请参见命名约束

  • column_name:向量列的名称。列名的命名规则,请参见命名约束

  • algorithm:向量距离计算公式使用的算法,取值仅支持:HNSW_PQ

  • distancemeasure:向量距离计算公式,取值仅支持:SquaredL2SquaredL2计算公式为:(x1-y1)2+(x2-y2)2+…...(xn-yn)2

示例

本示例创建表tbl_vector,表定义了两个向量列float_featureshort_feature,其中,float_feature的类型为arrary<float>,维度为4short_feature类型为arrary<smallint>,维度也为4,对这两个列分别创建向量索引。

CREATE TABLE tbl_vector (
  xid bigint not null,
  cid bigint not null,
  uid varchar not null,
  vid varchar not null,
  wid varchar not null,
  float_feature array < float >(4),
  short_feature array < smallint >(4),
  ann index idx_short_feature(short_feature),
  ann index idx_float_feature(float_feature),
  PRIMARY KEY (xid, cid, vid)
) DISTRIBUTE BY HASH(xid);

插入数据

INSERT into tbl_vector (xid,cid,uid,vid,wid,short_feature,float_feature) VALUES (1,2,'A','B','C','[1,1,1,1]','[1.2,1.5,2,3.0]');
INSERT into tbl_vector (xid,cid,uid,vid,wid,short_feature,float_feature) VALUES (2,1,'e','v','f','[2,2,2,2]','[1.5,1.15,2.2,2.7]');
INSERT into tbl_vector (xid,cid,uid,vid,wid,short_feature,float_feature) VALUES (0,6,'d','f','g','[3,3,3,3]','[0.2,1.6,5,3.7]');
INSERT into tbl_vector (xid,cid,uid,vid,wid,short_feature,float_feature) VALUES (5,4,'j','b','h','[4,4,4,4]','[1.0,4.15,6,2.9]');
INSERT into tbl_vector (xid,cid,uid,vid,wid,short_feature,float_feature) VALUES (8,5,'Sj','Hb','Dh','[5,5,5,5]','[1.3,4.5,6.9,5.2]');
insert into tbl_vector (xid,cid,uid,vid,wid,short_feature,float_feature) VALUES (5,4,'x','g','h','[3,4,4,4]','[1.0,4.15,6,2.9]');
insert into tbl_vector (xid,cid,uid,vid,wid,short_feature,float_feature) VALUES (5,4,'j','r','k','[6,6,4,4]','[1.0,4.15,6,2.9]');
insert into tbl_vector (xid,cid,uid,vid,wid,short_feature,float_feature) VALUES (5,4,'s','i','q','[2,2,4,4]','[1.0,4.15,6,2.9]');

查询数据

  • 查询short_feature与向量'[1,1,1,1]'距离最近的3条记录,按距离排序:

    SELECT xid, l2_distance(short_feature, '[1,1,1,1]') as dis FROM tbl_vector ORDER BY 2 LIMIT 3;

    返回结果:

    +-------+--------------+
    | xid   |   dis        |
    +-------+--------------+
    | 1     |   0.0        |
    +-------+--------------+
    | 2     |   4.0        |
    +-------+--------------+
    | 0     |   16.0       |
    +-------+--------------+
  • 查询xid为5且cid为4,short_feature与向量'[1,1,1,1]'距离最近的4条记录,按距离排序:

    SELECT uid, l2_distance(short_feature, '[1,1,1,1]') as dis FROM tbl_vector WHERE xid = 5 AND cid = 4 ORDER BY 2 LIMIT 4;

    返回结果:

    +-------+--------------+
    | uid   |   dis        |
    +-------+--------------+
    | s     |   20.0       |
    +-------+--------------+
    | x     |   31.0       |
    +-------+--------------+
    | j     |   36.0       |
    +-------+--------------+
    | j     |   68.0       |
    +-------+--------------+
  • 查询short_feature与向量'[1,1,1,1]'距离最近的3条记录,按距离排序,且距离不能超过50:

    SELECT uid, l2_distance(short_feature, '[1,1,1,1]') as dis FROM tbl_vector WHERE l2_distance(short_feature, '[1,1,1,1]') < 50.0 AND xid = 5 ORDER BY 2 LIMIT 3;

    返回结果:

    +-------+---------------+
    | uid   |   dis         |
    +-------+---------------+
    | s     |   20.0        |
    +-------+---------------+
    | x     |   31.0        |
    +-------+---------------+
    | j     |   36.0        |
    +-------+---------------+

  • 本页导读 (1)
文档反馈