Create and manage vector indexes

MySQL  |  PostgreSQL  |  SQL Server

To use the GA version of vector search, the instance maintenance version shouldbe versionMYSQL_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:

  1. Storing the vector index structure: the non-leaf portion of the vectorindex (theTREE_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 * 2

    For example, an index with 1000 leaves and 768 dimensions would have anapproximateTREE_MEMORY of 1000 * 768 * 4 * 2 or 6144000 bytes. You canalso check the actualTREE_MEMORY using theinformation_schema.innodb_vector_indexes table. 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.

  2. 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 dimensions

    For example, with a table of 1,000,000 rows and 768 dimensions, thetraining_memory would 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 thecloudsql_vector flag, Cloud SQLautomatically sets a defaultcloudsql_vector_max_mem_size based on your VMsize. This default usually suffices for typical workloads.Cloud SQL reduces theinnodb_buffer_pool_size flag toallocate this memory. The default maximum value forcloudsql_vector_max_mem_size is 16GB. If you need to tune your memorysize, you can dynamically adjustcloudsql_vector_max_mem_size based onyour vector index usage.

    Important: If you increasecloudsql_vector_max_mem_size, you mustcorrespondingly decreaseinnodb_buffer_pool_size to avoid memory issues.

cloudsql_vector_max_mem_size values

VM sizecloudsql_vector_max_mem_size
4GB194MB
8GB515MB
16GB1.2GB
32GB2.56GB
64GB5.12GB
128GB10.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:

Note: The size of your base table is calculated by scanning the table for thenumber of rows with non-NULL entries in the vector embeddings column.

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_SQUARED
    • COSINE
    • DOT_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.

ParameterDescriptionDefaultScopeImpact
cloudsql_vector_max_mem_sizeMemory allocated for index training.VariesInstanceInsufficient memory can lead to build failures. SeeConfigure memory allocation for vector indexes.
innodb_ddl_threadsDegree of parallelism for index training and build.4SessionHigher 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_indexes lists all the vector indexes thatare opened in the memory after restart.
  • information_schema.innodb_all_vector_indexes lists all the vectorindexes that exists on the instance (even if they aren't opened in thememory yet).
  • information_schema.innodb_vector_indexes_memory provides 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:443

What's next

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.