TL;DR
Using random UUIDs as primary keys in SQLite can lead to severe performance degradation due to increased tree re-balancing and paging. Alternatives like UUID7 or using rowid-based integers are recommended. The issue is confirmed through benchmarking, but certain details about long-term impacts remain unclear.
Recent performance tests confirm that using random UUIDs as primary keys in SQLite can cause substantial slowdowns, with insert times increasing by up to 16 times compared to integer keys. This development matters for developers and database administrators relying on UUIDs for unique identifiers, especially in high-volume applications.
Performance benchmarking shows that inserting 10 million rows with UUID4 primary keys in SQLite takes significantly longer than with integer primary keys, mainly due to the unordered nature of UUID4 forcing frequent re-balancing of the B-tree index. Profiling reveals that the database spends more time balancing trees and reading/writing pages during UUID4 insertions. Using UUID7, which is time-ordered, improves performance but remains slower than integer keys. Additionally, enabling UUID4 with a rowid-based table (the default in SQLite) results in slower insert speeds than integer primary keys, because of the overhead of maintaining two indexes and the random nature of UUIDs. These findings are based on controlled benchmark tests, with detailed profiling confirming the performance costs associated with random UUIDs in SQLite.
Why It Matters
This matters because many developers choose UUIDs for their global uniqueness and ease of merging distributed data. However, these performance issues could impact the scalability and efficiency of applications relying heavily on UUIDs, especially in high-throughput environments. Understanding these trade-offs is crucial for optimizing database design and avoiding bottlenecks.

Mastering SQLite with Python: From Basics to Advanced Techniques
As an affiliate, we earn on qualifying purchases.
As an affiliate, we earn on qualifying purchases.
Background
Using UUIDs as primary keys is common in distributed systems and applications requiring unique identifiers across multiple nodes. Prior to this, SQLite’s default rowid-based primary keys offered fast insert performance due to sequential ordering. The shift to UUIDs, particularly UUID4, introduces randomness that disrupts this efficiency. The recent benchmarks highlight the tangible performance costs of this choice, which has implications for database schema design and best practices in SQLite usage.
“The unordered nature of UUID4 leads to frequent re-balancing of the B-tree, causing significant performance degradation.”
— source author
“Switching to UUID7, which is time-ordered, can mitigate some of the performance problems associated with UUID4, but it doesn’t fully match the speed of integer primary keys.”
— database researcher
UUID version 7 generator
As an affiliate, we earn on qualifying purchases.
As an affiliate, we earn on qualifying purchases.
What Remains Unclear
It is not yet clear how these performance issues scale in real-world, long-term applications or with different hardware configurations. The impact on other database systems that use clustered indexes and how they compare to SQLite remains to be explored. Additionally, the long-term effects of frequent re-balancing on database stability are still uncertain.
high-performance database index cards
As an affiliate, we earn on qualifying purchases.
As an affiliate, we earn on qualifying purchases.
What’s Next
Further testing is expected to evaluate the performance of alternative UUID versions like UUID7 across various workloads. Developers may consider adopting ordered UUIDs or integer primary keys to optimize performance. Future updates could include best practices for schema design based on these findings and potential improvements in SQLite’s handling of UUIDs.

DELPHI SQLITE DATABASE GUIDE: Master SQL Integration, Efficient Query Writing, and Application Optimization in Delphi
As an affiliate, we earn on qualifying purchases.
As an affiliate, we earn on qualifying purchases.
Key Questions
Why do UUID4 primary keys slow down SQLite performance?
Because UUID4 is randomly generated, it causes unordered insertions that force frequent re-balancing of the B-tree index, increasing read/write overhead and slowing down insert speeds.
Can switching to UUID7 improve performance?
Yes, UUID7 is time-ordered, which reduces the randomness and the need for re-balancing, resulting in better performance than UUID4, though it still lags behind integer primary keys.
Are there alternatives to UUIDs for unique identifiers in SQLite?
Yes, using integer primary keys, especially auto-incremented rowids, provides faster insert performance. Alternatively, ordered UUIDs like UUID7 can offer a compromise between uniqueness and speed.
Does this issue affect other databases?
The problem of random UUIDs causing index re-balancing can affect other databases with clustered indexes, but the severity varies depending on the system’s architecture and indexing strategies.
Source: Hacker News