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
Enable vector support on your RDS for MySQL instance.
Create a table with a
VECTORcolumn and an HNSW index, specifying the distance metric and graph parameters.Insert vector data using
VEC_FROMTEXTor any equivalent conversion function.Query the nearest neighbors with
VEC_DISTANCEandORDER 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:
An RDS for MySQL instance running MySQL 8.0
Minor engine version 20251031 or later — if not, upgrade the minor engine version or upgrade the major engine version
Enable vector storage
Enabling or disabling vector storage does not require an instance restart.
Go to the Instances page. In the top navigation bar, select the region where your instance resides, then click the instance ID.
On the Basic Information page, find the Running Status section. Next to Vector Storage, click Enable.
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
| Function | Description |
|---|---|
VECTOR_DIM | Returns the number of dimensions in a vector |
VEC_FROMTEXT / TO_VECTOR / STRING_TO_VECTOR | Converts a JSON array string to a VECTOR value |
VEC_TOTEXT / FROM_VECTOR / VECTOR_TO_STRING | Converts a VECTOR value to a JSON array string |
VEC_DISTANCE | Calculates the distance between two vectors; auto-detects the index distance metric when one operand is an indexed column |
VEC_DISTANCE_EUCLIDEAN | Calculates the Euclidean distance between two vectors |
VEC_DISTANCE_COSINE | Calculates the cosine distance between two vectors |
Configure parameters
All vector-related parameters are dynamic — changes take effect immediately without restarting the instance.
Parameter reference
| Parameter | Scope | Type | Default | Valid values |
|---|---|---|---|---|
vidx_default_distance | Session | String | EUCLIDEAN | EUCLIDEAN, COSINE |
vidx_hnsw_default_m | Session | Integer | 6 | 3–200 |
vidx_hnsw_ef_search | Session | Integer | 20 | 1–10000 |
vidx_hnsw_cache_size | Global | BigInt | 1048576 (bytes) | 1048576–18446744073709551615 |
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
Go to the Instances page, select the region, and click the instance ID.
In the left navigation pane, click Parameter Settings.
On the Editable Parameters tab, search for the parameter and set its value.
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
inplacesyntax 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
vectordata type, synchronization or migration to a destination database that does not support vectors will fail.