Skip to content
AI & Vectors

IVF平面索引

IVFFlat 是一种用于近似最近邻搜索的向量索引类型。这是一种常用的索引类型,在查询高维向量(比如表示嵌入的向量)时可以提高性能。

🌐 IVFFlat is a type of vector index for approximate nearest neighbor search. It is a frequently used index type that can improve performance when querying highly-dimensional vectors, like those representing embeddings.

选择一个索引 #

🌐 Choosing an index

今天 pgvector 支持两种类型的索引:

🌐 Today pgvector supports two types of indexes:

一般来说,我们推荐使用 HNSW,因为它的 性能对变化数据的鲁棒性。如果你有特殊的使用情况需要使用 IVFFlat,请继续阅读。

🌐 In general we recommend using HNSW because of its performance and robustness against changing data. If you have a special use case that requires IVFFlat instead, keep reading.

用法 #

🌐 Usage

你创建 IVFFlat 索引的方式取决于你使用的距离运算符。pgvector 包含 3 个距离运算符:

🌐 The way you create an IVFFlat index depends on the distance operator you are using. pgvector includes 3 distance operators:

运算符描述运算符类别
<->欧几里得距离vector_l2_ops
<#>负内积vector_ip_ops
<=>余弦距离vector_cosine_ops

使用以下 SQL 命令为你查询中使用的操作符创建一个 IVFFlat 索引。

🌐 Use the following SQL commands to create an IVFFlat index for the operator(s) used in your queries.

欧几里得 L2 距离(vector_l2_ops#

🌐 Euclidean L2 distance (vector_l2_ops)

1
create index on items using ivfflat (column_name vector_l2_ops) with (lists = 100);

内积 (vector_ip_ops#

🌐 Inner product (vector_ip_ops)

1
create index on items using ivfflat (column_name vector_ip_ops) with (lists = 100);

余弦距离 (vector_cosine_ops#

🌐 Cosine distance (vector_cosine_ops)

1
create index on items using ivfflat (column_name vector_cosine_ops) with (lists = 100);

目前最多可以对 2,000 维的向量进行索引。

🌐 Currently vectors with up to 2,000 dimensions can be indexed.

IVFFlat 是怎么运作的? #

🌐 How does IVFFlat work?

IVF 代表“倒排文件索引”。它的工作原理是通过聚类你的向量来减少相似性搜索的范围。与其将一个向量与每一个其他向量比较,不如只将向量与同一单元簇内(或根据你的配置,与附近簇内)的向量进行比较。

🌐 IVF stands for 'inverted file indexes'. It works by clustering your vectors in order to reduce the similarity search scope. Rather than comparing a vector to every other vector, the vector is only compared against vectors within the same cell cluster (or nearby clusters, depending on your configuration).

倒排列表(单元格簇) #

🌐 Inverted lists (cell clusters)

当你创建索引时,你可以选择倒排列表(单元簇)的数量。增加这个数量可以加快查询速度,但会以召回率为代价。

🌐 When you create the index, you choose the number of inverted lists (cell clusters). Increase this number to speed up queries, but at the expense of recall.

例如,要在使用余弦运算符的列上创建一个有100个列表的索引:

🌐 For example, to create an index with 100 lists on a column that uses the cosine operator:

1
create index on items using ivfflat (column_name vector_cosine_ops) with (lists = 100);

想了解更多关于不同运算符的信息,请参见 距离运算

🌐 For more info on the different operators, see Distance operations.

对于每个查询,你可以设置探针的数量(默认是1)。探针的数量对应要检查的附近单元格数量,以寻找匹配。增加探针数量可以提高召回率,但会降低速度。

🌐 For every query, you can set the number of probes (1 by default). The number of probes corresponds to the number of nearby cells to probe for a match. Increase this for better recall at the expense of speed.

设置会话运行期间的探针数量:

🌐 To set the number of probes for the duration of the session run:

1
set ivfflat.probes = 10;

仅为当前事务运行设置探测数量:

🌐 To set the number of probes only for the current transaction run:

1
begin;
2
set local ivfflat.probes = 10;
3
select ...
4
commit;

如果探针的数量和列表的数量一样,就会执行精确的最近邻搜索,规划器不会使用索引。

🌐 If the number of probes is the same as the number of lists, exact nearest neighbor search will be performed and the planner won't use the index.

近似最近邻 #

🌐 Approximate nearest neighbor

关于 IVF 索引有一个重要的注意事项,那就是最近邻搜索是近似的,因为高维数据的精确搜索无法有效建立索引。这意味着在添加索引之后,相似性结果会(稍微)发生变化(以牺牲召回率换取速度)。

🌐 One important note with IVF indexes is that nearest neighbor search is approximate, since exact search on high dimensional data can't be indexed efficiently. This means that similarity results will change (slightly) after you add an index (trading recall for speed).

你什么时候应该创建 IVFFlat 索引? #

🌐 When should you create IVFFlat indexes?

pgvector 建议仅在表中有足够数据后再建立 IVFFlat 索引,这样内部的 IVFFlat 单元簇才会基于你的数据分布生成。每当分布发生显著变化时,考虑重建索引。

资源 #

🌐 Resources

pgvectorGitHub 页面 上阅读更多关于索引的信息。

🌐 Read more about indexing on pgvector's GitHub page.