DBAtlas · Database operations atlas

Vector search (AI/embeddings)

Oracle

-- Oracle 23ai native VECTOR type:
CREATE TABLE docs (id NUMBER, content CLOB, embedding VECTOR);
-- Similarity search (cosine distance):
SELECT id FROM docs
ORDER BY VECTOR_DISTANCE(embedding, :query_vector, COSINE)
FETCH FIRST 5 ROWS ONLY;

Oracle Database 23ai offers a native VECTOR data type and the AI Vector Search feature. The VECTOR_DISTANCE function computes similarity with the COSINE/EUCLIDEAN/DOT metrics; HNSW and IVF vector indexes are supported for approximate search. Embeddings are generated in-database or with external models.

PostgreSQL

-- pgvector extension:
CREATE EXTENSION IF NOT EXISTS vector;
CREATE TABLE docs (id int, embedding vector(1536));
CREATE INDEX ON docs USING hnsw (embedding vector_cosine_ops);
-- 5 nearest neighbors (<-> Euclidean, <=> cosine):
SELECT id FROM docs ORDER BY embedding <=> :query_vec LIMIT 5;

The pgvector extension adds a vector type and similarity operators to PostgreSQL: <-> (L2/Euclidean), <=> (cosine), <#> (inner product). HNSW or IVFFlat indexes speed up approximate nearest neighbor (ANN) search. Widely used in RAG and semantic search applications.

MySQL

-- MySQL 9.0+ native VECTOR type:
CREATE TABLE docs (id INT, embedding VECTOR(1536));
-- Cosine distance (DISTANCE is HeatWave-only):
SELECT id, DISTANCE(embedding, :query_vec, 'COSINE') AS d
FROM docs ORDER BY d LIMIT 5;

MySQL 9.0 added the native VECTOR data type and the STRING_TO_VECTOR/VECTOR_TO_STRING and VECTOR_DIM functions (also in Community). The DISTANCE() function and approximate vector search at scale are available only in MySQL HeatWave in Oracle Cloud; in Community/Enterprise, compute distances in the application or an external vector store. Older on-premises versions require an external vector store.

SQL Server

-- SQL Server 2025 native vector type:
CREATE TABLE docs (id INT, embedding VECTOR(1536));
-- Nearest neighbors by cosine distance:
SELECT TOP 5 id,
  VECTOR_DISTANCE('cosine', embedding, @query_vec) AS distance
FROM dbo.docs ORDER BY distance;

SQL Server 2025 (and Azure SQL) offer a native vector data type and the VECTOR_DISTANCE function (cosine/euclidean/dot); DiskANN-based vector indexes for approximate nearest neighbor search are under development. Embeddings are generated with external models such as Azure OpenAI and stored.

MongoDB

// Atlas Vector Search ($vectorSearch aggregation stage):
db.docs.aggregate([
  { $vectorSearch: {
      index: "vector_idx",
      path: "embedding",
      queryVector: [0.12, 0.87, /* ... */],
      numCandidates: 100,
      limit: 5
  } }
])

MongoDB Atlas Vector Search queries embeddings with the $vectorSearch aggregation stage; under the hood it performs HNSW-based approximate nearest neighbor search. The vector index is defined as an Atlas Search index. This feature is specific to the managed Atlas service (not available in the on-premises Community edition).

Redis

# Requires the RediSearch (Redis Stack) VECTOR field type.
# Define an HNSW vector index:
FT.CREATE docs_idx ON HASH PREFIX 1 docs: SCHEMA
  embedding VECTOR HNSW 6 TYPE FLOAT32 DIM 1536 DISTANCE_METRIC COSINE
# KNN search (with a query vector):
FT.SEARCH docs_idx "*=>[KNN 5 @embedding $vec]" PARAMS 2 vec "..."

Redis provides vector search through the RediSearch module (Redis Stack); the VECTOR field type supports FLAT (exact) or HNSW (approximate) indexes and the COSINE/L2/IP metrics. Nearest neighbors are retrieved with the KNN syntax inside FT.SEARCH. Used for low-latency semantic search and RAG.

Elasticsearch

# dense_vector field and kNN search:
PUT docs
{ "mappings": { "properties": { "embedding":
  { "type": "dense_vector", "dims": 1536, "similarity": "cosine" } } } }
GET docs/_search
{ "knn": { "field": "embedding", "query_vector": [0.12, 0.87],
  "k": 5, "num_candidates": 100 } }

Elasticsearch supports approximate nearest neighbor (HNSW) queries with the dense_vector field type and built-in knn search; similarity can be cosine/dot_product/l2_norm. Relevance can be improved by combining vector search with a classic text query (hybrid search / RRF).

Open in DBAtlas →