[ AliSQL | Vector Index | 向量索引 ]
AliSQL adds a VECTOR(N) column type for floating-point vectors with up to 16,383 dimensions. It provides Euclidean and cosine distance functions and an HNSW (Hierarchical Navigable Small World) index for approximate nearest-neighbor search.
Vector distance expressions and ordering can be combined with scalar predicates in standard SQL. Typical uses include semantic retrieval, recommendation, and multimodal search.
- Vector dimensions: up to 16,383 floating-point values
- ANN index: HNSW graph stored in an InnoDB auxiliary table
- Distance metrics:
EUCLIDEANandCOSINE - CPU paths: SIMD implementations for supported processors
- Search pruning: Bloom-filter-assisted batching in the search path
- Tuning: configurable graph degree, search width, and cache size
- SQL integration: vector distance expressions can be used with scalar predicates
Vector features are disabled by default. Enable them globally and use READ COMMITTED before creating or querying a vector index:
SET GLOBAL vidx_disabled = OFF;
SET SESSION transaction_isolation = 'READ-COMMITTED';VECTOR(N) is implemented by Field_vector, which derives from Field_varstring. Vector values are stored as binary floating-point arrays.
CREATE TABLE table_name (
id INT PRIMARY KEY,
vector_col VECTOR(3)
) ENGINE=InnoDB;
INSERT INTO table_name VALUES
(1, VEC_FROMTEXT('[1,2,3]')),
(2, VEC_FROMTEXT('[2,3,4]')),
(3, NULL);Vector indexes can be created using the following syntax:
CREATE VECTOR INDEX vidx_name ON table_name (vector_col); -- Using default parametersOr specify directly in table definition:
CREATE TABLE table_name (
id INT PRIMARY KEY,
vector_col VECTOR(3),
VECTOR INDEX vidx_name (vector_col) M=6 DISTANCE=COSINE -- Specifying parameters
) ENGINE=InnoDB;| Function Name | Meaning |
|---|---|
| VEC_FROMTEXT, TO_VECTOR, STRING_TO_VECTOR | String to vector |
| VEC_TOTEXT, FROM_VECTOR, VECTOR_TO_STRING | Vector to string |
| Function Name | Meaning |
|---|---|
| VECTOR_DIM | Vector dimension |
| VEC_DISTANCE, VEC_DISTANCE_EUCLIDEAN, VEC_DISTANCE_COSINE | Calculate distance between two vectors If one of the arguments is a column in the vector index, distance type does not need to be specified, the vector index distance type will be automatically recognized |
Usage examples:
-- Sort using vector distance
SELECT *
FROM table_name
ORDER BY VEC_DISTANCE(vector_col, VEC_FROMTEXT('[1,2,3]'))
LIMIT 10;
-- Display distance value in results
SELECT id,
VEC_DISTANCE_COSINE(vector_col, VEC_FROMTEXT('[1,2,3]')) AS distance
FROM table_name
ORDER BY distance
LIMIT 10;| Variable Name | Description | Type | Default Value | Range |
|---|---|---|---|---|
| vidx_disabled | Disable creation of vector columns and vector indexes | global | ON | ON, OFF |
| vidx_default_distance | Default vector distance type | global, session | EUCLIDEAN | EUCLIDEAN, COSINE |
| vidx_hnsw_default_m | HNSW algorithm default m | global, session | 6 | [3, 200] |
| vidx_hnsw_ef_search | HNSW algorithm default ef_search | global, session | 20 | [1, 10000] |
| vidx_hnsw_cache_size | HNSW node-cache memory limit in bytes | global | 16 MiB (16777216) | [1048576,18446744073709551615] |
M: Controls the number of connections for each node in the graph, default value is 6, valid range is 3 to 200DISTANCE: Distance type for building index, default value is EUCLIDEAN
- Vector-index operations require the
READ COMMITTEDtransaction isolation level. - Vector indexes are supported only on InnoDB tables.
- Creating, modifying, and deleting vector indexes cannot use
ALGORITHM=INPLACE. - Vector indexes cannot be set to
INVISIBLE. - Vector columns may be nullable. Rows whose vector value is
NULLare omitted from the vector index; scalar distance evaluation returnsNULLand places those rows last in ascending distance order. - Query vectors must have the same dimension as the indexed column.
- Creating and maintaining vector indexes consumes additional storage and compute resources.
- HNSW layer assignment and neighbor selection use randomized and heuristic steps. Replicas built from the same rows are not guaranteed to have byte-identical graph topology.
ER_NOT_SUPPORTED_YET: Unsupported transaction isolation levelER_WRONG_ARGUMENTS: Function argument errorER_VECTOR_INDEX_USAGE: Vector index usage errorER_VECTOR_INDEX_FAILED: Vector index operation failure
AliSQL currently implements approximate nearest-neighbor indexes with HNSW. The diagram below shows the SQL, plugin, cache, and storage components used by a vector query.
- The optimizer can select a vector index by cost, or the query can select one with an index hint such as
FORCE INDEX. - The HNSW graph is persisted in an InnoDB auxiliary table, with one row for each graph node.
- The vector-index plugin loads graph nodes into an in-memory cache and runs HNSW insertion and search over those nodes.
HNSW is an approximate nearest-neighbor algorithm built on a multilayer proximity graph:
- Layers: layer 0 contains every node; each higher layer contains a subset used for coarse navigation.
- Neighbors: nodes store a bounded set of nearby nodes, selected by vector distance and HNSW heuristics.
AliSQL uses two node caches with different lifetimes:
- MHNSW Share is attached to the auxiliary table's
TABLE_SHAREand is shared by read-only transactions. It avoids loading the same graph nodes from the table for every query. - MHNSW Trx is attached to the session through
thd_set_ha_data. A read-write transaction keeps accessed and modified nodes in its own cache, then updates the shared cache at commit.
- Precomputation: the node-loading path caches distance data used repeatedly during graph traversal.
- SIMD: supported CPU paths use SIMD instructions, including AVX-512, for batched distance calculations. Bloom filters group candidate checks before these calculations.
RDS MySQL provides managed vector storage with service-side enablement, data synchronization, backup, and recovery. Supported versions, console operations, and parameter defaults are specific to RDS and may differ from this source tree.


