UUIDs in Databases
UUIDs are widely used as database primary keys and identifiers in modern applications. They are particularly useful when records need to be created across multiple servers, services, or databases without relying on a central sequence. A UUID can be generated independently before a record is inserted, making it useful for distributed systems, APIs, microservices, replication, and data synchronization.
Using UUIDs as database keys also comes with tradeoffs. UUIDs are larger than integers and can require more storage in tables and indexes. Random UUID v4 values can also reduce index locality because new values are distributed throughout the index rather than inserted in a predictable order. For large databases, this can affect index size, page splits, fragmentation, and write performance. UUID v7 addresses some of these concerns by incorporating a timestamp while retaining a large random component, producing identifiers that are roughly time-ordered.
Choosing between a UUID and an integer or bigint primary key depends on the application architecture and database workload. Integer keys are compact, efficient, and naturally ordered, while UUIDs can be generated independently and remain unique across different systems. UUIDs can therefore be especially useful for distributed applications, data imports, replication, and systems where identifiers need to be created before reaching the database. There is no universally correct choice; the database's indexing behavior, expected scale, and application requirements all matter.
The right UUID database strategy also depends on the database engine, storage format, and indexing strategy. PostgreSQL, MySQL, SQL Server, Oracle, SQLite, and NoSQL databases have different UUID data types, functions, storage options, and indexing behavior. Storing UUIDs in an appropriate native or binary representation can also reduce storage requirements compared with storing them as 36-character text values.
The articles below cover the practical aspects of using UUIDs in databases, including UUID primary keys, UUID v4 and UUID v7, database performance, index bloat, storage requirements, indexing, and database-specific implementation details.
Related Articles
14 practical guides covering Postgres, MySQL, MongoDB, storage costs, and indexing strategies.
Latest
-
2026-08-11 — Why Random UUIDs Fragment Your Postgres Index (and How to Fix It)
Why random v4 causes 50% fragmentation and page splits, and how time-ordered v7 restores 90% fill factor.
Choosing Between UUIDs and Other Key Strategies
-
UUIDv4 vs UUIDv7 for Database Primary Keys: A Practical Comparison
Insert benchmarks, index locality, and when to choose random vs time-ordered IDs.
-
UUID as Primary Key: The Complete Storage & Indexing Tradeoffs Guide
Storage cost at scale, index bloat, and native type vs text vs binary(16).
-
Composite Keys vs UUIDs: When Natural Keys Beat Surrogate UUIDs
Surrogate vs natural keys, join performance, and design tradeoffs.
-
UUID Index Bloat Explained: B-Trees, Page Splits, and What to Do About It
How B-trees fragment, fill factor, page splits, and VACUUM strategies.
-
How Much Storage Do UUID Primary Keys Actually Cost at Scale? (Real Numbers)
Real numbers: 21MB BIGINT vs 37MB v4 vs 30MB v7 at 1M rows.
UUIDs in PostgreSQL
-
UUID vs BIGSERIAL as Primary Key in PostgreSQL: Which Should You Use?
Postgres native uuid type, performance, and replication benefits.
-
How to Store UUIDs Correctly in PostgreSQL (uuid type vs text vs varchar(36))
uuid type vs text vs varchar(36) — storage and index size.
-
Enabling gen_random_uuid() in Postgres: pgcrypto vs uuid-ossp vs Native (PG13+)
Enable UUID generation: pgcrypto, uuid-ossp, and native PG13+ method.
-
UUID v7 in PostgreSQL: Native Support, Extensions, and Benchmarks
Native PG support, pg_uuidv7 extension, and insert benchmarks.
UUIDs in MySQL
-
Storing UUIDs Efficiently in MySQL: BINARY(16) vs CHAR(36)
BINARY(16) saves 50% space vs CHAR(36) — conversion guide.
-
UUID() vs AUTO_INCREMENT in MySQL: Performance Benchmarks
InnoDB clustered index, insert performance, and fragmentation.
-
MySQL UUID Functions Explained (UUID_TO_BIN, BIN_TO_UUID, is_swapped)
UUID_TO_BIN, BIN_TO_UUID, and is_swapped for time-ordered storage.
UUIDs in NoSQL Databases
-
UUID Primary Keys in MongoDB: Do You Even Need Them?
ObjectId vs UUID in MongoDB — when UUID makes sense.