Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

iTechGuides is reader-supported. When you buy through links on our site, we may earn an affiliate commission. As an Amazon Associate I earn from qualifying purchases. Learn more

For a new database key, UUIDv7 is often the most straightforward choice when time ordering can improve index locality and revealing an approximate creation time is acceptable. UUIDv4 remains reasonable when random-looking identifiers matter more or measured performance is sufficient. ULID is another time-sortable option, especially if its text form fits your stack, but its comparison and generator behavior need to match your database and application. None guarantees a particular performance gain: test the target workload before changing existing keys.

How UUIDv4, UUIDv7, and ULID differ

All three formats identify records with 128-bit values, but they arrange those bits differently. That affects how identifiers sort, how they look when rendered as text, and how useful their order can be to an index.

Format What it encodes and how it sorts Representation and support Ordering and privacy considerations
UUIDv4 Random or pseudorandom UUID values have no time order. As keys arrive, their sort positions are scattered across the index. A standardized UUID version. Where feasible, storing a UUID as its underlying 128-bit value avoids the extra space required by text. Does not encode a creation timestamp. Random-looking IDs may suit applications that want less predictable identifiers, but an ID is not an authorization secret.
UUIDv7 The most significant 48 bits contain a Unix timestamp in milliseconds, so bytewise sorting places nearby generation times near each other. The remaining 74 bits, excluding version and variant bits, can use random data or optional sub-millisecond precision and monotonicity mechanisms. A UUID defined by RFC 9562; it remains 128 bits and can use a UUID-typed database column. PostgreSQL 18 documents native generation. Reveals approximate creation time and order. Ordering among IDs generated within one millisecond depends on the generator’s implementation and options.
ULID Uses a 48-bit Unix-millisecond timestamp and 80 bits of randomness. Canonical strings sort by time when compared using the specified character ordering; ordering within a millisecond depends on the generator. A separate identifier convention, not a UUID version. Its canonical form is 26 Crockford Base32 characters. Whether it takes less database space depends on whether it is stored as text or a binary value and on the database representation. Reveals approximate creation time. Same-millisecond monotonic ordering is not guaranteed unless the chosen generator provides it.

UUIDv7 and ULID both put time information first to support time ordering, but ULID’s short canonical text form does not by itself mean less database storage. RFC 9562 recommends storing UUIDs as their underlying 128-bit binary value where feasible: it notes that a UUID’s textual representation requires 288 bits, while binary storage can take less space and may make data access faster. See the RFC 9562 UUID specification and the ULID specification.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

What time ordering can—and cannot—do for a B-tree

A B-tree index keeps keys in sorted order. With UUIDv4, newly generated keys are scattered across that order, so inserts can target distant parts of the index. RFC 9562 describes this as poor database-index locality and notes that random-position inserts can have significant negative effects in B-trees and variants.

With UUIDv7, nearby generation times sort near one another as opaque bytes. ULID also sorts by time when represented and compared according to its specification. This is a locality advantage by design, not a guarantee that an index will never split pages, grow, or become fragmented. Real effects depend on the database engine and version, key representation, insert pattern, concurrency, generator behavior, and the rest of the workload.

The RFC puts the design rationale plainly: “Time-ordered monotonic UUIDs benefit from greater database-index locality because the new values are near each other in the index.” That is a statement about locality, not a universal benchmark result or a promised improvement in latency, index size, or throughput.

Generation order, clocks, and timestamp privacy

UUIDv7 ordering within a millisecond

UUIDv7’s timestamp is millisecond-based. RFC 9562 permits optional sub-millisecond precision and monotonicity mechanisms for generating multiple values during one timestamp tick. The exact guarantee depends on the implementation. Check whether the generator uses a counter or another monotonic approach, and how it behaves across processes or machines and when a clock moves backward.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

ULID ordering within a millisecond

The ULID specification describes a monotonic factory that increments the random component when it detects multiple calls in the same millisecond. Do not assume every ULID library uses that factory or provides the same guarantees. Verify the actual library’s process or node scope, clock-rollback behavior, and overflow handling; without suitable monotonic behavior, same-millisecond sort order is not guaranteed.

Creation-time exposure

Time-bearing IDs expose approximate creation time and relative order. RFC 9562 identifies timestamp disclosure as an attack surface and recommends UUIDv4 when UUIDs are required for a security operation in an application context. Regardless of format, do not use an identifier’s obscurity as a substitute for authorization checks.

PostgreSQL 18: native UUID storage and generation

PostgreSQL 18 documents the native uuid type as a 128-bit type that stores UUIDs conforming to RFC 9562 and accepts UUIDs of any version, regardless of origin. Its UUID functions include native UUIDv4 and UUIDv7 generation. This allows a new UUID-typed column to use UUIDv7 without changing the column’s type solely because of the UUID version.

Also popular on Amazon

For example, on PostgreSQL 18, a new table can define a UUIDv7 default with id uuid PRIMARY KEY DEFAULT uuidv7(). The uuidv7() function is documented as using Unix timestamp milliseconds along with sub-millisecond timestamp and random data. Check your exact PostgreSQL server version before relying on these native functions; see the PostgreSQL UUID type documentation and UUID functions documentation.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

PostgreSQL also documents UUID version and timestamp extraction functions. uuid_extract_timestamp works for versions 1 and 7, but the extracted time is not necessarily the exact instant an ID was generated; that depends on the generator implementation.

Which format should you choose?

Choose UUIDv7 for a new system when

  • Time-ordered key insertion is useful to your index workload.
  • Your platform has a maintained UUIDv7 generator with behavior you understand.
  • Exposing approximate creation time and order is acceptable.
  • Your database and applications can handle the UUID representation consistently.

Keep or choose UUIDv4 when

  • You prefer not to encode approximate creation time in identifiers.
  • Random-looking IDs are important to your application’s security context, while access control is still enforced separately.
  • Your measured index behavior is acceptable, or a different workload bottleneck matters more than key insertion order.

Consider ULID when

  • The canonical 26-character Crockford Base32 form is useful in logs, APIs, or operational workflows.
  • Your database and application stack consistently store and compare the chosen ULID representation.
  • You have verified the generator’s same-millisecond, multi-process, clock-rollback, and overflow behavior.
Independent reader supportYour contribution helps us test, update, and keep practical guides available for everyone.Support on Ko-Fi

How to benchmark before changing keys

There is no generally applicable performance percentage established for UUIDv4 versus UUIDv7 versus ULID. RFC 9562 explains why ordered UUIDs can improve index locality, but that design rationale is not a controlled comparative database benchmark. Measure the differences on the database, hardware, and workload that matter to you.

  1. Make the comparison fair. Keep the schema, row shape, indexes, database version, hardware, transaction settings, and workload the same. Change the identifier format and generator behavior, not several other variables at once.
  2. Match production conditions. Include realistic concurrency, write bursts, multiple generators, and the clock behavior of the generator deployment. If relevant to your application, include the queries and reads that share the same indexes.
  3. Record the outcomes that matter. Compare insert throughput and latency, index size and maintenance behavior, page splits or engine-specific index health indicators, write-ahead log or log volume where relevant, and representative query latency.
  4. Use the results to identify the bottleneck. If random key insertion is not a significant cost in your workload, changing ID format may not improve the performance problem you actually have.

When—and how—to migrate an existing UUIDv4 system

Changing the IDs of existing rows is an application and data migration, not an automatic database cleanup. Before considering it, profile index size, insert behavior, write load, cache pressure, and query performance. A decision to rekey should follow evidence that random key insertion is a meaningful cost and that the expected benefit justifies the migration risk.

Inventory the dependencies first

  • Primary keys and all foreign-key references.
  • External APIs, event payloads, caches, replicas, and other systems that store or compare IDs.
  • Application assumptions about key format, generation, sorting, or uniqueness.
  • Deployment and rollback constraints, including how old and new application versions will interpret IDs.

Choose a migration boundary

A lower-risk option is often to leave existing UUIDv4 values in place and generate UUIDv7 only for new rows, if every consumer accepts mixed UUID versions and the application does not assume that all IDs carry a timestamp or form one uniform time order. UUID-typed storage can hold different UUID versions, but mixed-population behavior still needs validation in your application and workload.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

If existing rows must be rekeyed, map out the primary-key and foreign-key changes, backfill and dual-write sequencing, uniqueness checks, compatibility deployments, rollback path, and cutover before starting. The right sequence depends on the schema and consumers; the cited UUID standards and PostgreSQL documentation do not define a universal zero-downtime migration procedure or a threshold at which migration is worthwhile.

Product prices and availability are accurate as of the date/time indicated and are subject to change. Any price and availability information displayed on Amazon at the time of purchase will apply. Prices as of November 28, 2026 2:51 am GMT.