Skip to content

The Postgres development platform. Supabase gives you a dedicated Postgres database to build your web, mobile, and AI applications.

docs: update vector sorting to ensure index gets used (#19133)

anngbaumce37e937b3fb
diff --git a/apps/docs/pages/guides/ai/vector-columns.mdx b/apps/docs/pages/guides/ai/vector-columns.mdx
index 8dc048667f4..c5d9cb18e9d 100644
--- a/apps/docs/pages/guides/ai/vector-columns.mdx
+++ b/apps/docs/pages/guides/ai/vector-columns.mdx
@@ -132,7 +132,7 @@ as $$
1 - (documents.embedding <=> query_embedding) as similarity
from documents
where 1 - (documents.embedding <=> query_embedding) > match_threshold
- order by similarity desc
+ order by (documents.embedding <=> query_embedding) asc
limit match_count;
$$;
```
@@ -141,6 +141,8 @@ This function takes a `query_embedding` argument and compares it to all other em
Feel free to modify this method to fit the needs of your application. The `match_threshold` ensures that only documents that have a minimum similarity to the `query_embedding` are returned. Without this, you may end up returning documents that subjectively don't match. This value will vary for each application - you will need to perform your own testing to determine the threshold that makes sense for your app.
+If you index your vector column, ensure that the `order by` sorts by the distance function directly (rather than sorting by the calculated `similarity` column, which may lead to the index being ignored and poor performance).
+
To execute the function from your client library, call `rpc()` with the name of your Postgres function:
```ts