Vector Search with ClickHouse - Part 1
Vector search replaces term-based inverted indexes, which use TF/IDF or BM25 scoring and can't capture meaning or handle vocabulary mismatch, with embeddings: arrays of floats where semantically similar text, images or audio sit close together under cosine or Euclidean distance. Exact KNN linear search compares the query against every vector in O(n) and rarely scales past ~100M vectors without heavy parallelism or GPUs. Approximate nearest neighbour methods such as HNSW and Annoy give up some recall for speed. Annoy, for example, recursively partitions space into a tree and only scans the leaf nearest the query. ClickHouse stores vectors as Array(Float32) columns with distance functions, and embeddings are typically generated by transformer models such as BERT or by CNNs and ViTs for images.
Related posts
- ANN Vector Search with SQL-powered LSH & Random Projections
ClickHouse mediumAdept
Approximate nearest neighbor search can be built in pure ClickHouse SQL using locality-sensitive hashing with random projections. Random hyperplanes, each defined by the difference and midpoint of two sampled vectors, turn every embedding into a 128-bit hash. Candidates are then found by Hamming distance and optionally re-scored with an exact distance function. Tested on the 2.1M-vector, 300-dimension GloVe dataset, the approach uses a MergeTree table ordered by the hash with a low index_granularity. It trades some recall for faster queries while staying disk-based, easy to update, and compatible with metadata filtering, unlike in-memory indexes such as HNSW.
- Vector Search with ClickHouse - Part 2
ClickHouse mediumAdept
ClickHouse can serve as a vector store by keeping embeddings in Array(Float32) columns and computing distance functions in SQL. The walkthrough uses the 2.2-billion-row English LAION dataset of CLIP ViT-L/14 image and caption embeddings, covering schema design, loading from S3 Parquet, compression, and exact linear-scan search combined with metadata filtering and aggregation. It also covers the experimental Annoy ANN index, UDFs for generating query vectors, and codec tuning. ClickHouse fits best when vector matching needs SQL filtering, joins or aggregation over datasets too large for memory. Dedicated vector databases or in-memory indexes suit small, very high-QPS workloads better.
- pgvector v0.5.0: Faster semantic search with HNSW indexes
Supabase mediumAdeptpgvector v0.5.0 adds HNSW indexes, which combine skip-list-style layering with navigable small world proximity graphs to do approximate nearest-neighbor search over embeddings. In Supabase benchmarks on 1536-dimension OpenAI vectors, HNSW delivered about 3x the QPS of IVFFlat on 224K vectors and over 6x on 1M vectors at equal accuracy. Tuning ef_search, m and ef_construction trades speed against recall; raising ef_construction to 80 reached 0.99 accuracy@10 with ef_search 100 and 35% more QPS. HNSW can be built on an empty table and stays optimal as data arrives, but it builds more slowly and uses more memory than IVFFlat, whose build times dropped over 50% thanks to parallel list assignment and float-based FMA distance math.
- Understanding vector search and HNSW index with pgvector
Neon mediumAdept
pgvector 0.5.0 adds an HNSW (Hierarchical Navigable Small Worlds) index for approximate nearest neighbor search in Postgres, replacing O(N) sequential exact scans with a multi-layer graph traversal that trades a little accuracy for much faster queries. A walkthrough covers vector embeddings, L2 and cosine distance with worked examples, and pgvector SQL for storing and querying vectors with the <=> operator. Index tuning hinges on m (graph degree), ef_construction (build-time candidate list) and ef_search (query-time beam size): higher values improve recall at the cost of build time, memory or query latency. HNSW is faster and higher-recall than IVF but resource-intensive to build, often requiring more RAM and a larger maintenance_work_mem.
- 20x faster than pgvector: introducing pg_embedding extension for vector search in Postgres and LangChain
Neon mediumAdept
pg_embedding brought HNSW graph-based approximate nearest neighbor indexing to Postgres. On the GIST-960 dataset (1M 960-dim vectors, k=100, 4 vCPU/16 GB), it ran 5–30x faster than pgvector's IVFFlat at equal recall. IVFFlat needs more probes to raise recall and drifts toward a sequential scan, while HNSW's layered graph gives roughly logarithmic search. HNSW trades higher memory use, slower index builds (driven by efConstruction) and L2-only distance for that speed. Neon has since deprecated pg_embedding in favor of pgvector, which now supports HNSW natively.