---
title: "ANN Vector Search with SQL-powered LSH & Random Projections"
description: "Approximate nearest neighbor search can be built in pure ClickHouse SQL using locality-sensitive hashing with random projections."
url: "https://swe.quest/articles/ann-vector-search-with-sql-powered-lsh-random-projections-ab84d00f3cdada4d"
canonical: "https://clickhouse.com/blog/approximate-nearest-neighbour-ann-with-sql-powered-local-sensitive-hashing-lsh-random-projections"
source: "ClickHouse"
published: "2023-12-05"
difficulty: "medium"
kind: "Deep dive"
reading_minutes: 29
tags: ["vector-search","analytics","sql","algorithms"]
---

# ANN Vector Search with SQL-powered LSH & Random Projections

A post on [ClickHouse](https://swe.quest/sources/clickhouse.md), published 2023-12-05, 29 min read, Deep dive. Original: [https://clickhouse.com/blog/approximate-nearest-neighbour-ann-with-sql-powered-local-sensitive-hashing-lsh-random-projections](https://clickhouse.com/blog/approximate-nearest-neighbour-ann-with-sql-powered-local-sensitive-hashing-lsh-random-projections)

## Summary

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.

Written by swe.quest, not by the author.

**Difficulty:** Medium: assumes a working developer; shows how it is built or done

## Topics

- [Vector search](https://swe.quest/tags/vector-search.md) (Data & databases)
- [Analytics & OLAP](https://swe.quest/tags/analytics.md) (Data & databases)
- [SQL](https://swe.quest/tags/sql.md) (Languages)
- [Algorithms](https://swe.quest/tags/algorithms.md) (Architecture & design)

## Prerequisites

- vector embeddings ([easy posts on RAG & retrieval](https://swe.quest/tags/rag.md?difficulty=easy))
- cosine similarity
- ClickHouse MergeTree basics ([easy posts on Analytics & OLAP](https://swe.quest/tags/analytics.md?difficulty=easy))
- SQL ([easy posts on SQL](https://swe.quest/tags/sql.md?difficulty=easy))

## Keywords

ClickHouse, Locality Sensitive Hashing, random projections, GloVe, Hamming distance, HNSW, Annoy, MergeTree

## Related posts

- [Vector Search with ClickHouse - Part 1](https://swe.quest/articles/vector-search-with-clickhouse-part-1-c538ccf9d6af206b.md): ClickHouse · 2023-05-31 · Easy · 14 min read · Vector search, Search, Machine learning
  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.
- [Vector Search with ClickHouse - Part 2](https://swe.quest/articles/vector-search-with-clickhouse-part-2-3f7737e1f27114da.md): ClickHouse · 2023-05-31 · Medium · 36 min read · Vector search, Analytics & OLAP, Databases, SQL
  ClickHouse can serve as a vector store by keeping embeddings in Array(Float32) columns and computing distance functions in SQL.
- [pgvector v0.5.0: Faster semantic search with HNSW indexes](https://swe.quest/articles/pgvector-v0-5-0-faster-semantic-search-with-hnsw-indexes-d1e5c38ac44f551e.md): Supabase · 2023-09-06 · Medium · 6 min read · Vector search, PostgreSQL, Performance engineering, Algorithms
  pgvector 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.
- [Understanding vector search and HNSW index with pgvector](https://swe.quest/articles/understanding-vector-search-and-hnsw-index-with-pgvector-db3498f2e1e5486e.md): Neon · 2023-09-20 · Medium · 10 min read · Vector search, PostgreSQL, LLMs
  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.
- [20x faster than pgvector: introducing pg\_embedding extension for vector search in Postgres and LangChain](https://swe.quest/articles/20x-faster-than-pgvector-introducing-pg-embedding-extension-for-vector-search-058737fc9388f495.md): Neon · 2023-07-11 · Medium · 7 min read · Vector search, PostgreSQL, Performance engineering
  pg\_embedding brought HNSW graph-based approximate nearest neighbor indexing to Postgres.

Browse: [Latest posts](https://swe.quest/index.md) · [Topics](https://swe.quest/tags.md) · [Sources](https://swe.quest/sources.md) · [Topic map](https://swe.quest/map.md) · [Site guide (llms.txt)](https://swe.quest/llms.txt)
