← Retour au blog
tech 6 June 2026

The Perils of UUID Primary Keys in SQLite

Using random UUIDs as primary keys in SQLite might seem like a good idea, but it can lead to significant performance issues.

Article inspired by the original source
The perils of UUID primary keys in SQLite ↗ andersmurphy.com

Introduction

In the realm of databases, selecting the right primary key is crucial for performance and scalability. While using random UUIDs (specifically UUID4) as a primary key is common, it is not without its drawbacks, particularly in databases like SQLite that use clustered indexes. This article explores the challenges of using UUIDs as a primary key in SQLite and suggests alternatives.

Understanding Clustered Indexes

A clustered index determines the physical storage order of rows in a table. In SQLite, each ordinary table has an implicit integer primary key called rowid. This rowid is used as the clustered index, meaning that the data is physically sorted according to this key. However, when you use a WITHOUT ROWID table with a UUID as the primary key, the clustered index becomes random, which can have severe performance implications.

The Problem with Random UUIDs

Using a random UUID4 means that new rows are inserted randomly into the B-tree. This leads to significant fragmentation, requiring constant rebalancing of the tree. Test results with 10 million inserts show performance up to 16 times slower with UUID4 compared to a sequential integer.

Case Study: Insertion Performance

Consider a concrete example: inserting 10 million rows into a table with rowid took about 8,382 ms, roughly a million inserts per second. In contrast, with a UUID4 as the primary key, the same number of inserts took about 125,860 ms. Why such a disparity? The unordered nature of UUID4 means each new insertion potentially requires rebalancing the tree, thus increasing the time cost.

Alternatives to UUIDs

To avoid these performance issues, several alternatives are available:

  1. Sequential Keys: Using auto-increment integers can significantly improve insertion performance. This reduces the need for rebalancing since new rows are always added at the end of the index.
  1. Ordered UUIDs: Some versions of UUIDs, like UUID1, contain a timestamp, making them more ordered and less likely to cause fragmentation.
  1. Hybrid Systems: Combining the use of UUIDs with a sequential prefix can allow you to leverage both worlds: the global uniqueness of UUIDs and the efficiency of ordered insertions.

Conclusion

Using UUIDs as a primary key in SQLite can lead to significant performance issues, especially in the case of massive insertions. While UUIDs are useful for ensuring uniqueness, databases that use clustered indexes, like SQLite, require tailored strategies to maintain optimal performance.

Let's discuss your project in 15 minutes.

SQLite UUID Clustered Index Database Performance Primary Key
Deepthix newsletter · 100% AI · every Monday 8am

An AI agent reads tech for you.

Our AI agent scans ~200 sources per week and ships the best articles to your inbox Monday 8am. Free. One click to unsubscribe.

Visit the newsletter page →

Want to automate your operations?

Let's talk about your project in 15 minutes.

Book a call