---
title: "Approximate nearest neighbor search with DiskANN in libSQL"
description: "libSQL implements approximate nearest neighbor search using LM-DiskANN, a low-memory variant of DiskANN."
url: "https://swe.quest/articles/approximate-nearest-neighbor-search-with-diskann-in-libsql-f704916b9c7db474"
canonical: "https://turso.tech/blog/approximate-nearest-neighbor-search-with-diskann-in-libsql"
source: "Turso"
published: "2024-07-26"
difficulty: "medium"
kind: "Deep dive"
reading_minutes: 7
tags: ["vector-search","sqlite","storage-engines","algorithms"]
---

# Approximate nearest neighbor search with DiskANN in libSQL

A post on [Turso](https://swe.quest/sources/turso.md), published 2024-07-26, 7 min read, Deep dive. Original: [https://turso.tech/blog/approximate-nearest-neighbor-search-with-diskann-in-libsql](https://turso.tech/blog/approximate-nearest-neighbor-search-with-diskann-in-libsql)

## Summary

libSQL implements approximate nearest neighbor search using LM-DiskANN, a low-memory variant of DiskANN. The index is wired into SQLite's indexing system through a new OpenVectorIdx bytecode instruction, so inserts update the vector index automatically. Each node is stored on disk as a fixed-size block holding its full vector plus its neighbors' IDs and compressed vectors. That layout lets best-first graph search make local decisions without extra I/O, trading extra storage for lower memory use. Queries go through a vector\_top\_k virtual table, and early 1-bit binary quantization with Hamming distance is in place to cut index size further.

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)
- [SQLite](https://swe.quest/tags/sqlite.md) (Data & databases)
- [Storage engines](https://swe.quest/tags/storage-engines.md) (Data & databases)
- [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))
- SQL indexes ([easy posts on SQL](https://swe.quest/tags/sql.md?difficulty=easy))
- SQLite basics ([easy posts on SQLite](https://swe.quest/tags/sqlite.md?difficulty=easy))

## Keywords

DiskANN, LM-DiskANN, libSQL, SQLite, vector\_top\_k, Turso, binary quantization

## Related posts

- [The space complexity of vector indexes in LibSQL](https://swe.quest/articles/the-space-complexity-of-vector-indexes-in-libsql-c0acaa3c84f4d05b.md): Turso · 2024-10-03 · Medium · 5 min read · Vector search, SQLite, Mobile, RAG & retrieval
  LibSQL's built-in vector index uses LM-DiskANN, a disk-oriented graph ANN algorithm that stores neighbor graphs in shadow tables.
- [Turso brings Native Vector Search to SQLite](https://swe.quest/articles/turso-brings-native-vector-search-to-sqlite-524b5f374ac6abee.md): Turso · 2024-06-05 · Medium · 5 min read · Vector search, SQLite, Multi-tenancy
  libSQL, the SQLite fork behind Turso, now has native vector similarity search exposed as a column type (F32\_BLOB) instead of an extension like sqlite-vss.
- [SQLite Retrieval Augmented Generation and Vector Search](https://swe.quest/articles/sqlite-retrieval-augmented-generation-and-vector-search-f6665825b9a1cb47.md): Turso · 2024-06-21 · Medium · 5 min read · Vector search, SQLite, RAG & retrieval
  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.
- [Indexing sparse vectors with Turso](https://swe.quest/articles/indexing-sparse-vectors-with-turso-3c9fdb0f1a894948.md): Turso · 2025-11-05 · Hard · 8 min read · Vector search, SQLite, Query engines, Algorithms, Performance engineering
  Turso 0.3.0 adds a compact sparse vector representation (vector32\_sparse) that uses 5–50x less storage than dense vectors, plus an inverted index for nearest-neighbor search by weighted Jaccard distance.
- [You Don't Need A Separate Vector Database](https://swe.quest/articles/you-don-t-need-a-separate-vector-database-3c832a16be076ef3.md): Turso · 2024-11-04 · Easy · 4 min read · Vector search, SQLite, Databases, RAG & retrieval
  Vector search belongs in your primary database rather than a separate system like Pinecone, because syncing embeddings across multiple stores adds cost and operational complexity.

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)
