How to boost your database performance with OpenAIs new v3 embeddings

Sascha Gstir
Sascha Gstir · Co-Founder, Pondhouse Data
Published Updated 10 min read
How to boost your database performance with OpenAIs new v3 embeddings
On this page

OpenAI's v3 embedding models, text-embedding-3-small and text-embedding-3-large, can be shortened to fewer dimensions while keeping most of their accuracy. They were trained with Matryoshka Representation Learning (MRL), which makes this possible. Shorter embeddings make vector search faster, and a technique called adaptive retrieval combines short and full-length embeddings to get speed and accuracy at the same time. In a case study by Supabase with 1 million embeddings in PostgreSQL and pgvector, adaptive retrieval reached 580 queries per second (QPS) at 99% accuracy.

OpenAI released the v3 models in late January 2024, in two sizes. Both substantially outperform the previous flagship model, text-embedding-ada-002, in accuracy, especially on the multilingual MIRACL benchmark. As we explained in our "Introduction to Retrieval Augmented Generators", semantic search for relevant content is a central part of a RAG system. Better semantic representations therefore improve RAG systems directly, particularly those working with non-English content.

Update (November 2026): pgvector 0.7.0 (April 2024, after this post) added the halfvec type, which stores half-precision vectors and can be indexed up to 4,000 dimensions, and a binary_quantize function for binary-quantized indexes that you re-rank with the original vectors. Both are further ways to shrink the first pass described below; see the pgvector README and changelog.

Benchmark scores of OpenAIs v3 embeddings

The smaller model is also much cheaper than its predecessor: it costs a fifth of the previous model's price ($0.00002 / 1k tokens). The larger model costs 30% more ($0.00013 / 1k tokens).

The new embeddings have 1536 (small) and 3072 (large) dimensions, and they come with native support for shortening. Unlike other embeddings, they can be reduced in length without losing their semantic integrity, so you can trade size against accuracy. OpenAI published benchmark values for the shortened embeddings, too. Both models score higher on MTEB in their shortened form than their predecessor, and the large model does so even when shortened to 256 dimensions.

Benchmark scores of OpenAIs v3 embeddings at different dimensions

So where does this ability to simply shorten the vectors come from?

Matryoshka embeddings: how shortening works

Matryoshka dolls (generated by DALL-E)

The v3 embedding models support shortening because they were trained with a technique called "Matryoshka Representation Learning", introduced in the paper of the same name. MRL trains a single high-dimensional vector to encode information at different levels of granularity. The name comes from Russian Matryoshka dolls, where smaller dolls nest inside larger ones: the first dimensions of the vector already form a usable, smaller embedding.

Schematic representation of how MRL works (source: paper "Matryoshka Representation Learning")

If you want the details of the training process, the paper is available here.

Practical note: To shorten embeddings, the OpenAI API has a dimensions parameter that sets the number of dimensions to keep. You can also shorten a vector yourself by truncating it, but then you must normalize it afterwards. The API's dimensions parameter does that step for you. Shortening manually has one advantage: you can keep an embedding in several lengths without calling the API again and paying for it.

How embedding size affects vector database performance

The embedding length you choose has a big effect on how a Retrieval Augmented Generation (RAG) system runs, especially on its vector database. Storage grows linearly with embedding size. On top of that, larger embeddings need larger and more complex indexes and much more compute for similarity calculations. That can slow down the vector database and, with it, the entire system. Choosing the embedding dimensions is therefore about more than storage.

Does this mean we have to choose between accuracy and performance?

Adaptive retrieval: fast and accurate retrieval

Between the headlines about accuracy and selectable embedding lengths, the most useful feature is easy to overlook. Shortening embeddings doesn't force you to choose between retrieval accuracy and database performance. Used right, it gives you both. The technique for this is adaptive retrieval.

Note: Adaptive retrieval should not be confused with "adaptive RAG", where information is only retrieved when necessary (see this paper). The authors of the original Matryoshka Representation Learning paper introduced and recommended adaptive retrieval, which is why we use their term in this article.

How adaptive retrieval works

First, consider the standard retrieval process. To find information that answers a user query, the query is embedded with the same model used for the content in the vector database. The database then runs a similarity search, typically with cosine similarity or, for normalized vectors, the equivalent but faster dot product. In an exact search, this comparison runs against every entry in the database, so the computational effort grows with both the number of dimensions and the number of entries. Large embeddings, such as the 3072 dimensions of text-embedding-3-large, can therefore slow down a vector database with a big knowledge base considerably.

Adaptive retrieval uses shortened embeddings in a two-step process that reduces one of these two factors in each step. In the first step, the search still covers every database entry, but with shortened embeddings. Fewer dimensions make each similarity calculation cheaper. Accuracy drops slightly, but this step efficiently pre-selects a shortlist of potentially relevant entries.

In the second step, the search runs only on this shortlist, with more dimensions, which brings the accuracy back up. Because the shortlist is so much smaller than the database, this step is fast too.

Adaptive retrieval: shortlist with short embeddings, then re-rank with full-size ones

Even with the extra step, the reduced complexity in both phases makes the whole retrieval process considerably faster. (The pattern of a cheap first pass followed by a more precise re-ranking step is also the idea behind rerankers such as the one in our ColBERT reranker post.)

Case study: Supabase and adaptive retrieval

To show what adaptive retrieval achieves in practice, we refer to a case study by Supabase. Full credit for the results in this section goes to the Supabase team and their blog post on Matryoshka embeddings.

For their tests, they embedded the dbpedia dataset (available on Hugging Face), 1 million entries, with OpenAI's text-embedding-3-large model. The database was PostgreSQL with the pgvector extension. (If you want to generate embeddings inside PostgreSQL itself, see our post on using AI directly from your database with pgai.)

All accuracy values below use the same definition: an exact KNN search on the full 3072-dimension vectors is the reference, and accuracy is the share of IDs returned by a search that match the IDs found by that reference KNN search.

Accuracy implications of ANN

In practice, vector databases use indexes for approximate nearest neighbor (ANN) search, which trade a small loss in accuracy for speed. The first test checked whether that loss matters for the rest of the evaluation.

It compared a KNN search and an ANN search with an HNSW index, both on 1536 dimensions. The KNN search reached an accuracy of 89.5%: 89.5% of the top 10 results matched those of the KNN search on the full 3072 dimensions. The ANN search reached 89.2% (at 670 QPS). The approximation costs so little accuracy that the following tests could use it.

One pass with reduced dimensionality

The next test measured how much accuracy a single pass loses at reduced dimensionality. With 1536 dimensions, accuracy drops to about 89%, so 9 out of 10 results still match those of the exact full-length KNN search. At 256 dimensions, accuracy falls to 59%, or about 6 out of 10 matching results.

One pass performance with shortened embeddings (source: Supabase blog)

Optimal first-pass dimensionality

As the previous test showed, first-pass accuracy depends heavily on the number of dimensions. To keep the overall accuracy constant, the first pass has to pre-select more candidates when it uses fewer dimensions. The second pass then picks the most relevant results from this larger shortlist at full resolution. The goal is to find the sweet spot in how the work is split between the two passes. Supabase tested first-pass embedding lengths from 256 to 768 dimensions, with an exact KNN search on the full 3072 dimensions as the second pass.

First pass optimization (source: Supabase blog)

At a constant overall accuracy of 99%, 512 dimensions in the first pass gave the best throughput (QPS). The first pass had to return the top 80 entries to reach that accuracy for the final top 10.

Final performance

Based on this, Supabase ran further tests with 512 dimensions in the first pass. With the chosen settings, the setup reached 580 QPS at 99% accuracy. Giving up 5 percentage points of accuracy raised throughput to 700 QPS.

Performance of final setup (source: Supabase blog)

Key insights from the Supabase case study

Adaptive retrieval vs. simple retrieval

The Supabase article doesn't report the throughput of an ANN search at 3072 dimensions, which would be the right baseline for measuring the improvement. We do know from the first test that a single ANN pass with 1536 dimensions reached 670 QPS at 89% accuracy. Adaptive retrieval achieved a higher accuracy of 94% at about 5% higher throughput. That already shows an improvement in both performance and accuracy. A comparison against full-length embeddings, at the highest accuracy, would be more interesting still. The single-stage search would then have to use twice as many dimensions, which would likely reduce its throughput considerably.

Dimension granularities

The embedding models are trained on specific granularities (embedding lengths), so you shouldn't pick an arbitrary number of dimensions. Lengths that were not part of the training may lose more accuracy. The Supabase team speculates:

As of the time of writing, we don't yet know. But we do know that they were likely trained on at least the following granularities (based on their MTEB comparisons):

  • text-embedding-3-small: 512 and 1536
  • text-embedding-3-large: 256, 1024, and 3072

Functional indexes in pgvector

To implement adaptive retrieval, the Supabase team used a functional index, a PostgreSQL feature that works with pgvector. A functional index is defined on the result of a function applied to one or more columns of a table. That lets you pick the number of dimensions to index (for the first pass) with a function over the stored embedding. You only need to store each embedding once, at the maximum length you want.

Funnel retrieval

The two-step process could in theory be extended to more stages. The original MRL paper calls this "Funnel Retrieval":

"Funnel thins out the initial shortlist by a repeated re-ranking and shortlisting with a series of increasing capacity representations. Funnel halves the shortlist size and doubles the representation size at every step of re-ranking."

Since the first step is by far the most expensive, Supabase gives this assessment:

  • The first pass is the most expensive, even with an index (as seen using explain analyze). It has to filter the entire table down to a much smaller number of records (relatively). This is why creating an index on the first pass is crucial.
  • The second pass is very quick. Even at 3072d using KNN, the time taken to re-rank the initial shortlist is a small fraction of the time taken to complete the first pass.

So the performance gained by splitting the second pass into multiple passes is likely minimal. Finding ways to optimize the first pass will likely result in more gains."

Conclusion

OpenAI's v3 embedding models, released in January 2024, come in two variants that both outperform text-embedding-ada-002 in accuracy, especially on multilingual benchmarks. For RAG systems, that means better retrieval of non-English content. How you split documents into chunks before embedding them matters as well; late chunking is one way to keep context in each chunk.

Because the models were trained with Matryoshka Representation Learning, their embeddings can be shortened. You can choose the balance between embedding size and accuracy, which matters for vector database performance.

Adaptive retrieval uses this to get both speed and accuracy: a fast first pass with short embeddings, then a re-rank of a small shortlist with full-size embeddings. In Supabase's case study with 1 million embeddings in pgvector, a 512-dimension first pass with a top-80 shortlist reached 580 QPS at 99% accuracy, and 700 QPS at 94%.

If you want to try this yourself, the case study gives three practical pointers: use dimension counts the models were likely trained on, use a functional index so you store each embedding only once, and spend your optimization effort on the first pass, since additional re-ranking stages add little.

Further reading

Interested in building high-quality AI agent systems?

We prepared a comprehensive guide based on cutting-edge research for how to build robust, reliable AI agent systems that actually work in production. This guide covers:

  • Understanding the 14 systematic failure modes in multi-agent systems
  • Evidence-based best practices for agent design
  • Structured communication protocols and verification mechanisms
Sascha Gstir

About the author

Sascha Gstir

Co-Founder, Pondhouse Data

Sascha worked for almost a decade in data analytics and data science. At Pondhouse Data he builds and tunes AI solutions, from model selection to production.