Skip to content

Commit d060aad

Browse files
authored
minor (#265)
1 parent 7de49c6 commit d060aad

File tree

2 files changed

+35
-4
lines changed

2 files changed

+35
-4
lines changed

zh_CN/sql-reference/vector-functions.md

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@ $$
1818
\text{l2\_distance}(\mathbf{x}, \mathbf{y}) = \sqrt{\sum_{i=1}^n (x_i - y_i)^2}
1919
$$
2020

21+
注意,由于开根并不影响相似度的比较,因此为了性能考虑,Datalayers 在计算 L2 距离时省去了开根操作。
22+
2123
语法:
2224

2325
```sql
@@ -39,12 +41,16 @@ SELECT * FROM t ORDER BY l2_distance(embed, [1.0, 2.0, 3.0]) LIMIT 1;
3941

4042
### 余弦距离函数
4143

44+
余弦距离通常用于对向量的模不敏感的场景。
45+
4246
计算公式为:
4347

4448
$$
4549
\text{cosine\_distance}(\mathbf{x}, \mathbf{y}) = 1 - \frac{\mathbf{x} \cdot \mathbf{y}}{\|\mathbf{x}\|_2 \cdot \|\mathbf{y}\|_2} = 1 - \frac{\sum_{i=1}^n x_i y_i}{\sqrt{\sum_{i=1}^n x_i^2} \cdot \sqrt{\sum_{i=1}^n y_i^2}}
4650
$$
4751

52+
注意,Datalayers 根据向量检索的传统,用 `1` 减去余弦距离,将余弦距离从 `[-1, 1]` 正则化到 `[0, 2]`。这使得余弦距离越小,向量相似度越高。
53+
4854
语法:
4955

5056
```sql
@@ -55,19 +61,22 @@ cosine_distance(x, y)
5561

5662
### 负内积距离函数
5763

58-
向量的内积(Dot Product,又称 Inner Product),定义为两个向量逐个元素的乘积之和。负内积,则为内积的负。之所以使用负内积,
59-
是因为我们希望使得所有向量距离函数,均满足距离越小、向量相似度越大的关系。
64+
向量的内积(Dot Product,又称 Inner Product),定义为两个向量逐个元素的乘积之和。负内积,由 `1` 减去内积所得到。
6065

6166
计算公式为:
6267

6368
$$
64-
\text{dot\_distance}(\mathbf{x}, \mathbf{y}) = -\mathbf{x} \cdot \mathbf{y} = -\sum_{i=1}^n x_i y_i
69+
\text{dot\_distance}(\mathbf{x}, \mathbf{y}) = 1 - \mathbf{x} \cdot \mathbf{y} = 1 - \sum_{i=1}^n x_i y_i
6570
$$
6671

72+
注意,当输入为归一化的向量时,负内积距离等于正则化后的余弦距离,因此负内积距离越小,向量相似度越高。
73+
当输入为未归一化的向量时,负内积距离受向量的模长影响,负内积距离越大,向量相似度越高。
74+
建议仅针对归一化的向量使用负内积距离,以获得更好的性能。
75+
6776
语法:
6877

6978
```sql
70-
l2_distance(x, y)
79+
dot_distance(x, y)
7180
```
7281

7382
示例:参考 L2 距离函数的示例。

zh_CN/vector-search/vector-index.md

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
# 向量索引技术指南
22

33
## 概述
4+
45
向量索引是加速大规模向量数据集检索的关键技术。Datalayers 支持多种向量索引类型,通过三层式架构实现高效的近似最近邻搜索。
56
使用向量索引会带来额外的构建索引开销、检索开销,同时可能会降低召回率(recall),因此需要根据具体场景选择合适的索引、以及配置合适的参数。
67

@@ -54,6 +55,27 @@ Vector Store 是向量的存储抽象。为了节省存储空间,我们支持
5455
- SQ 指 Scalar Quantization,即标量量化。
5556
- RQ 指 RaBit Quantization。
5657

58+
## 语法
59+
60+
Datalayers 支持在建表时为向量列指定向量索引,语法为:
61+
62+
``` sql
63+
VECTOR INDEX <index_name>(<vector_col_name>) [ WITH (<vector_index_options>) ]
64+
```
65+
66+
例如,在创建表 `t` 时,给 `embed` 向量列指定一个向量索引,命名为 `my_vector_index`,同时将其类型设置为 `IVF_PQ`,距离函数设置为 `L2`
67+
68+
``` sql
69+
CREATE TABLE `t` (
70+
`ts` TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
71+
`sid` INT32 NOT NULL,
72+
`embed` VECTOR(64),
73+
TIMESTAMP KEY(`ts`),
74+
VECTOR INDEX `my_vector_index`(`embed`) WITH (TYPE=IVF_PQ, DISTANCE=L2)
75+
)
76+
PARTITION BY HASH (`sid`) PARTITIONS 1
77+
```
78+
5779
## 示例
5880

5981
我们提供了一个 Python 脚本,展示如何使用向量索引来加速向量检索。这个脚本执行的步骤如下:

0 commit comments

Comments
 (0)