外观
本页尚未翻译
本页内容仍为英文原文,源自官方文档 sql/dql/vector-distance。中文翻译正在分批进行中。
Vector distance functions
cosine_distance, distance, exact_distance and inner_product: per-row vector distance, and the ORDER BY rewrite into vector search.
Applies to: Embedded ❌, Enterprise ✅
text
cosine_distance distance exact_distance inner_product注意
Distance functions can evaluate arrays per row. Some deployment versions also recognize ORDER BY distance(vector, query_vector) LIMIT k over a table scan and plan it as vector search, with indexed or flat search as appropriate. Where available, exact_distance evaluates distance without that rewrite. Confirm the functions supported by your deployment and inspect SQL EXPLAIN; the function name alone does not guarantee an index-backed plan. See SQL vector search.
A third, optional argument is a comma-separated options string, and only distance and exact_distance accept one; cosine_distance and inner_product are plain two-argument functions. metric selects l2 (default, alias euclidean), cosine, or dot. nprobes and use_index only affect distance, since it's the only one of the two that the index-search rewrite applies to in the first place -- exact_distance is never rewritten into an index search, so passing it use_index/nprobes does nothing; use a plain distance call with use_index=false to force a flat scan instead:
sql
SELECT id FROM documents
ORDER BY distance(embedding, ARRAY[0.1, 0.2, 0.3], 'metric=cosine,nprobes=20')
LIMIT 10
-- Force a flat scan, bypassing the index:
SELECT id FROM documents
ORDER BY distance(embedding, ARRAY[0.1, 0.2, 0.3], 'use_index=false')
LIMIT 10