Skip to content

In development. Expect rough edges and changes. The source code is not public yet.

ClickHouse 36 min read How-to mediumAdept

Vector Search with ClickHouse - Part 2

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.

Related posts

  • Vector Search with ClickHouse - Part 1

    ClickHouse easyNovice

    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.

  • 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.

  • We built a vector search engine that lets you choose precision at query time

    ClickHouse hardMaster

    QBit is a new ClickHouse column type that stores BFloat16, Float32 or Float64 vectors as transposed bit planes, each held in a FixedString subcolumn. Vector search can then read only the top N bit planes and choose precision at query time, trading recall for I/O and compute without duplicating data or committing to irreversible quantisation. Distance functions like L2DistanceTransposed reconstruct truncated floats with AVX-512 SIMD untransposition, and downcast only the reference vector when the bit layout matches, as with Float32 to BFloat16. Search remains O(n), so HNSW is still faster when its index fits in RAM.

  • SQLite Retrieval Augmented Generation and Vector Search

    Turso mediumAdept

    Retrieval-augmented generation fixes stale or hallucinated LLM answers by retrieving relevant data, typically via embeddings and vector similarity search, and injecting it into the prompt. libSQL adds native vector search to SQLite: embeddings are stored in an F32_BLOB column (SQLite treats column types as hints), inserted with a vector() function, and queried with vector_distance_cos() plus ORDER BY/LIMIT for exact top-k results. That exact search is a full table scan and only scales to small datasets, so larger collections need approximate nearest-neighbour indexing, which a follow-up post will cover.

  • ClickHouse Release 25.5

    ClickHouse mediumAdept

    ClickHouse 25.5 ships 15 new features, 23 performance optimizations and 64 bug fixes. The headline change is the HNSW vector similarity index moving from experimental to beta, with pre- and post-filtering for hybrid search. Other additions include Hive metastore catalog support for querying Iceberg tables, an implicit `table` in clickhouse-local so SELECT/FROM can be omitted for stdin input, an `introduced_in` field in system.functions, and correct parsing of Geo types from Parquet. Each feature comes with a runnable SQL example.

The quest for software engineering knowledge.

Look:
Layout: