Databases 8 min read

Why UUID Primary Keys Slow Down MySQL and How to Fix Them

Using UUIDs as primary keys in MySQL can severely degrade performance due to larger index size, random insertion order, and costly updates, but the article explains the underlying reasons and offers practical optimizations such as ordered UUIDs, binary storage, hybrid auto‑increment keys, and table partitioning.

Top Architect
Top Architect
Top Architect
Why UUID Primary Keys Slow Down MySQL and How to Fix Them

1. Problems with using UUID as a primary key

Characteristics of UUID

UUID is a 128‑bit identifier usually represented as a 36‑character string, e.g. 550e8400-e29b-41d4-a716-446655440000.

It is globally unique, which makes it convenient for distributed systems.

Disadvantages

Index size : Stored as CHAR(36) a UUID occupies 36 bytes, while a BIGINT primary key occupies only 8 bytes. Larger indexes increase storage and I/O costs.

Index fragmentation : UUIDs are essentially random and unordered. Inserting new rows can cause frequent B‑tree splits and rebalancing, degrading insert performance.

Insert performance : Random placement of rows forces InnoDB to split pages, leading to extra disk I/O.

Query performance : Comparing 36‑byte strings is slower than comparing 8‑byte integers, and the larger index widens the scan range. Example: WHERE id = '550e8400-e29b-41d4-a716-446655440000' is noticeably slower than WHERE id = 12345.

2. Why updating data triggers index refresh

Indexes (typically B+ trees) accelerate query processing.

Updating a primary‑key value : MySQL must delete the old index entry and insert a new one, causing tree adjustments and additional I/O.

Updating an indexed non‑primary column : The corresponding index record also needs to be updated, incurring the same overhead.

Because UUIDs are unordered, a change often moves the row to a different position in the index tree, making the operation more expensive than with an ordered key such as an auto‑increment integer.

3. Why character (string) primary keys reduce efficiency

Larger storage footprint : String keys consume more bytes, increasing the amount of data read/written during scans and updates.

Slower comparison : String comparison is computationally heavier than integer comparison, especially on large tables.

Frequent index splits : Unordered string keys cause the B‑tree to split more often, adding overhead to inserts and updates.

4. Optimising UUID primary‑key performance

Use ordered UUIDs (e.g., UUIDv7) that embed a timestamp, preserving insert order and reducing page splits.

Store UUIDs in binary form . Define the column as BINARY(16) instead of CHAR(36) to halve the storage size.

CREATE TABLE users (
    id BINARY(16) PRIMARY KEY,
    name VARCHAR(255)
);

Hybrid auto‑increment + UUID : Use an auto‑increment BIGINT as the physical primary key and keep a UUID column for logical uniqueness.

CREATE TABLE users (
    id BIGINT AUTO_INCREMENT PRIMARY KEY,
    uuid CHAR(36) UNIQUE,
    name VARCHAR(255)
);

Partition large tables : Split a massive table into partitions (by range, hash, etc.) to keep each index tree smaller, which improves both insert and query performance.

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

optimizationmysqluuidindexprimary key
Top Architect
Written by

Top Architect

Top Architect focuses on sharing practical architecture knowledge, covering enterprise, system, website, large‑scale distributed, and high‑availability architectures, plus architecture adjustments using internet technologies. We welcome idea‑driven, sharing‑oriented architects to exchange and learn together.

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.