Create and manage vector indexes Stay organized with collections Save and categorize content based on your preferences.
MYSQL_8_0_version.R20241208.01_00 or newer, whereversion is the minor version number. For information about how to upgrade yourinstance to a newer version that supports GA vector embeddings, seeSelf-service maintenance.For information about the syntax and behavior of vector embeddings for thePublic Preview version of this feature, seeWork with vector embeddings (Preview).
This page describes how to configure memory for vector indexes, and create,tune, monitor, and drop vector indexes.
Before you begin
Before you create a vector index, you must load data into the base table withvector embedding values. Your base table must have at least 1,000 rows. If youhave more data points available, you can get better partitioning and training ofthe index.
Configure memory allocation for vector indexes
Thecloudsql_vector_max_mem_size database flag controls how much memory yourCloud SQL instance dedicates to vector indexes. This is a static flagthat requires a restart of your instance. This memory serves two main purposes:
Storing the vector index structure: the non-leaf portion of the vectorindex (the
TREE_MEMORY) resides in this memory. The approximate size ofthis tree depends on the number of leaf nodes (num_leaves) and thedimensions of your vectors:Approximate TREE_MEMORY = num_leaves * vector dimensions * 4 * 2For example, an index with 1000 leaves and 768 dimensions would have anapproximate
TREE_MEMORYof 1000 * 768 * 4 * 2 or 6144000 bytes. You canalso check the actualTREE_MEMORYusing theinformation_schema.innodb_vector_indexestable. Cloud SQLmanages that memory. You don't need to allocate space for all vector indexessimultaneously, as inactive indexes are unloaded to make room for otherrequests.Memory for index creation (training data): during vector index creation,memory is needed to process a sample of the data from your base table tobuild the index. This memory is used only during the index creation processand is freed afterward. The approximate size of the memory needed fortraining is:
approximate_training_memory = num_rows in base table * 0.1 * 4 * vector dimensionsFor example, with a table of 1,000,000 rows and 768 dimensions, the
training_memorywould be 1000000 * 0.1 * 768 * 4 or 307,200,000 bytes.Only 10% of the base table data is sampled to compute the centroids for thetree.When you enable the
cloudsql_vectorflag, Cloud SQLautomatically sets a defaultcloudsql_vector_max_mem_sizebased on your VMsize. This default usually suffices for typical workloads.Cloud SQL reduces theinnodb_buffer_pool_sizeflag toallocate this memory. The default maximum value forcloudsql_vector_max_mem_sizeis 16GB. If you need to tune your memorysize, you can dynamically adjustcloudsql_vector_max_mem_sizebased onyour vector index usage.Important: If you increase
cloudsql_vector_max_mem_size, you mustcorrespondingly decreaseinnodb_buffer_pool_sizeto avoid memory issues.
cloudsql_vector_max_mem_size values
| VM size | cloudsql_vector_max_mem_size |
| 4GB | 194MB |
| 8GB | 515MB |
| 16GB | 1.2GB |
| 32GB | 2.56GB |
| 64GB | 5.12GB |
| 128GB | 10.24GB |
| 256GB+ | 16GB |
The range of vector index memory allocated is the following:
- 128MB minimum
- 10% of the buffer pool
- 16GB maximum
You can adjust the memory later, as needed. For more information, seeEnable the database flag for vector embeddings.
For information about monitoring the size of your vector index, seeMonitor vector indexes.
To update the memory allocated for vector indexes on the instance, use thefollowing command:
gcloud sql instances patchINSTANCE_NAME \ --database-flags= cloudsql_vector_max_mem_size=NEW_MEMORY_VALUE;Replace the following:
- INSTANCE_NAME: the name of the instance on which you are changing thememory allocation.
- NEW_MEMORY_VALUE: the updated memory allocation, in bytes, for your vectorindexes.
This change takes effect immediately after a database restart.
Create a vector index
There are two ways to create a vector index:
CREATE VECTOR INDEXstatement,a Cloud SQL extensionto standard MySQL syntax.ALTER TABLEstatementwith the Cloud SQLADD VECTOR INDEXclause extension. Youcan't run this statement simultaneously with other DDL statements on thetable.
Use the following syntax to create a vector index usingCREATE VECTOR INDEX:
CREATEVECTORINDEXINDEX_NAMEONTABLE_NAME(COLUMN_NAME)USINGSCANN[QUANTIZER=SQ8]DISTANCE_MEASURE=L2_SQUARED|COSINE|DOT_PRODUCT[NUM_LEAVES=INT_VALUE{'</var>'}}];The following are the index options:
USING SCANN: optional. Indicates the index type to use.SCANN isthe only supported value.QUANTIZER: optional. Maps a high-dimensional vector to a compressedrepresentation.SQ8 is the only supported value.DISTANCE_MEASURE: required. Specifies a mathematical formula to use tocalculate the similarity of two vectors. You must set the same distancemeasure in this parameter as the distance you set in theapprox_distancesearch options. The supported literals are:L2_SQUAREDCOSINEDOT_PRODUCT
NUM_LEAVES: optional. Specifies how many partitions (leaves) to build.Only change this setting from its default setting if you have a goodunderstanding of ANN search and your dataset. The number specified can't belarger than the number of embeddings in the base table.
For example, to create a vector index, run the following:
CREATEVECTORINDEXvectorIndexONdbname.books(embeddings)DISTANCE_MEASURE=L2_SQUARED;While theCREATE statement is running, the base table is put into a read-onlymode and no DMLs are allowed on the base table.
You can use the following syntax to create an index on an existing table:
ALTERTABLEtbl_nameADDVECTORINDEXindex_name(key_part)[index_option];For example, to create an index on an existing table:
ALTERTABLEt1ADDVECTORINDEXindex1(j)USINGSCANNQUANTIZER=SQ8DISTANCE_MEASURE=l2_squaredNUM_LEAVES=10;Tune the vector index
This section gives further information about the parameters that you use tobuild the vector index. To tune the vector index, use this information todetermine how to influence the build process.
| Parameter | Description | Default | Scope | Impact |
cloudsql_vector_max_mem_size | Memory allocated for index training. | Varies | Instance | Insufficient memory can lead to build failures. SeeConfigure memory allocation for vector indexes. |
innodb_ddl_threads | Degree of parallelism for index training and build. | 4 | Session | Higher values reduce build time but increase CPU load. Set this value to the number of CPUs you can spare without adversely affecting database operations. |
Ensurecloudsql_vector_max_mem_size is configured appropriately for training.Adjustinnodb_ddl_threads to balance build time and CPU load, considering theimpact on concurrent database operations. Monitor CPU utilization during thebuild.
Drop a vector index
To drop a vector index, use the SQLDROP INDEX orALTER TABLE statementswith the index name you want to drop, as shown in the following:
DROPINDEXindex_nameONbooks;ALTERTABLEtable_nameDROPINDEXindex_name;Monitor vector indexes
Cloud SQL provides the following information schema tables withreal-time information about vector indexes that are loaded in its memory:
information_schema.innodb_vector_indexeslists all the vector indexes thatare opened in the memory after restart.information_schema.innodb_all_vector_indexeslists all the vectorindexes that exists on the instance (even if they aren't opened in thememory yet).information_schema.innodb_vector_indexes_memoryprovides information aboutoverall memory usage of vector indexes in the instance.
For more detailed information, see theInformation schema.
To view information in theinnodb_vector_indexes table, run the following command:
SELECT*FROMinformation_schema.innodb_vector_indexes\G;The output looks similar to the following:
INDEX_NAME:t1_vec_indexTABLE_NAME:test.t1INDEX_TYPE:TREE_SQDIMENSION:3DIST_MEASURE:COSINESTATUS:ReadySTATE:INDEX_READY_TO_USENUM_LEAVES:10NUM_LEAVES_TO_SEARCH:10QUERIES:1MUTATIONS:1TREE_MEMORY:443What's next
- Read theoverview about vector search on Cloud SQL.
- Learn how toenable and disable vector embeddings on your instance.
- Learn how togenerate vector embeddings.
- Learn how toperform searches on vector embeddings.
Except as otherwise noted, the content of this page is licensed under theCreative Commons Attribution 4.0 License, and code samples are licensed under theApache 2.0 License. For details, see theGoogle Developers Site Policies. Java is a registered trademark of Oracle and/or its affiliates.
Last updated 2025-12-15 UTC.