[[["易于理解","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-08。"],[],[],null,["# Create and manage vector indexes\n\n\u003cbr /\u003e\n\nMySQL \\| PostgreSQL \\| SQL Server\n\n\u003cbr /\u003e\n\n| To use the GA version of vector search, the instance maintenance version should be version `MYSQL_8_0_version.R20241208.01_00` or newer, where version is the minor version number. For information about how to upgrade your instance to a newer version that supports GA vector embeddings, see [Self-service maintenance](/sql/docs/mysql/self-service-maintenance). \n|\n| For information about the syntax and behavior of vector embeddings for the Public Preview version of this feature, see [Work with vector embeddings (Preview)](/sql/docs/mysql/work-with-vectors-preview).\n\nThis page describes how to configure memory for vector indexes, and create,\ntune, monitor, and drop vector indexes.\n\nBefore you begin\n----------------\n\nBefore you create a vector index, you must load data into the base table with\nvector embedding values. Your base table must have at least 1,000 rows. If you\nhave more data points available, you can get better partitioning and training of\nthe index.\n\nConfigure memory allocation for vector indexes\n----------------------------------------------\n\nThe `cloudsql_vector_max_mem_size` database flag controls how much memory your\nCloud SQL instance dedicates to vector indexes. This is a static flag\nthat requires a restart of your instance. This memory serves two main purposes:\n\n1. **Storing the vector index structure:** the non-leaf portion of the vector\n index (the `TREE_MEMORY`) resides in this memory. The approximate size of\n this tree depends on the number of leaf nodes (`num_leaves`) and the\n dimensions of your vectors:\n\n Approximate TREE_MEMORY = num_leaves * vector dimensions * 4 * 2\n\n For example, an index with 1000 leaves and 768 dimensions would have an\n approximate `TREE_MEMORY` of 1000 \\* 768 \\* 4 \\* 2 or 6144000 bytes. You can\n also check the actual `TREE_MEMORY` using the\n `information_schema.innodb_vector_indexes` table. Cloud SQL\n manages that memory. You don't need to allocate space for all vector indexes\n simultaneously, as inactive indexes are unloaded to make room for other\n requests.\n2. **Memory for index creation (training data):** during vector index creation,\n memory is needed to process a sample of the data from your base table to\n build the index. This memory is used only during the index creation process\n and is freed afterward. The approximate size of the memory needed for\n training is:\n\n approximate_training_memory = num_rows in base table * 0.1 * 4 * vector dimensions\n\n For example, with a table of 1,000,000 rows and 768 dimensions, the\n `training_memory` would be 1000000 \\* 0.1 \\* 768 \\* 4 or 307,200,000 bytes.\n Only 10% of the base table data is sampled to compute the centroids for the\n tree.\n\n When you enable the `cloudsql_vector` flag, Cloud SQL\n automatically sets a default `cloudsql_vector_max_mem_size` based on your VM\n size. This default usually suffices for typical workloads.\n Cloud SQL reduces the `innodb_buffer_pool_size` flag to\n allocate this memory. The default maximum value for\n `cloudsql_vector_max_mem_size` is 16GB. If you need to tune your memory\n size, you can dynamically adjust `cloudsql_vector_max_mem_size` based on\n your vector index usage.\n\n **Important** : If you increase `cloudsql_vector_max_mem_size`, you must\n correspondingly decrease `innodb_buffer_pool_size` to avoid memory issues.\n\n### `cloudsql_vector_max_mem_size` values\n\nThe range of vector index memory allocated is the following:\n\n- 128MB minimum\n- 10% of the buffer pool\n- 16GB maximum\n\nYou can adjust the memory later, as needed. For more information, see\n[Enable the database flag for vector embeddings](/sql/docs/mysql/enable-vector-search#enable_the_database_flag_for_vector_embeddings).\n\nFor information about monitoring the size of your vector index, see\n[Monitor vector indexes](/sql/docs/mysql/create-manage-vector-indexes#monitor_vector_indexes).\n\nTo update the memory allocated for vector indexes on the instance, use the\nfollowing command: \n\n gcloud sql instances patch \u003cvar translate=\"no\"\u003eINSTANCE_NAME\u003c/var\u003e \\\n --database-flags= cloudsql_vector_max_mem_size=\u003cvar translate=\"no\"\u003eNEW_MEMORY_VALUE\u003c/var\u003e;\n\nReplace the following:\n\n- \u003cvar translate=\"no\"\u003eINSTANCE_NAME\u003c/var\u003e: the name of the instance on which you are changing the memory allocation.\n- \u003cvar translate=\"no\"\u003eNEW_MEMORY_VALUE\u003c/var\u003e: the updated memory allocation, in bytes, for your vector indexes.\n\nThis change takes effect immediately after a database restart.\n\nCreate a vector index\n---------------------\n\nThere are two ways to create a vector index:\n\n- [`CREATE VECTOR INDEX`statement](/sql/docs/mysql/data-definition-language#create_vector_index), a Cloud SQL extension to standard MySQL syntax.\n- [`ALTER TABLE` statement](/sql/docs/mysql/data-definition-language#alter_table) with the Cloud SQL `ADD VECTOR INDEX` clause extension. You can't run this statement simultaneously with other DDL statements on thetable.\n\n| **Note:** The size of your base table is calculated by scanning the table for the number of rows with non-NULL entries in the vector embeddings column.\n\nUse the following syntax to create a vector index using `CREATE VECTOR INDEX`: \n\n CREATE\n VECTOR INDEX \u003cvar translate=\"no\"\u003e\u003cspan class=\"devsite-syntax-n\"\u003eINDEX_NAME\u003c/span\u003e\u003c/var\u003e\n ON \u003cvar translate=\"no\"\u003e\u003cspan class=\"devsite-syntax-k\"\u003eTABLE_NAME\u003c/span\u003e\u003c/var\u003e(\u003cvar translate=\"no\"\u003e\u003cspan class=\"devsite-syntax-k\"\u003eCOLUMN_NAME\u003c/span\u003e\u003c/var\u003e)\n USING\n SCANN[QUANTIZER = SQ8]\n DISTANCE_MEASURE\n = L2_SQUARED | COSINE | DOT_PRODUCT[NUM_LEAVES = \u003cvar translate=\"no\"\u003e\u003cspan class=\"devsite-syntax-n\"\u003eINT_VALUE\u003c/span\u003e\u003cspan class=\"devsite-syntax-w\"\u003e \u003c/span\u003e\u003cspan class=\"devsite-syntax-err\"\u003e{\u003c/span\u003e\u003cspan class=\"devsite-syntax-w\"\u003e \u003c/span\u003e\u003cspan class=\"devsite-syntax-s1\"\u003e'</var>'\u003c/span\u003e\u003cspan class=\"devsite-syntax-w\"\u003e \u003c/span\u003e\u003cspan class=\"devsite-syntax-err\"\u003e}}\u003c/span\u003e\u003cspan class=\"devsite-syntax-p\"\u003e];\u003c/span\u003e\n \u003c/var\u003e\n\nThe following are the index options:\n\n- **`USING SCANN`** : optional. Indicates the index type to use. **SCANN** is the only supported value.\n- **`QUANTIZER`** : optional. Maps a high-dimensional vector to a compressed representation. **SQ8** is the only supported value.\n- **`DISTANCE_MEASURE`** : required. Specifies a mathematical formula to use to calculate the similarity of two vectors. You must set the same distance measure in this parameter as the distance you set in the `approx_distance` search options. The supported literals are:\n - `L2_SQUARED`\n - `COSINE`\n - `DOT_PRODUCT`\n- **`NUM_LEAVES`**: optional. Specifies how many partitions (leaves) to build. Only change this setting from its default setting if you have a good understanding of ANN search and your dataset. The number specified can't be larger than the number of embeddings in the base table.\n\nFor example, to create a vector index, run the following: \n\n CREATE\n VECTOR INDEX vectorIndex\n ON dbname.books(embeddings) DISTANCE_MEASURE = L2_SQUARED;\n\nWhile the `CREATE` statement is running, the base table is put into a read-only\nmode and no DMLs are allowed on the base table.\n\nYou can use the following syntax to create an index on an existing table: \n\n ALTER TABLE tbl_name\n ADD VECTOR INDEX index_name(key_part)[index_option];\n\nFor example, to create an index on an existing table: \n\n ALTER TABLE t1 ADD VECTOR INDEX index1(j)\n USING SCANN QUANTIZER = SQ8 DISTANCE_MEASURE = l2_squared NUM_LEAVES = 10;\n\nTune the vector index\n---------------------\n\nThis section gives further information about the parameters that you use to\nbuild the vector index. To tune the vector index, use this information to\ndetermine how to influence the build process.\n\nEnsure `cloudsql_vector_max_mem_size` is configured appropriately for training.\nAdjust `innodb_ddl_threads` to balance build time and CPU load, considering the\nimpact on concurrent database operations. Monitor CPU utilization during the\nbuild.\n\nDrop a vector index\n-------------------\n\nTo drop a vector index, use the SQL `DROP INDEX` or `ALTER TABLE` statements\nwith the index name you want to drop, as shown in the following: \n\n DROP INDEX index_name ON books;\n\n ALTER TABLE table_name\n DROP INDEX index_name;\n\nMonitor vector indexes\n----------------------\n\nCloud SQL provides the following information schema tables with\nreal-time information about vector indexes that are loaded in its memory:\n\n- `information_schema.innodb_vector_indexes` lists all the vector indexes that are opened in the memory after restart.\n- `information_schema.innodb_all_vector_indexes` lists all the vector indexes that exists on the instance (even if they aren't opened in the memory yet).\n- `information_schema.innodb_vector_indexes_memory` provides information about overall memory usage of vector indexes in the instance.\n\nFor more detailed information, see the\n[Information schema](/sql/docs/mysql/information-schema).\n\nTo view information in the `innodb_vector_indexes` table, run the following command: \n\n SELECT * FROM information_schema.innodb_vector_indexes \\ G;\n\nThe output looks similar to the following: \n\n INDEX_NAME: t1_vec_index\n TABLE_NAME: test.t1\n INDEX_TYPE: TREE_SQ\n DIMENSION: 3\n DIST_MEASURE: COSINE\n STATUS: Ready\n STATE: INDEX_READY_TO_USE\n NUM_LEAVES: 10\n NUM_LEAVES_TO_SEARCH: 10\n QUERIES: 1\n MUTATIONS: 1\n TREE_MEMORY: 443\n\nWhat's next\n-----------\n\n- Read the [overview about vector search on Cloud SQL](/sql/docs/mysql/vector-search).\n- Learn how to [enable and disable vector embeddings on your instance](/sql/docs/mysql/enable-vector-search).\n- Learn how to [generate vector embeddings](/sql/docs/mysql/generate-manage-vector-embeddings#generate_vector_embeddings_based_on_row_data).\n- Learn how to [perform searches on vector embeddings](/sql/docs/mysql/search-filter-vector-embeddings)."]]