Stay organized with collectionsSave and categorize content based on your preferences.
This page provides a detailed reference for the tuning parameters available for Scalable Nearest Neighbors (ScaNN) indexes in AlloyDB for PostgreSQL.
For a step-by-step tutorial on how to implement vector search from start to finish, see the guide on how toPerform a vector search.
Tuning parameters
The following index and query parameters are used to find the right balance of recall and queries per second (QPS).
Tuning parameter
Description
Option type
max_num_levels
The maximum number of centroid levels of the K-means clustering tree.
Two-level tree index: Set to1by default for a two-level tree (1 centroid level + bottom leaf level).
Three-level tree index: Set to2by default for a three-level tree (2 centroid levels + bottom leaf level)
Set the value to2if the number of vector rows exceeds 100 million rows.
Set the value to1if the number of vector rows are less than 10 million rows.
Set to either1or2if the number of vector rows lie between 10 million and 100 million rows to optimize for index build time (set to 2) or optimize for search recall (set to 1).
Index creation (optional)
num_leaves
The number of partitions to apply to this index. The number of partitions you apply to when creating an index affects the index performance. By increasing partitions for a set number of vectors, you create a more fine-grained index, which improves recall and query performance. However, this comes at the cost of longer index creation times.
Since three-level trees build faster than two-level trees, you can increase thenum_leaves_valuewhen creating a three-level tree index to achieve better performance.
Two-level index: Set this value to any value between1and1048576.
For an index that balances fast index build and good search performance, usesqrt(ROWS)as a starting point, whereROWSis the number of vector rows. The number of vectors that each partition holds is calculated by ROWS/sqrt(ROWS) = sqrt(ROWS).
Since a two-level tree index can be created on a dataset with less than 10 million vector rows, each partition will hold less than (sqrt(10M)) vectors, which is3200vectors. For optimal vector search quality, it's recommended to minimize the number of vectors in each partition. The recommended partition size is about 100 vectors per partition, so setnum_leavestoROWS/100. If you have 10 million vectors you would setnum_leavesto 100,000.
Three-level index: Set this value to any value between1and1048576.
If you are unsure about selecting the exact value, usepower(ROWS, 2/3)as a starting point, whereROWSis the number of vector rows. The number of vectors that each partition holds is calculated by ROWS/power(ROWS, 2/3) = power(ROWS, 1/3).
Since a three-level tree index can be created on a dataset with vector rows more than 100 million, each partition will hold more than (power(100M, 1/3)) vectors, which is465vectors. For optimal performance, it's recommended to minimize the number of vectors in each partition. The recommended partition size is about 100 vectors per partition, so setnum_leavestoROWS/100. If you have 100 million vectors you would setnum_leavesto 1 million.
Index creation (required)
quantizer
The type of quantizer you want to use for the K-means tree. The default value is set toSQ8which provides better query performance with minimal recall loss (typically less than 1-2%).
Set it toFLATif a recall of 99% or higher is required.
Index creation (optional)
scann.enable_pca
Enables Principal Component Analysis (PCA), which is a dimension reduction technique used to automatically
reduce the size of the embedding when possible. This option is enabled by default.
Set tofalseif you observe deterioration in recall.
You can set this parameter to any value between0to100. The default value is0, which disables this parameter and uses thescann.num_leaves_to_searchto calculate the number of leaves to search. The parameter is disabled by default.
scann.num_leaves_to_search
This database flag controls the absolute number of leaves or partitions to search which lets you trade off between recall and QPS. The default value is 1% of the value set innum_leaves.
A higher value will result in better recall but lower QPS. Similarly, a lower value will result in lower recall but higher QPS.
Query runtime (optional)
scann.pre_reordering_num_neighbors
The database flag, when set, specifies the number of candidate neighbors to consider during the reordering stages after the initial search identifies a set of candidates. Set this parameter to a value higher than the number of neighbors you want the query to return.
A higher value results in better recall, but a lower QPS. Set this value to0to disable reordering. The default is0if PCA is not enabled during index creation. Otherwise, the default is50 x K, whereKis the LIMIT specified in the query.
Query runtime (optional)
scann.num_search_threads
The number of searcher threads for multi-thread search. This can help reduce single query latency by using more than one thread for ScaNN ANN search in latency-sensitive applications. This setting doesn't improve single query latency if the database is already cpu-bound. The default value is2.
[[["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-09-04 UTC."],[[["\u003cp\u003eThis page details the tuning parameters for the ScaNN Index in AlloyDB for PostgreSQL, which are critical for balancing recall and QPS.\u003c/p\u003e\n"],["\u003cp\u003eThe \u003ccode\u003emax_num_levels\u003c/code\u003e parameter determines the K-means clustering tree's depth, with recommendations based on the number of vector rows, from optimizing for build time, or for search recall.\u003c/p\u003e\n"],["\u003cp\u003eThe \u003ccode\u003enum_leaves\u003c/code\u003e parameter affects index performance by partitioning the index, and a two-level or a three-level tree index has different guidelines for the recommended values, impacting both index creation and search performance.\u003c/p\u003e\n"],["\u003cp\u003eParameters like \u003ccode\u003equantizer\u003c/code\u003e, \u003ccode\u003escann.enable_inline_filtering\u003c/code\u003e, \u003ccode\u003escann.enable_pca\u003c/code\u003e, \u003ccode\u003escann.num_leaves_to_search\u003c/code\u003e, \u003ccode\u003escann.pre_reordering_num_neighbors\u003c/code\u003e, and \u003ccode\u003escann.num_search_threads\u003c/code\u003e allow fine-tuning for recall, QPS, filtering, dimensionality reduction, and multi-threading, offering extensive customization options.\u003c/p\u003e\n"],["\u003cp\u003eThe next step suggested is to begin using vector embeddings with AlloyDB AI, with a link to a guide.\u003c/p\u003e\n"]]],[],null,["# AlloyDB ScaNN Index reference\n\nThis page provides a detailed reference for the tuning parameters available for Scalable Nearest Neighbors (ScaNN) indexes in AlloyDB for PostgreSQL.\n\nFor a step-by-step tutorial on how to implement vector search from start to finish, see the guide on how to [Perform a vector search](/alloydb/docs/ai/perform-vector-search).\n| **Note:** Before creating a ScaNN index, you must install the `alloydb_scann` and `vector` extensions. For details on the `CREATE INDEX` command, refer to [Create indexes](/alloydb/docs/ai/store-index-query-vectors?resource=scann).\n\nTuning parameters\n-----------------\n\nThe following index and query parameters are used to find the right balance of recall and queries per second (QPS).\n\nOption type Index creation Query runtime \n\nWhat's next\n-----------\n\n- [Get started with vector embeddings using AlloyDB AI](https://codelabs.developers.google.com/alloydb-ai-embedding#0)."]]