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
Embeddings are dense numeric vector representations of data — text, images, audio, or other objects — learned so that semantically similar inputs end up close together in vector space.
Full-text search →Full-text search is a technique for searching text content by relevance to a query — matching keywords, handling stemming and ranking results — rather than requiring an exact substring or equality match.
Lance format →Lance is a modern, open-source columnar file format designed for machine learning and vector-search workloads, offering fast random access, versioning, and native support for embeddings alongside tabular data.
LIST and ARRAY type →LIST and ARRAY are nested SQL data types for storing an ordered collection of values in a single column — LIST is variable-length, ARRAY is fixed-length.
Vectorized execution →Vectorized execution is a query processing model where each operator processes a batch (a "vector") of values at once, instead of one row at a time, to reduce interpretation overhead and better use modern CPUs.
Database index →A database index is a data structure that stores a subset of a table's data in an order optimized for fast lookups, letting queries find matching rows without scanning the whole table.
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.
