Percona's Database 9.7 Adds Vector Distance Scoring in SQL
Percona's open-source database server version 9.7.2-2 now lets developers compute vector similarity directly in SQL. The new DISTANCE() and VECTOR_DISTANCE() functions, announced by Catalin Besleaga on September 21, 2026, support COSINE, EUCLIDEAN, EUCLIDEAN_SQUARED, MANHATTAN and DOT metrics. The upstream database server's native distance functions are only available in HeatWave, a managed cloud database service on a major enterprise cloud provider's infrastructure, and they cover only three metrics (COSINE, DOT, EUCLIDEAN). Percona brings the same capability to anyone running this open-source database server on any supported platform. The feature was described in a Percona blog post written by Catalin Besleaga and reviewed by Dennis Kittrell and Peter Zaitsev.
A vector is a fixed-length list of numbers, often called an embedding, produced by a machine learning model to represent something such as text, an image, a product or a user preference. Similar things end up with numerically close vectors, so computing the distance between two vectors allows ranking or filtering by similarity. This open-source database server version 9.7 already supports the VECTOR data type, along with TO_VECTOR() and FROM_VECTOR() functions, for storing embeddings. A distance metric is a mathematical function that turns two vectors into a single number describing similarity. This release supports five metrics. EUCLIDEAN (L2) is straight-line geometric distance and works well when magnitude matters. EUCLIDEAN_SQUARED skips the square root, keeping the same ranking but running faster for comparisons and ORDER BY clauses when only ordering matters. MANHATTAN (L1) is grid-style distance, like taxicab distance on a city grid, and can be more robust to outliers in some applications. COSINE similarity measures the angle between two vectors; cosine distance is computed as 1 minus cosine similarity so that smaller values mean more similar. DOT is raw dot product sum(a[i]*b[i]), used by models trained specifically for dot-product similarity; the industry commonly multiplies by -1 to flip the scale, so smaller values mean more similar, aligned with the other metrics.
The function signature is DISTANCE(vector1, vector2, metric). The two vector arguments can be VECTOR data type values or binary string literals created with TO_VECTOR(). The metric must be a fixed literal string, case-insensitive, not a column or expression. VECTOR_DISTANCE() is an alias with identical behavior. The function returns a DOUBLE. All dimensions must match; if either input is NULL, the result is NULL. To match metric to model, most modern embedding models such as OpenAI and Cohere are trained with COSINE similarity, so use COSINE. If magnitude carries meaning, use EUCLIDEAN or EUCLIDEAN_SQUARED; when in doubt, check the model documentation. A step-by-step example creates a products table with a VECTOR(1536) column, inserts rows with TO_VECTOR('[0.1, 0.2, 0.3, ..., 0.384]'), and queries the top five products with DISTANCE(embedding, TO_VECTOR('[0.12, 0.22, 0.32, ..., 0.382]'), 'EUCLIDEAN') ORDER BY similarity_score LIMIT 5. The example results show Product B first with 0.5432109, then Product A with 0.9654321, then Product C with 0.9876543, ordered by highest similarity first.
Under the hood, the implementation uses SIMD (Single Instruction, Multiple Data), a CPU capability that performs the same arithmetic on many numbers at once. Distance math over hundreds of dimensions is exactly the repetitive arithmetic SIMD accelerates. Percona detects the CPU capabilities at startup and selects the best available SIMD tier, no recompilation needed per target architecture. The tiers are SSE4.2 or NEON at 128-bit (about 2 to 3 times speedup over scalar), AVX2 at 256-bit (about 4 to 6 times), AVX-512F at 512-bit (about 8 to 12 times), SVE2 on ARM (scalable SIMD that adapts to CPU vector width), and scalar fallback that works everywhere. Dimension-aware kernel selection means distance calculations on small vectors with fewer than 16 dimensions use the narrower 128-bit tier because the overhead of setting up larger SIMD registers outweighs the benefit; larger vectors automatically use the widest available tier. Unaligned loads are used by design: VECTOR column data is not guaranteed to be cache-line aligned and should not require alignment. Modern CPUs have identical throughput for aligned and unaligned loads when data is in cache, so all SIMD kernels use unaligned-load intrinsics to avoid faulting on misaligned input without sacrificing performance.
DISTANCE() is a scalar function that computes the distance between two vectors and returns a single number. Without an approximate-nearest-neighbor (ANN) index, a query like ORDER BY DISTANCE(...) LIMIT k over a large table is a full table scan: the database calls the distance function on every row and then sorts. It is correct and SIMD-accelerated per row, but it scales as O(n), not O(log n). The natural next step is ANN indexing, such as HNSW (Hierarchical Navigable Small World) or IVF (Inverted File with refinement), which prune the search space and return approximate results much faster. That direction is on the roadmap. While ANN indexing delivers speed, it relies on approximate results; should an application require exact precision instead of estimates, DISTANCE() is available in this open-source database server version 9.7.2-2.
Percona invites users to try the feature and share feedback: report bugs or feature ideas on JIRA, join the conversation on the Percona community forum, or reach out with usage or performance questions. The feedback will shape the roadmap, especially use cases such as specific ANN index strategies, embedding model integrations, and performance tuning. For teams running this open-source database in production, a managed database service such as AEU Data can handle automated backups, encrypted storage, monitoring and high availability, letting them focus on query and index design including the new distance functions.