diff --git "a/content/zh/docs/ExtensionReference/datavec\344\275\277\347\224\250.md" "b/content/zh/docs/ExtensionReference/datavec\344\275\277\347\224\250.md" index 1e15cd95aa85542b06128e582c0340b37f4046d3..674d1baa1e5044fa844a9c5ca992340a488bda0e 100644 --- "a/content/zh/docs/ExtensionReference/datavec\344\275\277\347\224\250.md" +++ "b/content/zh/docs/ExtensionReference/datavec\344\275\277\347\224\250.md" @@ -22,14 +22,14 @@ CREATE TABLE [TABLE_NAME] ); ``` -示例1:创建一个带有3维向量的表。 +**示例1:** 创建一个带有3维向量的表。 ``` openGauss=# CREATE TABLE items (val vector(3)); ``` -### 数据插入 +### 向量存储 向量的数据插入与openGauss原生语法保持一致,使用INSERT或者COPY插入,指定数据类型即可。 @@ -42,40 +42,109 @@ INSERT INTO [TABLE_NAME] VALUES ); ``` -示例2:向量数据的插入。 +**示例2:** 插入向量数据。 ``` openGauss=# INSERT INTO items (val) VALUES ('[1,2,3]'), ('[4,5,6]'); +openGauss=# COPY items (val) FROM STDIN WITH (FORMAT BINARY); +``` + +### 向量索引 +DataVec目前支持了[IVFFLAT](#IVFFlat)、[HNSW](#HNSW)及HNSWPQ等算法,基于openGauss中的ASTORE和USTORE存储实现,通过索引结构能过够高效地检索出查询结果。 + +#### HNSW +``` +CREATE INDEX [INDFEX_NAME] +ON [TABLE_NAME] +USING hnsw (COLUMN_NAME [TYPE]_[DISTANCE_FUN]_ops) +with (m=, ef_construction=); ``` -### 向量索引创建 +支持向量类型: +- `vector` - 最大支持2000维向量 +- `bit` - 最大支持64000维向量 +- `sparsevec` - 最多1000非零元素 -DataVec目前支持了IVFFLAT、HNSW及HNSWPQ等算法,基于openGauss中的ASTORE和USTORE存储实现,通过索引结构能过够高效地检索出查询结果。 +支持索引距离函数: +- `l2` +- `ip` +- `cosine` +- `l1` +- `hamming` +- `jaccard` +具体索引操作符根据[向量数据类型](#向量数据类型)中所支持的距离函数而定,格式为`[TYPE]_[DISTANCE_FUN]_ops`。 + +##### 索引选项 +- `m` - 每个图层最大连接数 (默认为16) +- `ef_construction` - 用于图形构造的动态候选集大小 (默认为64) + +**示例3:** 使用L2距离计算创建HNSW索引并设置`m = 16, ef_construction = 64`。 ``` -CREATE INDEX [INDFEX_NAME] -ON [TABLE_NAME] -USING [ivfflat|hnsw|...] -WITH ( - lists=,| - m=, - ef_construction=, - ... -); +openGauss=# CREATE INDEX ON items USING hnsw (embedding vector_l2_ops) WITH (m = 16, ef_construction = 64); ``` +提高 `ef_construction` 可以提供更好的召回率,但会增加索引构建和插入速度。 -示例3:索引创建。 +##### 查询选项 +- `ef_search` - 查询时的动态候选集大小(默认为40) +**示例4:** 设置当前回话中`ef_search=100`。 ``` -openGauss=# CREATE INDEX ON items USING ivfflat (val vector_l2_ops) WITH (lists = 100); -openGauss=# CREATE INDEX ON items USING ivfflat (val vector_ip_ops) WITH (lists = 100); -openGauss=# CREATE INDEX ON items USING ivfflat (val vector_cosine_ops) WITH (lists = 100); -openGauss=# CREATE INDEX ON items USING hnsw (val vector_cosine_ops) WITH (m = 16); +openGauss=# SET hnsw.ef_search = 100; ``` ->![](public_sys-resources/icon-note.png) **说明:** + +#### IVFFlat +``` +CREATE INDEX [INDFEX_NAME] +ON [TABLE_NAME] +USING ivfflat (COLUMN_NAME OPERATORNAME) +WITH (lists = ); +``` + +支持向量类型: +- `vector` - 最大支持2000维向量 +- `bit` - 最大支持64000维向量 + +支持索引距离函数: +- `l2` +- `ip` +- `cosine` +- `hamming` + +具体索引操作符根据[向量数据类型](#向量数据类型)中所支持的距离函数而定,格式为`[TYPE]_[DISTANCE_FUN]_ops`。 + +##### 索引选项 +- `lists` - 倒排表(单元格)聚类聚类中心数量 (默认为100) + +**示例5:** 使用L2距离计算创建IVFFlat索引并设置lists = 200。 +``` +CREATE INDEX ON items USING ivfflat (embedding vector_l2_ops) WITH (lists = 200); +``` +>![](public_sys-resources/icon-note.png) **说明:** > ->索引最大支持2000维向量。 +>优化召回率: +> - 在表中有数据后创建索引。 +> - 选取适当的`lists`, 建议当数据集在100万行及以内:`rows / 1000`;超过100万行的数据:`sqrt(rows)`。 +> - 查询时指定适当数量的probe,建议`sqrt(lists)`。 + +##### 查询选项 +- `probe` - 查询时候选集的大小 (默认为1) +``` +openGauss=# SET ivfflat.probes = 10; +``` + +#### 索引构建时间 +通过开启并行构建功能来加速向量索引的创建: +``` +ALTER TABLE [TABLE_NAME] +SET (parallel_workers = ); +``` + +**示例6:** 构建索引并行数设置为8。 +``` +ALTER TABLE items SET (parallel_workers = 8); +``` ### 向量检索 通过ANN索引,DataVec可以进行高效的近似搜索;此外还可以进行非索引的精确检索。 @@ -87,14 +156,135 @@ ORDER BY COLN [VECTOR_OPERATER] '[0.1, 0.3, 0.7, ...]' LIMIT ; ``` -示例4:计算最近邻。 +支持的距离计算公式有: + +- `<->` - L2 距离 +- `<#>` - (负)内积 +- `<=>` - 余弦距离 +- `<+>` - L1 距离 + +**示例7:** 计算最近邻。 ``` openGauss=# SELECT * FROM items ORDER BY val <-> '[3,1,2]' LIMIT 5; openGauss=# SELECT * FROM items ORDER BY val <#> '[3,1,2]' LIMIT 5; openGauss=# SELECT * FROM items ORDER BY val <=> '[3,1,2]' LIMIT 5; +openGauss=# SELECT * FROM items ORDER BY val <+> '[3,1,2]' LIMIT 5; +``` + +#### 距离计算 +``` +SELECT VECTOR1 [VECTOR_OPERATER] VECTOR1 +AS DISTANCE +FROM [TABLE_NAME]; +``` + +**示例8:** 计算表中向量与'[3,1,2]'的距离。 +``` +openGauss=# SELECT val <-> '[3,1,2]' AS distance FROM items; +``` + +#### 聚合计算 +可以对向量执行聚合计算: +``` +SELECT AGGREGATE_FUNCION(COL) +FROM [TABLE_NAME]; ``` +**示例9:** 计算向量平均值。 +```sql +SELECT AVG(embedding) FROM items; +``` + +### 向量数据类型 + +- [Vector](#Vector) +- [Bit](#Bit) +- [Sparsevec](#Sparsevec) + +#### Vector +每个vector占用 `4 * dimensions + 8` 字节的储存空间,每个元素都是单精度浮点数,并且所有的元素都是有限的: +(不能有 `NaN`,`Infinity` 或者 `-Infinity`)。 +vector的最大维度为 16,000。 + +##### Vector 操作符 +操作符 | 描述 +--- | --- +\+ | element-wise addition +\- | element-wise subtraction +\* | element-wise multiplication +\|\| | concatenate +<-> | Euclidean distance +<#> | negative inner product +<=> | cosine distance +<+> | taxicab distance + +##### Vector 函数 + +函数 | 描述 +--- | --- +binary_quantize(vector) → bit | binary quantize +cosine_distance(vector, vector) → double precision | cosine distance +inner_product(vector, vector) → double precision | inner product +l1_distance(vector, vector) → double precision | taxicab distance +l2_distance(vector, vector) → double precision | Euclidean distance +l2_normalize(vector) → vector | Normalize with Euclidean norm +subvector(vector, integer, integer) → vector | subvector +vector_dims(vector) → integer | number of dimensions +vector_norm(vector) → double precision | Euclidean norm + +##### Vector 聚合函数 + +函数 | 描述 +--- | --- +avg(vector) → vector | average +sum(vector) → vector | sum + +#### Bit +每个bit向量占用`维度 / 8 + 8`字节的存储空间。 + +##### Bit 操作符 + +操作符 | 描述 +--- | --- +<~> | Hamming distance +<%> | Jaccard distance + +##### Bit 函数 + +函数 | 描述 +--- | --- +hamming_distance(bit, bit) → double precision | Hamming distance +jaccard_distance(bit, bit) → double precision | Jaccard distance + + +#### Sparsevec +每个sparse向量占用`8 * 非零元素数 + 16`字节的存储空间。 +每个元素都是单精度浮点数,并且所有的元素都是有限的: +(不能有 `NaN`,`Infinity` 或者 `-Infinity`)。 +Sparse的最大非零元素数为16,000。 + +##### Sparsevec 操作符 + +操作符 | 描述 +--- | --- +<-> | Euclidean distance +<#> | negative inner product +<=> | cosine distance +<+> | taxicab distance + +##### Sparsevec 函数 + +函数 | 描述 +--- | --- +cosine_distance(sparsevec, sparsevec) → double precision | cosine distance +inner_product(sparsevec, sparsevec) → double precision | inner product +l1_distance(sparsevec, sparsevec) → double precision | taxicab distance +l2_distance(sparsevec, sparsevec) → double precision | Euclidean distance +l2_norm(sparsevec) → double precision | Euclidean norm +l2_normalize(sparsevec) → sparsevec | Normalize with Euclidean norm + + ## 删除Extension 在openGauss中删除DataVec Extension的方法如下所示: diff --git a/content/zh/menu/index.md b/content/zh/menu/index.md index cfadb4f8ede625fe2ae1de1da10b059b57537005..9ae73cef2869a25dfa315b2cfa40ca6577c52ebe 100644 --- a/content/zh/menu/index.md +++ b/content/zh/menu/index.md @@ -1169,6 +1169,7 @@ headless: true - [DataVec限制]({{< relref "./docs/ExtensionReference/datavec限制.md" >}}) - [DataVec安装]({{< relref "./docs/ExtensionReference/datavec安装.md" >}}) - [DataVec使用]({{< relref "./docs/ExtensionReference/datavec使用.md" >}}) + - [DataVec容器化部署]({{< relref "./docs/ExtensionReference/datavec容器化部署.md" >}}) - [gms_profiler Extension]({{< relref "./docs/ExtensionReference/gms_profiler-Extension.md" >}}) - [gms_profiler概述]({{< relref "./docs/ExtensionReference/gms_profiler概述.md" >}}) - [gms_profiler限制]({{< relref "./docs/ExtensionReference/gms_profiler限制.md" >}}) diff --git a/sphinx/source/ExtensionReference/datavec-Extension.rst b/sphinx/source/ExtensionReference/datavec-Extension.rst index abaa3152985047514ae73911efa0ab8c123986d8..95ae5bcd22d95d0c51f0fd0a3295b0d6aed7947b 100644 --- a/sphinx/source/ExtensionReference/datavec-Extension.rst +++ b/sphinx/source/ExtensionReference/datavec-Extension.rst @@ -6,4 +6,5 @@ datavec-Extension ../content/zh/docs/ExtensionReference/datavec概述 ../content/zh/docs/ExtensionReference/datavec安装 ../content/zh/docs/ExtensionReference/datavec使用 - ../content/zh/docs/ExtensionReference/datavec限制 \ No newline at end of file + ../content/zh/docs/ExtensionReference/datavec限制 + ../content/zh/docs/ExtensionReference/datavec容器化部署 \ No newline at end of file