[[["易于理解","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-08-25。"],[[["\u003cp\u003eMaintaining vector indexes is crucial for adapting to data changes and ensuring accurate search results.\u003c/p\u003e\n"],["\u003cp\u003ePeriodic reindexing of ScaNN indexes is recommended for tables with frequent updates or insertions to enhance recall accuracy.\u003c/p\u003e\n"],["\u003cp\u003eVector index metrics can be monitored to identify changes in vector distributions or mutations, aiding in the decision to reindex.\u003c/p\u003e\n"],["\u003cp\u003eIndexes can be manually rebuilt using the \u003ccode\u003eREINDEX INDEX CONCURRENTLY\u003c/code\u003e command, maintaining the original configurations.\u003c/p\u003e\n"],["\u003cp\u003eThe name provided for each index should be unique across the database, and shared between all tables.\u003c/p\u003e\n"]]],[],null,["# Maintain vector indexes\n\nThis page describes options for maintaining your vector\nindexes. Maintaining indexes helps to ensure that the indexes adapt to data changes that might impact the\naccuracy of your search results. Use the strategies in this page to avoid degradation\nin query performance as your dataset grows.\n\nBefore you begin\n----------------\n\n- Install or update the `vector` and `alloydb_scann` extensions.\n\n 1. If the `vector` and `alloydb_scann` extensions aren't installed, install the\n latest extension versions.\n\n CREATE EXTENSION IF NOT EXISTS vector;\n CREATE EXTENSION IF NOT EXISTS alloydb_scann;\n\n 2. If the `vector` and `alloydb_scann` extensions are already installed, upgrade the\n extensions to the latest version.\n\n ALTER EXTENSION vector UPDATE;\n ALTER EXTENSION alloydb_scann UPDATE;\n\nView vector index metrics\n-------------------------\n\nIf your table is prone to frequent updates or insertions, then we recommend\nperiodically [reindexing the existing ScaNN index](#manually-rebuild-index)\nin order to improve the recall accuracy for your index. You can monitor\nindex metrics to view changes in vector distributions or vector mutations since\nthe index was built, and then reindex accordingly.\n\nFor more information about metrics, see [View vector index metrics](/alloydb/docs/reference/vector-index-metrics).\n\nMaintain indexes automatically\n------------------------------\n\n|\n| **Preview**\n|\n|\n| This feature is subject to the \"Pre-GA Offerings Terms\" in the General Service Terms section\n| of the [Service Specific Terms](/terms/service-terms#1).\n|\n| Pre-GA features are available \"as is\" and might have limited support.\n|\n| For more information, see the\n| [launch stage descriptions](/products#product-launch-stages).\n\nYou can use the `scann.enable_preview_features` database flag (GUC) along with the\nindex-level `auto_maintenance` parameter while creating a ScaNN index. Using these settings together lets\nAlloyDB incrementally manage the index such that when your\ndataset grows, it splits large outlier partitions. By\nsplitting partitions, AlloyDB tries to provide better QPS and\nsearch results.\n\nAny updates made to the index as a result of auto maintenance are permanent\nuntil AlloyDB updates the index again.\n\nTo enable AlloyDB to maintain an index automatically, enable the\n`scann.enable_preview_features` flag: \n\n gcloud alloydb instances update \u003cvar translate=\"no\"\u003e\u003cspan class=\"devsite-syntax-n\"\u003eINSTANCE_ID\u003c/span\u003e\u003c/var\u003e \\\n --database-flags scann.enable_preview_features=on \\\n --region=\u003cvar translate=\"no\"\u003eREGION_ID\u003c/var\u003e \\\n --cluster=\u003cvar translate=\"no\"\u003eCLUSTER_ID\u003c/var\u003e \\\n --project=\u003cvar translate=\"no\"\u003ePROJECT_ID\u003c/var\u003e\n\nReplace the following:\n\n- \u003cvar translate=\"no\"\u003eINSTANCE_ID\u003c/var\u003e: The ID of the instance.\n- \u003cvar translate=\"no\"\u003eREGION_ID\u003c/var\u003e: The region where the instance is placed---for example, `us-central1`.\n- \u003cvar translate=\"no\"\u003eCLUSTER_ID\u003c/var\u003e: The ID of the cluster where the instance is placed.\n- \u003cvar translate=\"no\"\u003ePROJECT_ID\u003c/var\u003e: The ID of the project where the cluster is placed.\n\nAfter you enable the `scann.enable_preview_features` flag, you can enable auto\nmaintenance for indexes, or you can enable the `scann_index_maintenance`\nfunction to manually invoke maintenance.\n\n### Enable auto-maintenance during index creation\n\nTo create a ScaNN index with auto maintenance enabled, run the following example command: \n\n CREATE INDEX \u003cvar translate=\"no\"\u003e\u003cspan class=\"devsite-syntax-n\"\u003eINDEX_NAME\u003c/span\u003e\u003c/var\u003e ON \u003cvar translate=\"no\"\u003e\u003cspan class=\"devsite-syntax-k\"\u003eTABLE\u003c/span\u003e\u003c/var\u003e\n USING scann (\u003cvar translate=\"no\"\u003e\u003cspan class=\"devsite-syntax-n\"\u003eEMBEDDING_COLUMN\u003c/span\u003e\u003c/var\u003e \u003cvar translate=\"no\"\u003e\u003cspan class=\"devsite-syntax-n\"\u003eDISTANCE_FUNCTION\u003c/span\u003e\u003c/var\u003e)\n WITH (num_leaves=\u003cvar translate=\"no\"\u003e\u003cspan class=\"devsite-syntax-n\"\u003eNUM_LEAVES_VALUE\u003c/span\u003e\u003c/var\u003e, auto_maintenance=on);\n\nReplace the following:\n\n- \u003cvar translate=\"no\"\u003eINDEX_NAME\u003c/var\u003e: the name of the index you want to\n create---for example, `my-scann-index`. The index names are shared\n across your database. Ensure that each index name is unique to each\n table in your database.\n\n- \u003cvar translate=\"no\"\u003eTABLE\u003c/var\u003e: the table to add the index to.\n\n- \u003cvar translate=\"no\"\u003eEMBEDDING_COLUMN\u003c/var\u003e: a column that stores `vector`\n data.\n\n- \u003cvar translate=\"no\"\u003eDISTANCE_FUNCTION\u003c/var\u003e: the distance function to use\n with this index. Choose one of the following:\n\n - **L2 distance:** `l2`\n\n - **Dot product:** `dot_product`\n\n - **Cosine distance:** `cosine`\n\n- \u003cvar translate=\"no\"\u003eNUM_LEAVES_VALUE\u003c/var\u003e: the number of partitions to apply to\n this index. Set to any value between 1 to 1048576. For more information\n about how to decide this value, see [Tune a `ScaNN` index](/alloydb/docs/ai/tune-indexes).\n\n### Manage leaves to search for split partitions automatically\n\nIf you have enabled automatic maintenance of indexes, then AlloyDB\nautomatically splits partitions when the `num_leaves` threshold is reached. As\nthe number of partitions grows due to these splits, you should adjust the number\nof leaves to search to maintain optimal performance.\n\nTo manage the number of leaves to search automatically, use\n`pct_leaves_to_search`. This parameter lets you specify a percentage of number\nof partitions to search. If you expect your dataset to grow significantly, then\nstart by setting the `pct_leaves_to_search` value to 1. The parameter is\ndisabled by default.\n\nSet this value to the percentage of current number of partitions. For example, to search 1%\nof current number of partitions, set this value to `1`. \n\nYou\ncan set this parameter to any value between `0` to `100`.\nThe default value is `0`, which disables this parameter and uses the\n`scann.num_leaves_to_search` to calculate the number of leaves to search.\n\nTo set the `pct_leaves_to_search` flag on your database, run the following command: \n\n ALTER DATABASE \u003cvar translate=\"no\"\u003e\u003cspan class=\"devsite-syntax-n\"\u003eDATABASE_NAME\u003c/span\u003e\u003c/var\u003e SET scann.pct_leaves_to_search = \u003cvar translate=\"no\"\u003e\u003cspan class=\"devsite-syntax-n\"\u003ePERCENTAGE_LEAVES_TO_SEARCH\u003c/span\u003e\u003c/var\u003e;\n\nReplace the following:\n\n- \u003cvar translate=\"no\"\u003eDATABASE_NAME\u003c/var\u003e: the name of the database.\n- \u003cvar translate=\"no\"\u003ePERCENTAGE_LEAVES_TO_SEARCH\u003c/var\u003e: the percentage of `num_leaves` to search.\n\n### Manually invoke maintenance\n\nIf you want to invoke maintenance on a particular index on-demand, then run the following command: \n\n SELECT scann_index_maintenance('\u003cvar translate=\"no\"\u003eINDEX_NAME\u003c/var\u003e');\n\nManually rebuild your index\n---------------------------\n\nYou can manually rebuild your index if you want to\nrebuild it with the configurations you specified when it was created.\n\nTo manually rebuild your index, run the following command: \n\n REINDEX INDEX CONCURRENTLY \u003cvar translate=\"no\"\u003e\u003cspan class=\"devsite-syntax-n\"\u003eINDEX_NAME\u003c/span\u003e\u003c/var\u003e;\n\nReplace \u003cvar translate=\"no\"\u003eINDEX_NAME\u003c/var\u003e with the name of the index you want to\nrebuild---for example, `my-scann-index`. The index names are shared\nacross your database. Ensure that each index name is unique to each\ntable in your database.\n\nFor more information about reindexing in PostgreSQL, see\n[REINDEX](https://www.postgresql.org/docs/15/sql-reindex.html).\n\nWhat's next\n-----------\n\n- [Vector index metrics](/alloydb/docs/reference/vector-index-metrics)\n- [Optimize vector query performance for ScaNN](/alloydb/docs/ai/scann-vector-query-perf-overview)"]]