Stop Using UUIDs in Your Database

By Daniel CraciunLast updated
Stop Using UUIDs in Your Database

The most common method for uniquely identifying rows in a database is by using UUID fields. However, this approach has performance drawbacks that you should be aware of.

In this article, we will explore two performance issues that may occur when using UUIDs as keys in your database tables.

So, without further ado, let's dive right in!

What are UUIDs?

UUID stands for Universally Unique Identifier.

There are several versions of UUID, but in this article, we will focus on the most popular one: UUIDv4.

Here is an example of what a UUIDv4 looks like:

e9f4dc77-1cfc-46db-a240-228a020b4c14
NOTE: Each UUID has the digit 4 in the same position to denote the version.

Problem 1 — Insert Performance

When a new record is added to a table, the index linked to the primary key must be updated to ensure optimal query performance.

Indexes are built using the B+ Tree data structure.

To learn more about how indexes and B+ Trees function, I highly recommend watching this excellent video by Abdul Bari.

TL;DR: for every record insertion, the underlying B+ Tree must be rebalanced to optimize query performance.

The rebalancing process becomes highly inefficient for UUIDs due to their inherent randomness, which makes it harder to maintain a balanced tree. As your system scales and you accumulate millions of nodes to rebalance, the insert performance significantly decreases when using UUID keys.

NOTE: Other options such as UUIDv7 could be a better option, since they have inherent ordering which makes it easier to index them.

Problem 2 — Higher Storage

Let's examine the size of a UUID compared to an auto-incrementing integer key:

Auto-incrementing integers consume 32 bits per value, whereas UUIDs consume 128 bits per value, which is 4 times more per row. Furthermore, when stored in human-readable form, a UUID can use up to 688 bits per value.

This is approximately 20x more per row.

Let's evaluate the impact of UUIDs on storage by simulating a realistic database scenario.

We'll use the tables from Josh Tried Coding's example:

  • Table 1 will contain 1 million rows with UUIDs.
  • Table 2 will contain 1 million rows with auto-incrementing integers.

Here are the results; let's break down each statistic one by one:

Image

Total table size: The UUID table is about 2.3 times larger than the Integer table when considering the overall table sizes.

ID field size: A single UUID field requires 9.3 times more storage space than an equivalent integer field.

ID column size: Excluding other attributes in each table, the UUID column is 3.5 times larger than the Integer column.

Conclusion

UUIDs are an excellent method for ensuring uniqueness between records in a table.

While these issues become significant at scale, most users will not experience noticeable performance degradation from using UUIDs.

However, it is important to understand the implications of using UUIDs in your tables and ensure optimal database design to mitigate potential problems.

Keep reading

More posts tagged “Community”.
What are Connection Pools and How Do They Help with PostgreSQL?

When you're building an application that interacts with a database, you’ll often need to establish a connection to the database each time your application wants to retrieve or store data. This can be slow and resource-intensive, especially if your application needs to interact with the database frequently. This is where connection pooling comes in.

Tue, Sep 17, 2024
Count(*) vs Count(1) in SQL

If you've written SQL queries, you've likely encountered both COUNT(*) and COUNT(1) to count rows in a table. But what's the difference between these two, and which one is better to use? Let's explore this in detail.

Sun, Aug 4, 2024
Simply Rely on Postgres for All Your Needs

Replace all your infrastructure with one simple database

Tue, Jul 9, 2024
Redis Alternatives 2025

Redis is usually chosen as a cache because of its speed and versatility, allowing developers to provide users with a faster and smoother experience. But after its license change, many companies are looking for an alternative. In this post, we will take a look at the most popular options for replacing Redis as a cache.

Fri, Jan 10, 2025
Heroku vs Granite: PostgreSQL pricing and limits in 2026

This article breaks down how Heroku's PostgreSQL pricing and limits compare to Granite in 2026, with enough detail for you to make a real decision in the next hour.

Tue, Mar 31, 2026
Fly vs Railway vs Render: deploy costs compared 2026

For solo technical founders launching an MVP on a tight budget, Railway offers the lowest entry cost and fastest setup, while Granite provides a compelling all-in-one alternative with integrated storage and CDN for a predictable $9/month—making either a better starting point than Fly.io or Render for most early-stage projects.

Fri, Mar 27, 2026
Be first in line for updates
and special pricing
Get early access to new features and exclusive discounts delivered straight to your inbox