Skip to content

Commit 6aadb40

Browse files
authored
docs: add vector index documentation for CREATE/DROP/REFRESH commands (#3057)
1 parent 795ad1d commit 6aadb40

File tree

10 files changed

+604
-0
lines changed

10 files changed

+604
-0
lines changed
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
{
2+
"label": "Vector Index",
3+
"position": 12
4+
}
Lines changed: 197 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,197 @@
1+
---
2+
title: CREATE VECTOR INDEX
3+
sidebar_position: 1
4+
---
5+
6+
import FunctionDescription from '@site/src/components/FunctionDescription';
7+
8+
<FunctionDescription description="引入或更新于:v1.2.777"/>
9+
10+
为表的 [VECTOR](/sql/sql-reference/data-types/vector) 列创建 Vector Index,使用 HNSW(Hierarchical Navigable Small World)算法实现高效的相似度搜索。
11+
12+
## 语法
13+
14+
```sql
15+
-- 在现有表上创建 Vector Index
16+
CREATE [OR REPLACE] VECTOR INDEX [IF NOT EXISTS] <index_name>
17+
ON [<database>.]<table_name>(<column>)
18+
distance = '<metric>' [m = <number>] [ef_construct = <number>]
19+
20+
-- 在创建表时创建 Vector Index
21+
CREATE [OR REPLACE] TABLE <table_name> (
22+
<column_definitions>,
23+
VECTOR INDEX <index_name> (<column>)
24+
distance = '<metric>' [m = <number>] [ef_construct = <number>]
25+
)...
26+
```
27+
28+
### 参数说明
29+
30+
- **`distance`**(必需)- 指定用于相似度搜索的距离度量。支持多个度量,用逗号分隔:
31+
- `'cosine'` - 余弦距离(适用于语义相似度、文本 embedding)
32+
- `'l1'` - L1 距离 / 曼哈顿距离(适用于特征对比、稀疏数据)
33+
- `'l2'` - L2 距离 / 欧几里得距离(适用于几何相似度、图像特征)
34+
- 示例:`distance = 'cosine,l1,l2'` 同时支持三种度量
35+
36+
- **`m`**(可选,默认:16)- 控制 HNSW 图中每个节点的双向连接数:
37+
- 更高的值会增加内存使用,但可以提高搜索准确性
38+
- 必须大于 0
39+
- 典型取值范围:8-64
40+
41+
- **`ef_construct`**(可选,默认:100)- 控制索引构建过程中动态候选列表的大小:
42+
- 更高的值能提升索引质量,但会增加构建时间和内存消耗
43+
- 必须 >= 40
44+
- 典型取值范围:40-500
45+
46+
## Vector Index 工作原理
47+
48+
Databend 的 Vector Index 使用 HNSW 算法构建多层图结构:
49+
50+
1. **图结构**:每个向量是一个节点,与其最近邻节点相连
51+
2. **搜索过程**:查询从粗粒度到细粒度遍历图的各层,快速找到近似最近邻
52+
3. **量化处理**:原始向量会被量化以减少存储空间并提升查询性能(精度损失可忽略不计)
53+
4. **自动构建**:索引随数据写入自动构建。每次 INSERT、COPY 或数据加载操作都会自动为新行生成索引,无需手动维护
54+
55+
## 示例
56+
57+
### 创建带 Vector Index 的表
58+
59+
```sql
60+
-- 为 embedding 创建简单的 Vector Index
61+
CREATE TABLE documents (
62+
id INT,
63+
title VARCHAR,
64+
content TEXT,
65+
embedding VECTOR(1024),
66+
VECTOR INDEX idx_embedding(embedding) distance = 'cosine'
67+
);
68+
```
69+
70+
### 创建带自定义参数的 Vector Index
71+
72+
```sql
73+
-- 支持多种距离度量并调整参数的 Vector Index
74+
CREATE TABLE images (
75+
id INT,
76+
filename VARCHAR,
77+
feature_vector VECTOR(512),
78+
VECTOR INDEX idx_features(feature_vector)
79+
distance = 'cosine,l2'
80+
m = 32
81+
ef_construct = 200
82+
);
83+
```
84+
85+
### 在已有表上创建 Vector Index
86+
87+
```sql
88+
CREATE TABLE products (
89+
id INT,
90+
name VARCHAR,
91+
description TEXT,
92+
embedding VECTOR(768)
93+
);
94+
95+
-- 表创建后添加 Vector Index
96+
CREATE VECTOR INDEX idx_product_embedding
97+
ON products(embedding)
98+
distance = 'cosine,l1,l2'
99+
m = 20
100+
ef_construct = 150;
101+
```
102+
103+
### 在不同列上创建多个 Vector Index
104+
105+
```sql
106+
CREATE TABLE multimodal_data (
107+
id INT,
108+
text_embedding VECTOR(384),
109+
image_embedding VECTOR(512),
110+
VECTOR INDEX idx_text(text_embedding) distance = 'cosine',
111+
VECTOR INDEX idx_image(image_embedding) distance = 'l2'
112+
);
113+
```
114+
115+
### 查看索引
116+
117+
使用 [SHOW INDEXES](/sql/sql-commands/administration-cmds/show-indexes) 查看所有索引:
118+
119+
```sql
120+
SHOW INDEXES;
121+
```
122+
123+
结果:
124+
```
125+
┌──────────────────────┬────────┬──────────┬────────────────────────────┬──────────────────────────┐
126+
│ name │ type │ original │ definition │ created_on │
127+
├──────────────────────┼────────┼──────────┼────────────────────────────┼──────────────────────────┤
128+
│ idx_embedding │ VECTOR │ │ documents(embedding) │ 2025-05-13 01:22:34.123 │
129+
│ idx_product_embedding│ VECTOR │ │ products(embedding) │ 2025-05-13 01:23:45.678 │
130+
└──────────────────────┴────────┴──────────┴────────────────────────────┴──────────────────────────┘
131+
```
132+
133+
### 使用 Vector Index 进行相似度搜索
134+
135+
```sql
136+
-- 创建带 Vector Index 的表
137+
CREATE TABLE wiki_articles (
138+
id INT,
139+
title VARCHAR,
140+
embedding VECTOR(8),
141+
VECTOR INDEX idx_embedding(embedding) distance = 'cosine'
142+
);
143+
144+
-- 插入示例数据(为演示使用 8 维向量)
145+
INSERT INTO wiki_articles VALUES
146+
(1, 'Machine Learning', [0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8]),
147+
(2, 'Deep Learning', [0.15, 0.25, 0.35, 0.45, 0.55, 0.65, 0.75, 0.85]),
148+
(3, 'Natural Language Processing', [0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9]),
149+
(4, 'Computer Vision', [0.8, 0.7, 0.6, 0.5, 0.4, 0.3, 0.2, 0.1]);
150+
151+
-- 使用余弦距离查找与查询向量最相似的 2 篇文章
152+
SELECT id, title, cosine_distance(embedding, [0.12, 0.22, 0.32, 0.42, 0.52, 0.62, 0.72, 0.82]) AS distance
153+
FROM wiki_articles
154+
ORDER BY distance ASC
155+
LIMIT 2;
156+
```
157+
158+
结果:
159+
```
160+
┌────┬─────────────────┬──────────────┐
161+
│ id │ title │ distance │
162+
├────┼─────────────────┼──────────────┤
163+
│ 1 │ Machine Learning│ 0.00012345 │
164+
│ 2 │ Deep Learning │ 0.00023456 │
165+
└────┴─────────────────┴──────────────┘
166+
```
167+
168+
## 性能调优
169+
170+
### 选择距离度量
171+
172+
根据你的使用场景选择合适的距离度量。查询时使用距离函数,参见 [Vector 函数](/sql/sql-functions/vector-functions/)
173+
174+
- **余弦距离(Cosine)**:适用于 BERT、GPT 等模型生成的文本 embedding,向量长度不重要的场景
175+
- **L2 距离(欧几里得)**:适用于图像特征、空间数据等关注绝对差异的场景
176+
- **L1 距离(曼哈顿)**:适用于稀疏向量,以及希望强调单个维度差异的场景
177+
178+
### 调整 HNSW 参数
179+
180+
| 参数 | 较低值 | 较高值 |
181+
|----------------|--------------------------------------|--------------------------------------|
182+
| `m` | 内存占用少,构建快 | 准确性高,内存占用多 |
183+
| `ef_construct` | 构建快,质量低 | 质量高,构建慢 |
184+
185+
**推荐配置:**
186+
187+
- **小数据集(< 10万向量)**:使用默认设置(`m=16`, `ef_construct=100`
188+
- **中等数据集(10万 - 100万向量)**`m=24`, `ef_construct=150`
189+
- **大数据集(> 100万向量)**`m=32`, `ef_construct=200`
190+
- **高精度需求**`m=48`, `ef_construct=300`
191+
192+
## 限制
193+
194+
- Vector Index 仅支持 [VECTOR](/sql/sql-reference/data-types/vector) 数据类型的列
195+
- `distance` 参数为必需,未指定的索引将被忽略
196+
- 量化处理可能引入极小的距离计算误差(通常 < 0.01%)
197+
- 索引大小随 `m` 值增加而增长(约为每个向量 `m * 向量维度 * 4 字节`
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
---
2+
title: DROP VECTOR INDEX
3+
sidebar_position: 3
4+
---
5+
6+
import FunctionDescription from '@site/src/components/FunctionDescription';
7+
8+
<FunctionDescription description="引入或更新于:v1.2.777"/>
9+
10+
从表中删除 Vector Index。
11+
12+
## 语法
13+
14+
```sql
15+
DROP VECTOR INDEX [IF EXISTS] <index_name> ON [<database>.]<table_name>
16+
```
17+
18+
## 示例
19+
20+
```sql
21+
-- 创建带 Vector Index 的表
22+
CREATE TABLE articles (
23+
id INT,
24+
title VARCHAR,
25+
embedding VECTOR(768),
26+
VECTOR INDEX idx_embedding(embedding) distance = 'cosine'
27+
);
28+
29+
-- 删除 Vector Index
30+
DROP VECTOR INDEX idx_embedding ON articles;
31+
32+
-- 使用 IF EXISTS 避免索引不存在时报错
33+
DROP VECTOR INDEX IF EXISTS idx_embedding ON articles;
34+
```
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
---
2+
title: Vector Index
3+
---
4+
5+
Databend 的 Vector Index 基于 HNSW(Hierarchical Navigable Small World)算法,能够对高维向量数据进行高效的相似度搜索,支持语义搜索、推荐系统和 AI 应用等场景。
6+
7+
:::tip 核心特性:自动构建索引
8+
Vector Index **随数据写入自动构建**。当你向包含 Vector Index 的表中插入或加载数据时,索引会自动生成,无需手动维护。只有在对已有数据的表创建索引时,才需要运行 `REFRESH VECTOR INDEX`
9+
:::
10+
11+
## Vector Index 管理
12+
13+
| 命令 | 说明 |
14+
|-------------------------------------------------|----------------------------------------------------|
15+
| [CREATE VECTOR INDEX](create-vector-index.md) | 创建 Vector Index 以实现高效相似度搜索 |
16+
| [REFRESH VECTOR INDEX](refresh-vector-index.md) | 为创建索引前已存在的数据构建索引 |
17+
| [DROP VECTOR INDEX](drop-vector-index.md) | 删除 Vector Index |
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
---
2+
title: REFRESH VECTOR INDEX
3+
sidebar_position: 2
4+
---
5+
6+
import FunctionDescription from '@site/src/components/FunctionDescription';
7+
8+
<FunctionDescription description="引入或更新于:v1.2.777"/>
9+
10+
为创建索引前已存在的数据构建 Vector Index。
11+
12+
## 语法
13+
14+
```sql
15+
REFRESH VECTOR INDEX <index_name> ON [<database>.]<table_name>
16+
```
17+
18+
## 何时使用 REFRESH
19+
20+
`REFRESH VECTOR INDEX` **仅在一种特定场景下使用**:在已包含数据的表上创建 Vector Index。
21+
22+
对于创建索引前写入的数据,不会自动建立索引。你必须运行 `REFRESH VECTOR INDEX` 为这些已有数据构建索引。刷新完成后,所有后续的数据写入都会自动生成索引。
23+
24+
## 示例
25+
26+
### 示例:为已有数据建立索引
27+
28+
```sql
29+
-- 步骤 1:创建不带索引的表
30+
CREATE TABLE products (
31+
id INT,
32+
name VARCHAR,
33+
embedding VECTOR(4)
34+
) ENGINE = FUSE;
35+
36+
-- 步骤 2:插入数据(此时无索引)
37+
INSERT INTO products VALUES
38+
(1, 'Product A', [0.1, 0.2, 0.3, 0.4]),
39+
(2, 'Product B', [0.5, 0.6, 0.7, 0.8]),
40+
(3, 'Product C', [0.9, 1.0, 1.1, 1.2]);
41+
42+
-- 步骤 3:在已有数据上创建 Vector Index
43+
CREATE VECTOR INDEX idx_embedding ON products(embedding) distance='cosine';
44+
45+
-- 步骤 4:刷新以为现有的 3 行数据构建索引
46+
REFRESH VECTOR INDEX idx_embedding ON products;
47+
48+
-- 步骤 5:新插入的数据会自动建立索引(无需刷新)
49+
INSERT INTO products VALUES (4, 'Product D', [1.3, 1.4, 1.5, 1.6]);
50+
```
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
{
2+
"label": "Vector Index",
3+
"position": 12
4+
}

0 commit comments

Comments
 (0)