← Back to Glossary

Vector database

A vector database stores high-dimensional numeric vectors (typically embeddings) and provides fast similarity search over them, powering use cases like semantic search and retrieval-augmented generation.

Overview

A vector database is a system optimized for storing dense numeric vectors — usually embeddings produced by a machine learning model — and answering nearest-neighbor queries: "which stored vectors are most similar to this query vector?" Because exact nearest-neighbor search is expensive at scale, most vector databases rely on approximate nearest-neighbor (ANN) index structures, such as HNSW (Hierarchical Navigable Small World graphs) or IVF, which trade a small amount of accuracy for large speedups. Purpose-built vector databases (like Pinecone, Weaviate, or Milvus) exist alongside vector search capabilities added to general-purpose databases.

Similarity metrics

Vector similarity is typically measured with cosine similarity, Euclidean (L2) distance, or dot/inner product, and the choice of metric should match how the embedding model was trained.

Vector search in DuckDB

DuckDB supports vector similarity search through its vss extension, which adds an HNSW index type and built-in distance functions over DuckDB's native ARRAY (fixed-size) type.

Copy code

INSTALL vss; LOAD vss; CREATE TABLE docs (id INTEGER, embedding FLOAT[384]); CREATE INDEX docs_hnsw_idx ON docs USING HNSW (embedding) WITH (metric = 'cosine'); SELECT id, array_cosine_similarity(embedding, [0.1, 0.2, ...]::FLOAT[384]) AS score FROM docs ORDER BY score DESC LIMIT 10;

Built-in functions include array_distance (Euclidean), array_cosine_similarity, and array_inner_product. The HNSW index must fit entirely in memory, which makes DuckDB's vector search well suited to small-to-medium embedding sets queried locally, rather than the massive, horizontally-scaled indexes that dedicated vector database services target.

Why it matters

Vector search underpins semantic search (finding conceptually similar text rather than exact keyword matches) and retrieval-augmented generation (RAG), where relevant document chunks are retrieved by embedding similarity before being passed to a language model as context.

Related terms

FAQS

DuckDB isn't marketed as a dedicated vector database, but its vss extension adds HNSW-indexed vector similarity search over array columns, which is enough for many small-to-medium embedding search workloads run locally or embedded in an application.

Exact search compares a query vector against every stored vector and is guaranteed correct but slow at scale. Approximate nearest-neighbor (ANN) methods like HNSW use an index structure to find very likely nearest neighbors much faster, at a small, tunable risk of missing the true closest match.