mariadb-vector-functions

Documents MariaDB vector distance functions and VECTOR index usage for embedding queries.

28|115|Updated Jan 28, 2025
One-click install
npx skills add https://github.com/mariadb-corporation/mariadb-docs --skill mariadb-vector-functions-mariadb-corporation
Or copy as Structured Prompt for Agent▼
Please help me install this Agent Skill.
Skill: mariadb-vector-functions
Source: https://github.com/mariadb-corporation/mariadb-docs/tree/main/agent-skills/granular/functions/mariadb-vector-functions
Command: npx skills add https://github.com/mariadb-corporation/mariadb-docs --skill mariadb-vector-functions-mariadb-corporation

SYSTEM DOCUMENTATION & REQUIREMENTS

What problem does it solve? Writing vector similarity search SQL in MariaDB is error-prone because the feature is new (11.7 preview, 11.8 LTS GA), has no pgvector-style distance operators, and imposes strict index-matching rules. This Skill provides the correct function signatures, version constraints, and common pitfalls so generated SQL works the first time. ## Core Features & Use Cases - Function Catalog: Reference for VEC_DISTANCE, VEC_DISTANCE_EUCLIDEAN, VEC_DISTANCE_COSINE, VEC_FromText, and VEC_ToText with signatures and semantics. - Pitfall Corrections: Maps common wrong assumptions (pgvector <-> operators, automatic JSON array conversion, normalized similarity scores) to the correct MariaDB forms. - Index Usage Rules: Explains when ORDER BY ... LIMIT actually uses the VECTOR INDEX, the one-index-per-table limit, and metric matching requirements. - Use Case: When asked to write a k-nearest-neighbor query over stored embeddings, produce ORDER BY VEC_DISTANCE_EUCLIDEAN(embedding, VEC_FromText('[...]')) LIMIT 10 with the correct index and version checks. ## Quick Start Write a MariaDB 11.8 query that finds the 10 nearest embeddings to a given vector using cosine distance and tell me whether it will use the vector index.

Frequently Asked Questions about mariadb-vector-functions

High-intent search queries and answers about installing and using this skill.

FAQPage Schema
How do I write a vector similarity search query in MariaDB?▼

Use ORDER BY with a distance function and LIMIT, for example ORDER BY VEC_DISTANCE_EUCLIDEAN(col, VEC_FromText('[1,2,3]')) LIMIT 10. MariaDB has no pgvector-style operators like `<->` or `<=>`; distance functions are the only form.

Which MariaDB versions support the VECTOR data type and vector functions?▼

The vector feature landed as a preview in MariaDB 11.7.1 and is GA in 11.8, the first LTS to include it. It does not exist in 10.6, 10.11, or 11.4, so code targeting those branches must not use it.

Why is my MariaDB vector query not using the VECTOR INDEX?▼

The index is used only when ORDER BY is the literal distance call ascending combined with LIMIT, and the function matches the index's metric (euclidean by default or cosine). A mismatched metric, a bare WHERE threshold without LIMIT, or wrapping the distance in an expression falls back to a full table scan.

Does MariaDB have a dot product or inner product distance function?▼

No, MariaDB deliberately omits dot-product distance because it is not a proper distance metric and offers no speed advantage in its implementation. Use VEC_DISTANCE_EUCLIDEAN or VEC_DISTANCE_COSINE, which are equally fast for normalized vectors.

How do I insert a JSON array into a MariaDB VECTOR column?▼

Wrap the array string explicitly with VEC_FromText, for example VEC_FromText('[1,2,3]'). A VECTOR column stores little-endian IEEE-754 float32 bytes, not JSON text, so bare array strings are not converted automatically.

Can a MariaDB table have multiple vector indexes for different metrics?▼

No, MariaDB allows only one vector index per table, and the indexed column must be NOT NULL. The DISTANCE option chosen at CREATE TABLE time locks in the metric that queries must match to use the index.