---
title: "Matryoshka embeddings: faster OpenAI vector search using Adaptive Retrieval"
description: "OpenAI's text-embedding-3 models are trained with Matryoshka Representation Learning, so an embedding can be shortened by truncating trailing dimensions and renormalizing, which reproduces the API's `dimensions` output."
url: "https://swe.quest/articles/matryoshka-embeddings-faster-openai-vector-search-using-adaptive-retrieval-739d1a8595798695"
canonical: "https://supabase.com/blog/matryoshka-embeddings"
source: "Supabase"
published: "2024-02-13"
difficulty: "medium"
kind: "Deep dive"
reading_minutes: 17
tags: ["vector-search","postgres","rag","performance"]
---

# Matryoshka embeddings: faster OpenAI vector search using Adaptive Retrieval

A post on [Supabase](https://swe.quest/sources/supabase.md), published 2024-02-13, 17 min read, Deep dive. Original: [https://supabase.com/blog/matryoshka-embeddings](https://supabase.com/blog/matryoshka-embeddings)

## Summary

OpenAI's text-embedding-3 models are trained with Matryoshka Representation Learning, so an embedding can be shortened by truncating trailing dimensions and renormalizing, which reproduces the API's \`dimensions\` output. Adaptive Retrieval exploits this with a two-pass search: a fast HNSW-indexed ANN pass on a low-dimensional prefix fetches an oversized shortlist, then exact KNN on the full 3072d vectors re-ranks it. Benchmarks on 1M dbpedia embeddings found a 512d first pass (ef\_construction=400, m=32, ef\_search=200, limit = top\_k × 8) reached 99% accuracy against 3072d KNN at about 580 QPS. By comparison, a single pass on 1536d vectors topped out near 89%. The post finishes with a Postgres/pgvector implementation using a sub\_vector SQL function.

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)
- [PostgreSQL](https://swe.quest/tags/postgres.md) (Data & databases)
- [RAG & retrieval](https://swe.quest/tags/rag.md) (AI & ML)
- [Performance engineering](https://swe.quest/tags/performance.md) (Reliability & operations)

## Prerequisites

- vector embeddings ([easy posts on RAG & retrieval](https://swe.quest/tags/rag.md?difficulty=easy))
- pgvector basics
- HNSW indexes
- cosine/dot product similarity

## Keywords

Matryoshka Representation Learning, Adaptive Retrieval, pgvector, text-embedding-3-large, OpenAI, HNSW

## Related posts

- [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.
- [On-disk HNSW index for Postgres with pg\_embedding](https://swe.quest/articles/on-disk-hnsw-index-for-postgres-with-pg-embedding-aadc4bd81ebf65c4.md): Neon · 2023-08-03 · Medium · 5 min read · Vector search, PostgreSQL
  pg\_embedding's new version builds its HNSW vector index on disk rather than in memory and adds cosine, Manhattan and Euclidean (L2) distance support.
- [pgvector: Fewer dimensions are better](https://swe.quest/articles/pgvector-fewer-dimensions-are-better-23087227ac0db157.md): Supabase · 2023-08-03 · Medium · 6 min read · Vector search, PostgreSQL, Performance engineering, RAG & retrieval
  Lower-dimensional embedding models can match or beat OpenAI's 1536-dimension text-embedding-ada-002 on the MTEB leaderboard while making pgvector queries much cheaper, since fewer dimensions mean fewer distance computations and smaller indexes that fit in RAM.
- [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.
- [Storing OpenAI embeddings in Postgres with pgvector](https://swe.quest/articles/storing-openai-embeddings-in-postgres-with-pgvector-19a5d03614f86fc6.md): Supabase · 2023-02-06 · Medium · 11 min read · Vector search, PostgreSQL, RAG & retrieval, LLMs
  pgvector adds a vector data type and distance operators (Euclidean \<-\>, negative inner product \<#\>, cosine \<=\>) to Postgres, so OpenAI text-embedding-ada-002 embeddings (1536 dimensions) can be stored and queried for similarity directly in the database.

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)
