Skip to content

Commit

Permalink
Merge branch 'VectorFunction-and-VectorIndex' of https://github.com/E…
Browse files Browse the repository at this point in the history
…ricZequan/docs-cn into pr/18502
  • Loading branch information
qiancai committed Sep 14, 2024
2 parents 5eeb336 + 4b54e6d commit c6dad29
Show file tree
Hide file tree
Showing 7 changed files with 61 additions and 7 deletions.
9 changes: 7 additions & 2 deletions vector-search-data-types.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ summary: 本文介绍 TiDB 的向量数据类型。

与使用 [`JSON`](/data-type-json.md) 类型相比,使用向量类型具有以下优势:

- 支持向量索引。 可以通过构建[向量搜索索引](/vector-search-index.md)加速查询。
- 可指定维度。指定一个固定维度后,不符合维度的数据将被阻止写入到表中。
- 存储格式更优。向量数据类型针对向量数据进行了特别优化,在空间利用和性能效率上都优于 `JSON` 类型。

Expand Down Expand Up @@ -52,7 +53,8 @@ ERROR 1105 (HY000): Invalid vector text: [5, ]
ERROR 1105 (HY000): vector has 2 dimensions, does not fit VECTOR(3)
```

可参阅[向量函数与操作符](/vector-search-functions-and-operators.md)了解向量数据类型支持的所有函数和操作符。
可参阅 [向量函数与操作符](/vector-search-functions-and-operators.md) 了解向量数据类型支持的所有函数和操作符。
可参阅 [向量搜索索引](/vector-search-index.md) 了解向量搜索索引的信息。

## 混合存储不同维度的向量

Expand All @@ -68,6 +70,8 @@ INSERT INTO vector_table VALUES (1, '[0.3, 0.5, -0.1]'); -- 3 dimensions vector,
INSERT INTO vector_table VALUES (2, '[0.3, 0.5]'); -- 2 dimensions vector, OK
```

但是,我们不能为存储了不同维度的向量列构建 [向量搜索索引](/vector-search-index.md),因为向量距离只能在具有相同维度的向量之间计算。

## 比较

[比较运算符](/vector-search-functions-and-operators.md#扩展的内置函数和运算符)`=`, `!=`, `<`, `>`, `<=``>=` 等都能正常对向量数据进行比较。可参阅[向量函数与操作符](/vector-search-functions-and-operators.md#扩展的内置函数和运算符)了解向量数据类型支持的所有函数和操作符。
Expand Down Expand Up @@ -239,4 +243,5 @@ ERROR 1105 (HY000): vectors have different dimensions: 1 and 3

## 另请参阅

- [向量函数和操作符](/vector-search-functions-and-operators.md)
- [向量函数和操作符](/vector-search-functions-and-operators.md)
- [向量搜索索引](/vector-search-index.md)
2 changes: 1 addition & 1 deletion vector-search-improve-performance.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ summary: 了解优化 TiDB 向量搜索性能的最佳实践。

## 减少向量维数或缩短嵌入时间

随着向量大小的增加,向量搜索索引和查询的计算复杂度会显著增加,因为这意味着要进行更多的浮点数比较运算。
随着向量维度大小的增加,向量搜索索引和查询的计算复杂度会显著增加,因为这意味着要进行更多的浮点数比较运算。

为了优化性能,可以考虑尽可能地减少向量的维数。这通常需要切换到另一种嵌入模型。在切换模型时,你需要确保改变嵌入模型对向量查询准确性的影响。

Expand Down
4 changes: 2 additions & 2 deletions vector-search-index.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ TiDB 目前支持以下向量搜索索引算法:
id INT PRIMARY KEY,
data VECTOR(5),
data64 VECTOR64(10),
VECTOR INDEX data USING HNSW ((VEC_COSINE_DISTANCE(data)))
VECTOR INDEX idx_data USING HNSW ((VEC_COSINE_DISTANCE(data)))
);
```

Expand Down Expand Up @@ -122,7 +122,7 @@ CREATE TABLE docs (
ver VARCHAR(10),
doc TEXT,
embedding VECTOR(3),
VECTOR INDEX embedding USING HNSW ((VEC_COSINE_DISTANCE(embedding)))
VECTOR INDEX idx_embedding USING HNSW ((VEC_COSINE_DISTANCE(embedding)))
) PARTITION BY LIST COLUMNS (ver) (
PARTITION p_v1_0 VALUES IN ('v1.0'),
PARTITION p_v1_1 VALUES IN ('v1.1'),
Expand Down
19 changes: 18 additions & 1 deletion vector-search-integrate-with-django-orm.md
Original file line number Diff line number Diff line change
Expand Up @@ -225,6 +225,22 @@ Document.objects.create(content="fish", embedding=[1, 2, 4])
Document.objects.create(content="tree", embedding=[1, 0, 0])
```

#### 用索引定义优化的向量列

定义三维向量列,并使用 [向量搜索索引 (HNSW 索引)](/vector-search-index.md) 对其进行优化。

```python
class DocumentWithIndex(models.Model):
content = models.TextField()
# Note:
# - Using comment to add hnsw index is a temporary solution. In the future it will use `CREATE INDEX` syntax.
# - Currently the HNSW index cannot be changed after the table has been created.
# - Only Django >= 4.2 supports `db_comment`.
embedding = VectorField(dimensions=3, db_comment="VECTOR INDEX idx_embedding USING HNSW ((VEC_COSINE_DISTANCE(embedding)))")
```

TiDB 将使用该索引来加速基于余弦距离函数的向量搜索查询。

### 搜索近邻向量

TiDB 向量支持以下距离函数:
Expand Down Expand Up @@ -254,4 +270,5 @@ results = Document.objects.annotate(

## 另请参阅

- [向量数据类型](/vector-search-data-types.md)
- [向量数据类型](/vector-search-data-types.md)
- [向量搜索索引](/vector-search-index.md)
19 changes: 18 additions & 1 deletion vector-search-integrate-with-peewee.md
Original file line number Diff line number Diff line change
Expand Up @@ -223,6 +223,22 @@ Document.create(content='fish', embedding=[1, 2, 4])
Document.create(content='tree', embedding=[1, 0, 0])
```

#### 用索引定义优化的向量列

定义三维矢量列,并使用 [向量搜索索引](/vector-search-index.md) (HNSW 索引) 对其进行优化。

```python
class DocumentWithIndex(Model):
class Meta:
database = db
table_name = 'peewee_demo_documents_with_index'

content = TextField()
embedding = VectorField(3, constraints=[SQL("VECTOR INDEX idx_embedding USING HNSW ((VEC_COSINE_DISTANCE(embedding)))")])
```

TiDB 将使用该索引来加速基于余弦距离函数的向量搜索查询。

### 搜索近邻向量

可以选择使用余弦距离 (`CosineDistance`) 函数,查询与向量 `[1, 2, 3]` 语义最接近的前 3 个 `document`
Expand All @@ -244,4 +260,5 @@ results = Document.select(Document, distance).where(distance_expression < 0.2).o

## 另请参阅

- [向量数据类型](/vector-search-data-types.md)
- [向量数据类型](/vector-search-data-types.md)
- [向量搜索索引](/vector-search-index.md)
14 changes: 14 additions & 0 deletions vector-search-integrate-with-sqlalchemy.md
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,20 @@ with Session(engine) as session:
session.commit()
```

#### 用索引定义优化的矢量列

定义三维矢量列,并使用 [向量量搜索索引](/vector-search-index.md) (HNSW 索引)对其进行优化。

```python
class DocumentWithIndex(Base):
__tablename__ = 'sqlalchemy_demo_documents_with_index'
id = Column(Integer, primary_key=True)
content = Column(Text)
embedding = Column(VectorType(3), comment="VECTOR INDEX idx_embedding USING HNSW ((VEC_COSINE_DISTANCE(embedding)))")
```

TiDB 将使用该索引来加速基于余弦距离函数的矢量搜索查询。

### 搜索近邻向量

可以选择使用余弦距离 (`CosineDistance`) 函数,查询与向量 `[1, 2, 3]` 语义最接近的前 3 个 `document`
Expand Down
1 change: 1 addition & 0 deletions vector-search-limitations.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ summary: 了解 TiDB 向量搜索功能的限制。

- 向量最大支持 16383 维。
- 向量数据中不支持 `NaN``Infinity``-Infinity` 浮点数。
- 创建 [向量搜索索引](/vector-search-index.md) 时只支持余弦距离和L2距离。
- 目前,向量数据类型不支持存储双精度浮点数(该功能计划在未来的版本中支持)。当向 TiDB 中的向量字段插入或存储数据时,如果这些数据的类型是双精度浮点数,TiDB 会将这些双精度浮点数自动转换为单精度浮点数。

## 反馈
Expand Down

0 comments on commit c6dad29

Please sign in to comment.