December 5, 2026

UUID v4 vs v7: Random vs Time-Ordered IDs, and an 8x Difference in Database Inserts

Metal key tags on a pegboard, one row ordered and one row jumbled

Loading 300,000 rows into a SQLite table keyed by random version 4 UUIDs took 5.7 seconds. Loading the same number of rows keyed by time-ordered version 7 UUIDs took 0.69 seconds. Both kinds are 128-bit identifiers, both look like the same string of hex digits and dashes, and both are effectively impossible to collide. The difference is where the randomness sits. RFC 9562, the 2024 standard that replaced the original UUID specification, made version 7 official, and testing both versions in Python and SQLite shows what changed and when it matters.

Video: Database Star on YouTube

What changed in 2024

RFC 9562

RFC 9562 was published in May 2024 on the IETF Standards Track and states that it “obsoletes RFC 4122,” the 2005 document that defined UUIDs for nearly two decades. RFC 9562 It keeps the older versions and adds new ones, including version 7, with a clear recommendation: “Implementations SHOULD utilize UUIDv7 instead of UUIDv1 and UUIDv6 if possible.”

Language support

Python’s standard library added uuid.uuid7() in Python 3.14. Its documentation describes it as generating “a time-based UUID according to RFC 9562, §5.7,” and notes that it embeds “a 48-bit timestamp and use a 42-bit counter to guarantee monotonicity within a millisecond.” Python documentation, uuid PostgreSQL’s current documentation lists a uuidv7() function that “Generates a version 7 (time-ordered) UUID,” alongside uuidv4() and gen_random_uuid(). PostgreSQL documentation, UUID Functions

The tests here ran on Python 3.13, which has no uuid7(), so a small generator was written following the RFC’s layout.

Cozy living room with large bookshelf and comfortable seating

How the two are built

Version 4: almost all random

RFC 9562 says “UUIDv4 is meant for generating UUIDs from truly random or pseudorandom numbers,” with 122 random bits. The remaining six bits mark the version and variant. Python’s uuid.uuid4() generates one “in a cryptographically-secure method.” In the test, a v4 UUID reported version 4 and the RFC variant; its 13th hex digit was always 4.

Version 7: time first, then random

The RFC defines version 7 as a “48-bit big-endian unsigned number of the Unix Epoch timestamp in milliseconds” in the first 48 bits, then the 4-bit version set to 7, the 2-bit variant, and the rest filled “with random bits for each new UUIDv7 generated to provide uniqueness.”

The test generator wrote the current millisecond into the first six bytes, filled the remaining ten with random bytes, then set the version and variant bits. Python’s uuid module read the result back as version 7 with the RFC variant, and the first six bytes decoded to the exact millisecond the ID was created, one millisecond before the clock was read again.

What the timestamp reveals

That embedded time is useful for sorting and a privacy consideration at once. Anyone holding a v7 UUID can recover when it was created, to the millisecond. A v4 UUID reveals nothing. For IDs that appear in public URLs, such as account or document links, that difference may matter.

Sorting and monotonicity

Across milliseconds: sorted

Twenty v7 UUIDs were generated with a 1.5 ms pause between each. Sorting them as strings and as raw bytes both reproduced the generation order exactly. Since the timestamp leads, a later millisecond always sorts after an earlier one.

Within one millisecond: not with pure random bits

A thousand v7 UUIDs generated with the same fixed millisecond did not sort into generation order. With only random bits after the timestamp, IDs made in the same millisecond sort in random order among themselves.

RFC 9562’s section on monotonicity calls it “the backbone of time-based sortable UUIDs” and describes three methods for keeping order within a single tick: a dedicated counter after the timestamp, a randomly seeded counter that increments, or extra sub-millisecond clock precision. Python 3.14’s built-in uses the counter approach, according to its documentation, and PostgreSQL’s describes adding a sub-millisecond timestamp. A hand-rolled generator like the test’s simple one does not guarantee order within a millisecond.

A bookshelf filled with many old books

Why databases care: the insert test

The setup

Two SQLite 3.53.1 database files each received 300,000 rows into a table whose primary key was a 16-byte UUID, declared WITHOUT ROWID so the table itself is the B-tree ordered by that key. Rows were inserted in batches of 1,000 with a commit after each batch. One file used uuid.uuid4(). The other used v7 UUIDs with the timestamp advancing one millisecond every 50 IDs, imitating a steady stream of inserts.

The results

Key type Insert time Pages File size
UUID v4 5.70 s 1,793 7.3 MB
UUID v7 0.69 s 1,856 7.6 MB

The v7 load was about 8 times faster. The file sizes ended up close.

Why

Each new v7 key is larger than the previous ones, so it lands at the end of the B-tree, on the same few pages that were just written and are still in memory. Each new v4 key lands at a random position, so nearly every insert touches a different page somewhere in the tree. RFC 9562 describes this directly: non-time-ordered versions such as v4 “have poor database-index locality,” and “Time-ordered monotonic UUIDs benefit from greater database-index locality,” with a performance difference that “can be one order of magnitude or more.” The test landed just under that, at a factor of 8, on a small table that fit in memory; larger tables that exceed the cache would be expected to show a wider gap.

Text keys cost twice the space

The same test stored the UUIDs a second way, as their 36-character text form. The v4 text table took 8.06 seconds and 14.1 MB; the v7 text table took 0.84 seconds and 14.5 MB. Storing text roughly doubled the file size for both, compared with 7.3 and 7.6 MB as 16-byte blobs, and made the v4 load about 40% slower still. Ordering held up in text form too, since the hex string sorts in the same order as the bytes. A repeat of the v4 blob run took 5.71 seconds, within a hundredth of a second of the first.

A bonus: time-range queries on the key

Because the first six bytes are a timestamp, the primary key doubles as a rough time index. In an in-memory table of 300,000 v7 IDs spread over 6,000 milliseconds, a query for every ID created during one particular second, written as WHERE id >= ? AND id < ? with the bounds built from two timestamps followed by zero bytes, returned exactly 50,000 rows in 1.67 ms. SQLite’s query plan showed SEARCH t USING PRIMARY KEY, a direct range scan rather than a full table read. With v4 keys, the same question would need a separate timestamp column and its own index.

Collisions: how unlikely

Version 4

With 122 random bits, the birthday-problem approximation gives the chance of any duplicate among n random v4 UUIDs. Calculated in Python:

UUIDs generated Chance of a collision
1 million about 9 × 10⁻²⁶
1 billion about 9 × 10⁻²⁰
1 trillion about 9 × 10⁻¹⁴
2.71 × 10¹⁸ about 50%

The 50% point is around 2.7 quintillion IDs, far beyond any single application.

Version 7

A v7 UUID trades random bits for the timestamp. In the test layout, 74 random bits remained, so collisions can only occur between IDs created in the same millisecond. A million IDs within one millisecond gave a collision chance of about 2.6 × 10⁻¹¹; a thousand gave about 2.6 × 10⁻¹⁷. Implementations that use a counter, like Python 3.14’s, make same-millisecond duplicates from a single generator impossible as long as the counter does not overflow.

The real risks

Bad randomness, not arithmetic, causes real UUID collisions: virtual machines cloned with the same random-number state, or code that uses a non-cryptographic generator. Using the language’s standard function avoids both.

A round wall clock with a cracked white face and black Roman numerals

Choosing between them

Use v7 when

  • The UUID is a database primary key or indexed column receiving frequent inserts.
  • Sorting by ID should roughly follow creation time.
  • Logs, events or messages benefit from IDs that sort chronologically.

Use v4 when

  • The ID is exposed publicly and creation time should not leak.
  • IDs are generated in bulk ahead of time or across systems with unreliable clocks.
  • Unpredictability matters more than index performance.

Practical notes

Store UUIDs as 16-byte binary or a native UUID type rather than 36-character text where the database allows; the test used 16-byte blobs. Clock jumps backwards on a server can break v7 ordering, which is one reason library implementations add counters and guard logic. And switching an existing table from v4 to v7 keys speeds up new inserts without reordering the rows already there. Because the version digit sits in the same place in every UUID, the two kinds can share one column during a migration, and code that needs to tell them apart can read the 13th hex character: 4 for random, 7 for time-ordered.

Sources

  • RFC 9562, Universally Unique IDentifiers (UUIDs), May 2024: https://www.rfc-editor.org/rfc/rfc9562.html
  • Python documentation, uuid: UUID objects according to RFC 9562: https://docs.python.org/3/library/uuid.html
  • PostgreSQL documentation, UUID Functions: https://www.postgresql.org/docs/current/functions-uuid.html

920 Posts

Peter Kalafatis writes and edits X9M8. He holds a bachelor's degree from LIU Post and a master's in Media Studies from The New School, with academic work in the philosophy of science, human behavior, AI, and memetics.

View All Posts

Leave a Reply

Your email address will not be published. Required fields are marked *