Before you decide an index strategy, you must know the following terms:
Recall: It defines how effectively a search retrieves all relevant items for
a given query. For example, if you have 100 images and 10 are cats, which is
true match, and your search for "cat" returns 6 cat images, your recall is 6
out of 10, which is 60%. In approximate nearest neighbors (ANN) search for vector indexes, recall is measured
by comparing the results obtained with the ANN index to the true match results obtained
through a brute-force search.
Query latency: It defines how fast the search results are generated. For
example, latency is calculated based on the time spent on a search to return the 6 cat images after you submit a query.
Search strategy
You must select one of the following search strategies. Consider an example
table with customer data that includes details such as purchase history,
preferences, and demographics to understand how both algorithms differ:
K-Nearest Neighbors (KNN) is an algorithm that finds the k-nearest data
points to a given query data point. For the previous example, to run a query
that recommends products to new customers based on preferences of similar
customers, KNN calculates the distance between the new customer data points
and the data points of each customer in the table using a distance metric. KNN
would identify k customers that match the criteria based on the distance, and
then recommend products that the identified customers (k) like to the new
customer.
A KNN query with distance metric, such as Euclidean distance or cosine
similarity, is recommended for smaller datasets. If your use case requires 100%
accuracy and you can tolerate a higher query latency, then we recommend using
KNN. For more information about performing a KNN search, see Perform similarity
search.
Approximate Nearest Neighbors (ANN) algorithm-based indexes help queries
run faster, but might compromise on query accuracy or recall. For
the earlier example, to run a query that recommends products to new customers
based on preferences of similar customers, ANN divides existing
customer data points into small groups based on similarities. When a new
customer data point is added, ANN first identifies a group, out of multiple
groups, that the data point belongs to, and then only calculates distance
using a distance metric within the group. This filtering reduces the number of
calculations required to show recommendations.
Google recommends creating a vector index that implements the ANN index when you have a
large dataset since it finds similarities faster and is less resource-intensive,
but comprises accuracy. For more information about how the ANN index is used for
similarity searches, see Create indexes and query vectors using ScaNN. To achieve KNN accuracy with a
vector index, set the search parameters equal to the number of partitions. This
strategy helps to achieve KNN accuracy with a balance of query latency.
Use the following flowchart as a guidance to decide which index type is optimal
based on your dataset and requirements.
[[["Easy to understand","easyToUnderstand","thumb-up"],["Solved my problem","solvedMyProblem","thumb-up"],["Other","otherUp","thumb-up"]],[["Hard to understand","hardToUnderstand","thumb-down"],["Incorrect information or sample code","incorrectInformationOrSampleCode","thumb-down"],["Missing the information/samples I need","missingTheInformationSamplesINeed","thumb-down"],["Other","otherDown","thumb-down"]],["Last updated 2025-08-25 UTC."],[[["\u003cp\u003eAlloyDB AI uses k-nearest neighbors (KNN) search by default to find vectors similar to a query, but Approximate Nearest Neighbor (ANN) is employed when a vector index is created for better performance.\u003c/p\u003e\n"],["\u003cp\u003eRecall measures the effectiveness of a search in retrieving all relevant items, with KNN providing 100% recall through a brute force method, while ANN may result in a lower recall due to faster retrieval.\u003c/p\u003e\n"],["\u003cp\u003eQuery latency is the time taken to generate search results, and it is a key factor in choosing a vector search strategy.\u003c/p\u003e\n"],["\u003cp\u003eKNN is ideal for applications requiring exact matches and with fewer than 100,000 vectors, whereas ANN is preferred for low-latency needs with more than 100,000 vectors.\u003c/p\u003e\n"],["\u003cp\u003eGoogle recommends creating a vector index to optimize performance on vector search queries.\u003c/p\u003e\n"]]],[],null,["# Choose a vector index\n\nSelect a documentation version: Current (16.8.0)keyboard_arrow_down\n\n- [Current (16.8.0)](/alloydb/omni/current/docs/ai/choose-index-strategy)\n- [16.8.0](/alloydb/omni/16.8.0/docs/ai/choose-index-strategy)\n- [16.3.0](/alloydb/omni/16.3.0/docs/ai/choose-index-strategy)\n- [15.12.0](/alloydb/omni/15.12.0/docs/ai/choose-index-strategy)\n- [15.7.1](/alloydb/omni/15.7.1/docs/ai/choose-index-strategy)\n- [15.7.0](/alloydb/omni/15.7.0/docs/ai/choose-index-strategy)\n\n\u003cbr /\u003e\n\nYou must select an index type for your vector search that gives you a balance between query latency and accuracy of your results. Effective indexing directly impacts query speed and the ability to retrieve truly relevant matches that are measured by recall.\n\n\u003cbr /\u003e\n\nKey terms\n---------\n\nBefore you decide an index strategy, you must know the following terms:\n\n- *Recall*: It defines how effectively a search retrieves all relevant items for a given query. For example, if you have 100 images and 10 are cats, which is true match, and your search for \"cat\" returns 6 cat images, your recall is 6 out of 10, which is 60%. In approximate nearest neighbors (ANN) search for vector indexes, recall is measured by comparing the results obtained with the ANN index to the true match results obtained through a brute-force search.\n- *Query latency*: It defines how fast the search results are generated. For example, latency is calculated based on the time spent on a search to return the 6 cat images after you submit a query.\n\nSearch strategy\n---------------\n\nYou must select one of the following search strategies. Consider an example\ntable with customer data that includes details such as purchase history,\npreferences, and demographics to understand how both algorithms differ:\n\n- **K-Nearest Neighbors (KNN)** is an algorithm that finds the k-nearest data\n points to a given query data point. For the previous example, to run a query\n that recommends products to new customers based on preferences of similar\n customers, KNN calculates the distance between the new customer data points\n and the data points of each customer in the table using a distance metric. KNN\n would identify k customers that match the criteria based on the distance, and\n then recommend products that the identified customers (k) like to the new\n customer.\n\n A KNN query with distance metric, such as Euclidean distance or cosine\n similarity, is recommended for smaller datasets. If your use case requires 100%\n accuracy and you can tolerate a higher query latency, then we recommend using\n KNN. For more information about performing a KNN search, see [Perform similarity\n search](/alloydb/omni/current/docs/ai/work-with-embeddings#generate-embeddings-similarity-search).\n- **Approximate Nearest Neighbors (ANN)** algorithm-based indexes help queries\n run faster, but might compromise on query accuracy or recall. For\n the earlier example, to run a query that recommends products to new customers\n based on preferences of similar customers, ANN divides existing\n customer data points into small groups based on similarities. When a new\n customer data point is added, ANN first identifies a group, out of multiple\n groups, that the data point belongs to, and then only calculates distance\n using a distance metric within the group. This filtering reduces the number of\n calculations required to show recommendations.\n\n Google recommends creating a vector index that implements the ANN index when you have a\n large dataset since it finds similarities faster and is less resource-intensive,\n but comprises accuracy. For more information about how the ANN index is used for\n similarity searches, see [Create indexes and query vectors using ScaNN](/alloydb/omni/current/docs/ai/store-index-query-vectors?resource=scann). To achieve KNN accuracy with a\n vector index, set the search parameters equal to the number of partitions. This\n strategy helps to achieve KNN accuracy with a balance of query latency.\n\nUse the following flowchart as a guidance to decide which index type is optimal\nbased on your dataset and requirements.\n\nWhat's next\n-----------\n\n- [Create indexes and query vectors using ScaNN](/alloydb/omni/current/docs/ai/store-index-query-vectors?resource=scann)\n- [Tune vector query performance](/alloydb/omni/current/docs/ai/tune-indexes)"]]