向量索引
一旦你的向量表开始变大,你可能会想添加一个索引来加快查询速度。没有索引的话,你就得进行顺序扫描,而当记录很多时,这可能会非常耗费资源。
🌐 Once your vector table starts to grow, you will likely want to add an index to speed up queries. Without indexes, you'll be performing a sequential scan which can be a resource-intensive operation when you have many records.
选择一个索引 #
🌐 Choosing an index
今天 pgvector 支持两种类型的索引:
🌐 Today pgvector supports two types of indexes:
一般来说,我们推荐使用HNSW,因为它的性能和对数据变化的稳健性。
🌐 In general we recommend using HNSW because of its performance and robustness against changing data.
距离运算符 #
🌐 Distance operators
索引可以用来通过各种距离度量提高最近邻搜索的性能。pgvector 包含 3 个距离运算符:
🌐 Indexes can be used to improve performance of nearest neighbor search using various distance measures. pgvector includes 3 distance operators:
| 运算符 | 描述 | 运算符类别 |
|---|---|---|
<-> | 欧几里得距离 | vector_l2_ops |
<#> | 负内积 | vector_ip_ops |
<=> | 余弦距离 | vector_cosine_ops |
对于 pgvector 0.7.0 及以上版本,可以在向量上创建如下最大维度的索引:
🌐 For pgvector versions 0.7.0 and above, it's possible to create indexes on vectors with the following maximum dimensions:
- 向量:最多 2,000 维
- halfvec:最多 4000 个维度
- 位:高达 64,000 维
你可以通过运行 SELECT * FROM pg_extension WHERE extname = 'vector'; 来查看你当前的 pgvector 版本,或者在你的 Supabase 项目仪表板中导航到 扩展 标签查看。
🌐 You can check your current pgvector version by running: SELECT * FROM pg_extension WHERE extname = 'vector'; or by navigating to the Extensions tab in your Supabase project dashboard.
如果你使用的是早期版本的 pgvector,你应该在这里升级你的项目。
🌐 If you are on an earlier version of pgvector, you should upgrade your project here.
资源 #
🌐 Resources
在 pgvector 的 GitHub 页面 上阅读更多关于索引的信息。
🌐 Read more about indexing on pgvector's GitHub page.