本页面介绍了 AlloyDB AI 向量搜索策略,并说明了每种策略的使用时间。默认情况下,AlloyDB 使用 k 最近邻搜索 (KNN) 来查找与查询相似的向量。向量索引实现了一种称为近似最近邻 (ANN) 的搜索策略。创建向量索引时,AlloyDB AI 会使用 ANN,其性能优于 KNN。请注意,在选择向量索引时,您需要平衡查询延迟时间和召回率。
召回率用于衡量搜索如何有效地检索给定查询的所有相关项。例如,假设您有 100 个嵌入,每个嵌入表示数据库中的一个实体。您想查询具有目标向量的嵌入,并将其限制为 10 个结果。KNN 向量搜索使用暴力计算方法查找 10 个精确的最近向量,这可实现 100% 召回率。如果未创建或选择向量搜索索引,AlloyDB AI 会默认使用此方法。在 AlloyDB for PostgreSQL 中创建向量索引时,它通常会使用 ANN,这可能会根据相似度对向量进行分区,以便更快地检索。因此使用 ANN 时,前面的示例中返回的 10 个向量可能并非确切是距离上最接近的 10 个向量。如果检索到的 10 个向量中只有 8 个在空间上与查询向量最接近,则召回率为 80%。
[[["易于理解","easyToUnderstand","thumb-up"],["解决了我的问题","solvedMyProblem","thumb-up"],["其他","otherUp","thumb-up"]],[["很难理解","hardToUnderstand","thumb-down"],["信息或示例代码不正确","incorrectInformationOrSampleCode","thumb-down"],["没有我需要的信息/示例","missingTheInformationSamplesINeed","thumb-down"],["翻译问题","translationIssue","thumb-down"],["其他","otherDown","thumb-down"]],["最后更新时间 (UTC):2025-09-04。"],[[["\u003cp\u003eAlloyDB AI uses k-nearest neighbors (KNN) search by default to find vectors similar to a query, but when a vector index is created, it uses Approximate Nearest Neighbor (ANN) for better performance.\u003c/p\u003e\n"],["\u003cp\u003eRecall measures how effectively a search retrieves all relevant items, with KNN achieving 100% recall by using brute force, while ANN, used with indexes, might have a lower recall rate.\u003c/p\u003e\n"],["\u003cp\u003eQuery latency measures the speed at which search results are generated.\u003c/p\u003e\n"],["\u003cp\u003eKNN is recommended for applications requiring high accuracy and when dealing with fewer than 100,000 vectors, whereas ANN is preferred for low latency and when handling over 100,000 vectors.\u003c/p\u003e\n"],["\u003cp\u003eCreating a vector index is recommended by Google to optimize performance of vector searches.\u003c/p\u003e\n"]]],[],null,["This page describes AlloyDB AI vector search strategies and explains\nwhen to use each strategy. By default, AlloyDB uses k-nearest neighbors\nsearch (KNN) to find vectors that are similar to a query. Vector indexes\nimplement a search strategy called Approximate Nearest Neighbor (ANN). When you\ncreate a vector index, AlloyDB AI uses ANN, which provides better\nperformance than KNN. Keep in mind that, when you select a vector index, you\nneed to balance query latency and recall.\n\n*Recall* measures how effectively a search retrieves all relevant items for\na given query. For example, imagine you have 100 embeddings, each one\nrepresenting an entity in your database. You query your embeddings with a\ntarget vector and limit it to 10 results. A KNN vector search finds the 10\nexact closest vectors using a brute force calculation method, which\nresults in 100% recall. AlloyDB AI uses this method by default\nif no vector search index is created or chosen.\nWhen you create a vector index in AlloyDB for PostgreSQL, it typically uses ANN,\nwhich might partition\nvectors according to similarity to facilitate faster retrieval. As a result,\nusing ANN, the 10 vectors returned in the earlier example might not be exactly\nthe 10 vectors that are closest in\ndistance. If only 8 out of the 10 retrieved vectors are the closest in space\nto your query vector, then your recall is 80%.\n\n*Query latency* defines how fast the search results are generated. For\nexample, latency is calculated based on the time spent on a search to\nreturn the vectors after you submit a query.\n\nChoose your search strategy\n\nWhen you perform vector search in AlloyDB, choose one the following\nsearch strategies:\n\n|-------------------------------------|--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|---------------------------------------------------------------------------------------------------------------------------------|\n| Search Strategy | Description | Use Cases |\n| K-nearest neighbors (KNN) | An algorithm that finds the k-nearest neighbors data points to a given query data point. When you perform a vector search without creating an index, a KNN search is performed by default. To further improve the performance of KNN search, add your embedding column, and other columns related to your query, to the column store in the [columnar engine](/alloydb/docs/columnar-engine/about). You can [add the columns manually](/alloydb/docs/columnar-engine/manage-content-manually) or [add the columns using auto-columnarization](/alloydb/docs/columnar-engine/manage-content-recommendations). | - Your application is very sensitive to accuracy and you need the exact closest matches. - You have fewer than 100,000 vectors. |\n| Approximate Nearest Neighbors (ANN) | An algorithm that finds approximately the closest data points. ANN divides existing customer data points into small groups based on similarities. | - Your application requires low latency. - You have more than 100,000 vectors. |\n\nGoogle recommends that you create a vector index to optimize performance on your\nvector search queries. For more information about how the ANN index is used for\nsimilarity searches, see [Create indexes using ScaNN](/alloydb/docs/ai/store-index-query-vectors?resource=scann).\n\nTo accelerate your filtered KNN search, use the [columnar engine](/alloydb/docs/columnar-engine/configure).\n\nWhat's next\n\n- [Create indexes and query vectors using ScaNN](/alloydb/docs/ai/store-index-query-vectors?resource=scann)\n- [Tune vector query performance](/alloydb/docs/ai/tune-indexes)"]]