在 pgvector 中选择距离指标
理解 pgvector 中的余弦距离、L2 距离和内积距离运算符,并学习如何为嵌入选择合适的运算符。
在 pgvector 中选择距离指标 是 CoddyKit 上的免费 Vector Databases: Pinecone, Weaviate & pgvector 课时。 这是第 4 节课,共 4 节。 你可以在下方免费阅读本课时的完整内容 — 然后在浏览器中使用内置代码编辑器和全天候 AI 导师进行实践。 这是 Vector Databases: Pinecone, Weaviate & pgvector 学习路径的一部分,你的进度在网页和 CoddyKit 应用中同步。 Vector Databases: Pinecone, Weaviate & pgvector 课程共包含 4 节课。
本课时的部分内容尚未翻译,以英文显示。
Why Distance Matters
Similarity search ranks vectors by distance. The metric you choose must match how your embedding model was trained, or results will be subtly wrong.
The Three Operators
pgvector provides three distance operators:
<->Euclidean (L2) distance<=>Cosine distance<#>Negative inner product
SELECT '[1,2,3]'::vector <=> '[1,2,4]'::vector AS cosine_distance;Cosine Distance
Cosine measures angle, ignoring magnitude. It is the default for most text embedding models like OpenAI and Sentence Transformers.
SELECT id, embedding <=> '[0.1,0.2,0.3]'::vector AS dist
FROM items
ORDER BY dist
LIMIT 5;Euclidean (L2) Distance
L2 measures straight-line distance and is sensitive to magnitude. Use it when vector length carries meaning, such as raw feature vectors.
SELECT id, embedding <-> '[0.1,0.2,0.3]'::vector AS dist
FROM items
ORDER BY dist
LIMIT 5;Inner Product
The inner product operator <#> returns the negative dot product (so smaller is more similar). It is fastest and ideal for normalized vectors.
SELECT id, (embedding <#> '[0.1,0.2,0.3]'::vector) * -1 AS similarity
FROM items
ORDER BY embedding <#> '[0.1,0.2,0.3]'::vector
LIMIT 5;Match Metric to Model
Always check your embedding model's documentation:
- OpenAI text-embedding-3: cosine
- Normalized vectors: inner product (equivalent to cosine, faster)
- Geometric/spatial features: L2
Normalization Trick
If your vectors are unit-normalized, cosine and inner product give the same ranking. Inner product is cheaper, so normalize once at insert time.
-- Normalize before insert in app code, then use <#>
UPDATE items SET embedding = l2_normalize(embedding);Metric in Indexes
Indexes are metric-specific. You must declare the operator class so the index matches your query operator.
CREATE INDEX ON items
USING hnsw (embedding vector_cosine_ops);Mismatched Operators
If your index uses vector_l2_ops but you query with <=>, Postgres ignores the index and does a slow sequential scan. Keep them aligned.
Verifying Index Usage
Use EXPLAIN to confirm the planner uses your vector index for the chosen metric.
EXPLAIN ANALYZE
SELECT id FROM items
ORDER BY embedding <=> '[0.1,0.2,0.3]'::vector
LIMIT 5;Summary of Choices
Rule of thumb:
- Text/semantic search: cosine
- Pre-normalized vectors: inner product
- Raw numeric features: L2
Quick Check
Pick the right metric.
Recap
You learned pgvector's three distance operators, when to use cosine, L2, and inner product, and why your index operator class must match your query operator for fast searches.
常见问题解答
「在 pgvector 中选择距离指标」课时是免费的吗?
是的 — 「在 pgvector 中选择距离指标」的完整文本可在网页上免费阅读。要进行交互式练习(内置代码编辑器和全天候 AI 导师)并解锁 Vector Databases: Pinecone, Weaviate & pgvector 课程的其余内容,请升级到 CoddyKit PRO。 Vector Databases: Pinecone, Weaviate & pgvector 课程共包含 4 节课。
「在 pgvector 中选择距离指标」这节课中我会学到什么?
理解 pgvector 中的余弦距离、L2 距离和内积距离运算符,并学习如何为嵌入选择合适的运算符。 你通过在浏览器中直接运行的动手代码来练习 Vector Databases: Pinecone, Weaviate & pgvector,全天候 AI 导师会在你学习这节课的过程中回答你的问题。
学习 Vector Databases: Pinecone, Weaviate & pgvector 需要有经验吗?
无需任何先前经验。CoddyKit 上的 Vector Databases: Pinecone, Weaviate & pgvector 课程适合初学者到高级学习者,你可以从这里开始或从头开始,按照自己的节奏学习。 这是第 4 节课,共 4 节。
「在 pgvector 中选择距离指标」课时需要多长时间?
大多数 CoddyKit 课程大约需要 5–10 分钟。每节课都很精短且互动,所以你能稳步进步,并在网页和应用中从离开的地方继续。
我能在这节 Vector Databases: Pinecone, Weaviate & pgvector 课中编写并运行代码吗?
能。每节 Vector Databases: Pinecone, Weaviate & pgvector 课都包含内置代码编辑器,你可以在浏览器中直接编写并运行真实代码,并获得即时 AI 反馈 — 无需本地设置。
此课程中的所有课时
- 设置 pgvector 扩展
- 在 PostgreSQL 中存储向量
- 执行相似度查询
- 在 pgvector 中选择距离指标