All Products
Search
Document Center

ApsaraDB RDS:Vector storage

Last Updated:Mar 28, 2026

RDS for MySQL natively supports storing and querying vector data of up to 16,383 dimensions. Using the VECTOR data type and a built-in Hierarchical Navigable Small World (HNSW) index, you can run approximate nearest neighbor (ANN) searches directly in SQL — no separate vector database required.

How it works

  1. Enable vector support on your RDS for MySQL instance.

  2. Create a table with a VECTOR column and an HNSW index, specifying the distance metric and graph parameters.

  3. Insert vector data using VEC_FROMTEXT or any equivalent conversion function.

  4. Query the nearest neighbors with VEC_DISTANCE and ORDER BY ... LIMIT.

The HNSW index uses single instruction multiple data (SIMD) hardware acceleration, Bloom filter search pruning, and LIMIT condition pushdown to accelerate large-scale retrieval. Vector and scalar columns can coexist in the same table, so you can combine full-text filters with vector similarity in a single query.

RDS for MySQL is fully compatible with the MySQL protocol. It supports Java Database Connectivity (JDBC), Object-Relational Mapping (ORM) tools, and mainstream developer frameworks. It integrates with Alibaba Cloud services such as DTS and DMS to provide full lifecycle capabilities, including data synchronization, management, backup, and recovery. You can upgrade existing instances with a single click without creating new clusters.

Prerequisites

Before you begin, ensure that you have:

Enable vector storage

Enabling or disabling vector storage does not require an instance restart.
  1. Go to the Instances page. In the top navigation bar, select the region where your instance resides, then click the instance ID.

  2. On the Basic Information page, find the Running Status section. Next to Vector Storage, click Enable.

  3. When the status changes to Enabled, the feature takes effect immediately.

Get started

Step 1: Create a table with a vector index

The following example creates a table with a 5-dimension embedding column and an HNSW index that uses cosine distance.

-- Create a table with a 5-dimension vector column and an HNSW index
CREATE TABLE product_embeddings (
  id INT NOT NULL AUTO_INCREMENT PRIMARY KEY,
  product_name VARCHAR(255),
  embedding VECTOR(5) NOT NULL,
  -- Specify M (graph connectivity) and the distance metric
  VECTOR INDEX idx_embedding(embedding) M=16 DISTANCE=COSINE
);

Step 2: Insert vector data

Use VEC_FROMTEXT to convert a JSON-formatted array string to the VECTOR type.

INSERT INTO product_embeddings (product_name, embedding) VALUES
('product_A', VEC_FROMTEXT('[0.1, 0.2, 0.3, 0.4, 0.5]')),
('product_B', VEC_FROMTEXT('[0.6, 0.7, 0.8, 0.9, 1.0]')),
('product_C', VEC_FROMTEXT('[0.11, 0.22, 0.33, 0.44, 0.55]'));

Step 3: Query nearest neighbors

VEC_DISTANCE auto-detects the distance metric from the index when the column has a vector index. Order results by the score in ascending order and use LIMIT to retrieve the top-k results.

-- Return the 2 products most similar to the query vector
SELECT
  id,
  product_name,
  VEC_DISTANCE(embedding, VEC_FROMTEXT('[0.1, 0.2, 0.3, 0.4, 0.51]')) AS similarity_score
FROM product_embeddings
ORDER BY similarity_score ASC  -- smaller cosine distance = more similar
LIMIT 2;

Supported functions

FunctionDescription
VECTOR_DIMReturns the number of dimensions in a vector
VEC_FROMTEXT / TO_VECTOR / STRING_TO_VECTORConverts a JSON array string to a VECTOR value
VEC_TOTEXT / FROM_VECTOR / VECTOR_TO_STRINGConverts a VECTOR value to a JSON array string
VEC_DISTANCECalculates the distance between two vectors; auto-detects the index distance metric when one operand is an indexed column
VEC_DISTANCE_EUCLIDEANCalculates the Euclidean distance between two vectors
VEC_DISTANCE_COSINECalculates the cosine distance between two vectors

Configure parameters

All vector-related parameters are dynamic — changes take effect immediately without restarting the instance.

Parameter reference

ParameterScopeTypeDefaultValid values
vidx_default_distanceSessionStringEUCLIDEANEUCLIDEAN, COSINE
vidx_hnsw_default_mSessionInteger63200
vidx_hnsw_ef_searchSessionInteger20110000
vidx_hnsw_cache_sizeGlobalBigInt1048576 (bytes)104857618446744073709551615

Parameter descriptions:

  • `vidx_default_distance` — The default distance metric for vector operations.

    • EUCLIDEAN: Straight-line distance in multi-dimensional space. Use this when absolute magnitude matters.

    • COSINE: Cosine of the angle between two vectors. Use this when directional similarity matters and vector length is irrelevant.

  • `vidx_hnsw_default_m` — Maximum number of outgoing edges per node in the HNSW graph. Higher values improve recall at the cost of more memory and slower index builds.

  • `vidx_hnsw_ef_search` — Search scope during ANN queries. Higher values improve recall but increase query latency.

  • `vidx_hnsw_cache_size` — Maximum memory the HNSW index cache may use, in bytes.

Modify parameters

  1. Go to the Instances page, select the region, and click the instance ID.

  2. In the left navigation pane, click Parameter Settings.

  3. On the Editable Parameters tab, search for the parameter and set its value.

  4. Click OK, then click Submit Parameters. In the dialog that appears, choose when to apply the changes.

Limitations

Vector index requirements

  • Vector indexes are supported only on tables using the InnoDB storage engine.

  • The primary key of a table with a vector index cannot exceed 256 bytes.

  • The inplace syntax cannot be used to create, modify, or delete vector indexes.

  • Vector indexes cannot be set to INVISIBLE.

  • Tables with vector indexes do not support the Recycle Bin feature.

Query and isolation level

  • Data modifications and queries on vector indexes support only the Read Committed (RC) isolation level.

Primary/standby consistency

  • Because the HNSW algorithm involves randomness (random levels and heuristic graph construction), the index graph structures on the primary and standby instances are not guaranteed to be identical. Query results may differ slightly between the two.

Data migration

  • If stored procedures or functions in the source database use the vector data type, synchronization or migration to a destination database that does not support vectors will fail.

What's next