UUID v4 vs v7: which one to use for database keys

UUIDs are popular primary keys: any server, phone or browser can create one without asking the database, and they don’t reveal how many orders or users you have. But teams that switch from auto-increment integers to random UUIDs often notice inserts slowing down as a table grows into the millions. The cause is not UUIDs in general — it is random UUIDs. Version 7 fixes that, with one trade-off you should understand before adopting it.

Anatomy of a UUID

A UUID is a 128-bit number, usually written as 32 hex digits in five groups: 8-4-4-4-12. The current standard is RFC 9562 (2024), which replaced RFC 4122 and added versions 6, 7 and 8. Two fields are fixed in every modern UUID:

7a8d7dc8-90c5-429d-9850-667f47584c94   ← v4 (version 4, variant digit 9)
01a0e105-0e00-7014-8d82-8f82c27a30ca   ← v7 (version 7, variant digit 8)

That leaves 122 bits for the actual content. What goes into those bits is the whole difference between v4 and v7.

v4: 122 random bits

A v4 UUID is pure randomness from a cryptographically secure generator. How likely is a collision? By the birthday problem, with n UUIDs the probability of any duplicate is about n² / (2 × 2¹²²):

UUIDs generatedChance of at least one duplicate
1 billion (10⁹)about 9.4 × 10⁻²⁰
1 trillion (10¹²)about 9.4 × 10⁻¹⁴
2.7 × 10¹⁸about 50%

Reaching a 50% chance would take a billion UUIDs per second for roughly 86 years. In practice, v4 collisions come from bugs — a badly seeded or non-cryptographic random generator, or cloned virtual machines — not from the math. Always use your platform’s secure generator, never Math.random().

Why random keys hurt B-tree indexes

Most databases store primary keys in a B-tree, kept in sorted order in fixed-size pages. With an auto-increment key, each new row goes at the right-hand end of the tree: the same few pages stay hot in memory and fill up completely. With v4 keys, each insert lands on a random page anywhere in the index. That causes:

In MySQL’s InnoDB the effect is stronger, because the table itself is stored in primary-key order (a clustered index), and every secondary index stores the primary key too.

When does it matter? For a table with a few hundred thousand rows, almost never — the index fits in memory and you won’t measure a difference. It starts to matter with tens of millions of rows, high insert rates, or indexes larger than RAM. Don’t rewrite a small app for this; do choose carefully for a new high-volume table.

v7: timestamp first, then randomness

A v7 UUID starts with a 48-bit Unix timestamp in milliseconds, followed by the version, then random bits (74 in total after the version and variant). Because the time is at the front, new UUIDs sort after old ones, and inserts go to the end of the index — like auto-increment, but still generated anywhere without coordination.

BitsField
48Unix time in milliseconds (big-endian)
4Version (0111 = 7)
12Random, or extra sub-millisecond precision / counter
2Variant (10)
62Random

48 bits of milliseconds lasts until the year 10889, so overflow is not a concern. Two details to know:

// Read the creation time from a v7 UUID
const id = "01a0e105-0e00-7014-8d82-8f82c27a30ca";
new Date(parseInt(id.replace(/-/g, "").slice(0, 12), 16)).toISOString();
// "2026-09-27T04:00:00.000Z"

Alternatives in brief

Generating UUIDs

// JavaScript: v4 is built in (browsers in secure contexts, Node.js)
crypto.randomUUID();   // "7a8d7dc8-90c5-429d-9850-667f47584c94"

// v7 by hand, following the RFC 9562 layout
function uuidv7() {
  const bytes = crypto.getRandomValues(new Uint8Array(16));
  let ms = BigInt(Date.now());
  for (let i = 5; i >= 0; i--) { bytes[i] = Number(ms & 0xffn); ms >>= 8n; }
  bytes[6] = (bytes[6] & 0x0f) | 0x70;   // version 7
  bytes[8] = (bytes[8] & 0x3f) | 0x80;   // variant 10
  const hex = [...bytes].map((b) => b.toString(16).padStart(2, "0")).join("");
  return `${hex.slice(0, 8)}-${hex.slice(8, 12)}-${hex.slice(12, 16)}-${hex.slice(16, 20)}-${hex.slice(20)}`;
}

In production, the widely used uuid npm package also provides v7().

-- PostgreSQL
SELECT gen_random_uuid();   -- v4, built in since PostgreSQL 13
SELECT uuidv7();            -- v7, PostgreSQL 18 and later

CREATE TABLE orders (
  id uuid PRIMARY KEY DEFAULT uuidv7(),
  created_at timestamptz NOT NULL DEFAULT now()
);
# Python
import uuid
uuid.uuid4()   # every version of Python 3
uuid.uuid7()   # Python 3.14 and later

Store them as 16 bytes, not text

The text form is 36 characters; the value is 16 bytes. Storing text more than doubles the size of the key and of every index and foreign key that contains it, and string comparison is slower than byte comparison.

Generate v4 or v7 UUIDs in bulk. The UUID Generator creates either version, lets you copy or download them, and checks the version of any UUID you paste. Try it now

FAQ

Should I migrate my existing v4 keys to v7?

Usually not. Changing primary keys means rewriting every foreign key. If inserts are fine today, keep v4 and use v7 for new tables. Migrate only if you’ve measured an index problem.

Is v7 less secure than v4?

It has fewer random bits (74 vs 122), which is still far too many to guess. The real difference is the readable timestamp. Neither version should be used as a secret, such as a password-reset token — use a dedicated random token for that.

Can I sort by a v7 UUID instead of a created_at column?

Roughly, yes, at millisecond precision — but clocks on different servers drift, and order within one millisecond may be random. Keep a real timestamp column if exact ordering or reporting matters.

UUID v4 না v7: ডেটাবেস কী-এর জন্য কোনটি

Primary key হিসেবে UUID বেশ জনপ্রিয়: যেকোনো সার্ভার, ফোন বা ব্রাউজার ডেটাবেসকে জিজ্ঞেস না করেই একটা তৈরি করতে পারে, আর এতে বোঝা যায় না আপনার কতগুলো অর্ডার বা ইউজার আছে। কিন্তু auto-increment integer ছেড়ে র‍্যান্ডম UUID-তে যাওয়া অনেক টিম দেখে, টেবিল লাখ-কোটিতে বড় হতে থাকলে insert ধীর হয়ে যাচ্ছে। দোষটা UUID-এর নয় — দোষ র‍্যান্ডম UUID-এর। Version 7 এই সমস্যা মেটায়, তবে একটা ছাড় দিয়ে, যা ব্যবহারের আগে বুঝে নেওয়া দরকার।

UUID-এর গঠন

UUID হলো একটি 128-bit সংখ্যা, সাধারণত লেখা হয় পাঁচ ভাগে ৩২টি hex অঙ্কে: 8-4-4-4-12। বর্তমান স্ট্যান্ডার্ড RFC 9562 (2024), যা RFC 4122-এর জায়গা নিয়েছে এবং version 6, 7 ও 8 যোগ করেছে। আধুনিক প্রতিটি UUID-এ দুটি ফিল্ড নির্দিষ্ট:

7a8d7dc8-90c5-429d-9850-667f47584c94   ← v4 (version 4, variant digit 9)
01a0e105-0e00-7014-8d82-8f82c27a30ca   ← v7 (version 7, variant digit 8)

বাকি থাকে আসল তথ্যের জন্য 122 bit। এই bit-গুলোতে কী বসে, সেটাই v4 আর v7-এর পুরো পার্থক্য।

v4: 122টি র‍্যান্ডম bit

v4 UUID পুরোটাই ক্রিপ্টোগ্রাফিকভাবে নিরাপদ জেনারেটরের র‍্যান্ডমনেস। collision বা একই মান দুবার হওয়ার সম্ভাবনা কত? birthday problem অনুযায়ী, nটি UUID থাকলে কোনো একটি duplicate হওয়ার সম্ভাবনা মোটামুটি n² / (2 × 2¹²²):

কতগুলো UUIDঅন্তত একটি duplicate-এর সম্ভাবনা
১০০ কোটি (10⁹)প্রায় 9.4 × 10⁻²⁰
১ লাখ কোটি (10¹²)প্রায় 9.4 × 10⁻¹⁴
2.7 × 10¹⁸প্রায় ৫০%

৫০% সম্ভাবনায় পৌঁছাতে প্রতি সেকেন্ডে ১০০ কোটি UUID বানাতে হবে টানা প্রায় ৮৬ বছর। বাস্তবে v4 collision আসে বাগ থেকে — ঠিকমতো seed না করা বা non-cryptographic র‍্যান্ডম জেনারেটর, কিংবা clone করা ভার্চুয়াল মেশিন — অঙ্ক থেকে নয়। সবসময় প্ল্যাটফর্মের নিরাপদ জেনারেটর ব্যবহার করুন, কখনো Math.random() নয়।

র‍্যান্ডম কী কেন B-tree ইনডেক্সের ক্ষতি করে

বেশিরভাগ ডেটাবেস primary key রাখে B-tree-তে, নির্দিষ্ট আকারের page-এ সাজানো ক্রমে। auto-increment কী হলে প্রতিটি নতুন row যায় tree-এর একেবারে ডান প্রান্তে: একই কয়েকটা page মেমরিতে গরম থাকে আর পুরোপুরি ভরে ওঠে। v4 কী হলে প্রতিটি insert পড়ে ইনডেক্সের যেকোনো র‍্যান্ডম page-এ। এর ফল:

MySQL-এর InnoDB-তে প্রভাবটা আরও বেশি, কারণ টেবিলটাই primary key-এর ক্রমে রাখা হয় (clustered index), আর প্রতিটি secondary index-এও primary key থাকে।

কখন এটা আসলে গুরুত্বপূর্ণ? কয়েক লাখ row-এর টেবিলে প্রায় কখনোই না — ইনডেক্স মেমরিতে এঁটে যায়, পার্থক্য মাপতেও পারবেন না। সমস্যা শুরু হয় কোটি কোটি row, খুব বেশি insert-এর হার, বা RAM-এর চেয়ে বড় ইনডেক্সে। ছোট অ্যাপ এর জন্য নতুন করে লিখবেন না; তবে নতুন বড়-ভলিউমের টেবিলের বেলায় ভেবে বেছে নিন।

v7: আগে সময়, তারপর র‍্যান্ডমনেস

v7 UUID শুরু হয় মিলিসেকেন্ডে 48-bit Unix timestamp দিয়ে, তারপর version, তারপর র‍্যান্ডম bit (version ও variant বাদে মোট 74টি)। সময়টা সামনে থাকায় নতুন UUID পুরোনোগুলোর পরে সাজে, আর insert যায় ইনডেক্সের শেষ প্রান্তে — auto-increment-এর মতোই, অথচ কোনো সমন্বয় ছাড়াই যেকোনো জায়গায় তৈরি করা যায়।

Bitফিল্ড
48মিলিসেকেন্ডে Unix সময় (big-endian)
4Version (0111 = 7)
12র‍্যান্ডম, অথবা মিলিসেকেন্ডের চেয়ে সূক্ষ্ম সময় / counter
2Variant (10)
62র‍্যান্ডম

48 bit মিলিসেকেন্ড চলবে 10889 সাল পর্যন্ত, তাই overflow নিয়ে ভাবনা নেই। দুটি বিষয় জেনে রাখুন:

// v7 UUID থেকে তৈরির সময় পড়া
const id = "01a0e105-0e00-7014-8d82-8f82c27a30ca";
new Date(parseInt(id.replace(/-/g, "").slice(0, 12), 16)).toISOString();
// "2026-09-27T04:00:00.000Z"

বিকল্পগুলো সংক্ষেপে

UUID তৈরি করা

// JavaScript: v4 বিল্ট-ইন (secure context-এ ব্রাউজার, Node.js)
crypto.randomUUID();   // "7a8d7dc8-90c5-429d-9850-667f47584c94"

// RFC 9562-এর লেআউট মেনে হাতে v7
function uuidv7() {
  const bytes = crypto.getRandomValues(new Uint8Array(16));
  let ms = BigInt(Date.now());
  for (let i = 5; i >= 0; i--) { bytes[i] = Number(ms & 0xffn); ms >>= 8n; }
  bytes[6] = (bytes[6] & 0x0f) | 0x70;   // version 7
  bytes[8] = (bytes[8] & 0x3f) | 0x80;   // variant 10
  const hex = [...bytes].map((b) => b.toString(16).padStart(2, "0")).join("");
  return `${hex.slice(0, 8)}-${hex.slice(8, 12)}-${hex.slice(12, 16)}-${hex.slice(16, 20)}-${hex.slice(20)}`;
}

প্রোডাকশনে বহুল ব্যবহৃত uuid npm প্যাকেজেও v7() আছে।

-- PostgreSQL
SELECT gen_random_uuid();   -- v4, PostgreSQL 13 থেকে বিল্ট-ইন
SELECT uuidv7();            -- v7, PostgreSQL 18 ও পরের সংস্করণ

CREATE TABLE orders (
  id uuid PRIMARY KEY DEFAULT uuidv7(),
  created_at timestamptz NOT NULL DEFAULT now()
);
# Python
import uuid
uuid.uuid4()   # Python 3-এর সব সংস্করণে
uuid.uuid7()   # Python 3.14 ও পরের সংস্করণে

টেক্সট নয়, ১৬ বাইট হিসেবে রাখুন

টেক্সট রূপটা ৩৬ অক্ষরের; আসল মান ১৬ বাইট। টেক্সট হিসেবে রাখলে কী-এর আকার দ্বিগুণেরও বেশি হয় — সাথে যেসব ইনডেক্স ও foreign key-তে এটা আছে সেগুলোরও — আর স্ট্রিং তুলনা বাইট তুলনার চেয়ে ধীর।

একসাথে অনেক v4 বা v7 UUID তৈরি করুন। UUID জেনারেটর যেকোনো version বানায়, কপি বা ডাউনলোড করতে দেয়, আর পেস্ট করা যেকোনো UUID-এর version যাচাই করে। এখনই চেষ্টা করুন

প্রশ্নোত্তর

আমার পুরোনো v4 কী-গুলো কি v7-এ বদলে ফেলা উচিত?

সাধারণত না। primary key বদলানো মানে প্রতিটি foreign key নতুন করে লেখা। আজ insert ঠিকঠাক চললে v4 রেখে দিন, নতুন টেবিলে v7 নিন। ইনডেক্সের সমস্যা মেপে নিশ্চিত হলে তবেই মাইগ্রেট করুন।

v7 কি v4-এর চেয়ে কম নিরাপদ?

এতে র‍্যান্ডম bit কম (122-এর বদলে 74), তবু অনুমান করে বের করার পক্ষে তা অনেক বেশি। আসল পার্থক্য হলো পড়া যায় এমন timestamp। কোনো version-ই গোপন মান হিসেবে — যেমন পাসওয়ার্ড রিসেট টোকেন — ব্যবহার করবেন না; সেজন্য আলাদা র‍্যান্ডম টোকেন নিন।

created_at কলামের বদলে কি v7 UUID দিয়ে sort করা যায়?

মোটামুটি হ্যাঁ, মিলিসেকেন্ড পর্যন্ত — কিন্তু আলাদা সার্ভারের ঘড়ি একটু এদিক-ওদিক হয়, আর একই মিলিসেকেন্ডের ভেতরের ক্রম র‍্যান্ডম হতে পারে। নিখুঁত ক্রম বা রিপোর্টিং দরকার হলে আসল timestamp কলাম রাখুন।

Related guides

More free tools