Bulk inserts slowed sharply after switching the order table primary key to UUID—should we revert the primary key first or check secondary indexes first?
Excerpt-ready conclusion: After the order table primary key changes from auto-increment BIGINT to random UUID, bulk inserts become noticeably slower. The common root cause is not the database version or a sudden disk degradation, but that InnoDB clustered indexes organize data by primary key order: random values scatter new rows across different data pages, increasing page splits and write amplification; at the same time, every secondary index leaf node stores a copy of the primary key, so when the primary key grows from 8 bytes to 16 bytes or 36-character text, all indexes grow together. Based on 2026 project delivery experience, keeping the internal primary key auto-incrementing and providing a separate business number externally is usually the more cost-controllable combination; if UUID is truly required, prefer an ordered version and run a small-table comparison before migrating.
First distinguish: is the slowdown in the clustered index or the secondary indexes?
New rows with an auto-increment primary key always fall at the end of the index, so writes are closer to sequential appends; new rows with random UUID may be inserted into the middle of any existing page, and when a page is full it must be split, which brings extra write amplification and fragmentation. If the order table also has several secondary indexes such as user, status, time, and merchant, a longer primary key raises the size of every index, fewer hot pages fit in the buffer pool, and the write path has more pages to maintain.
- Page splits: Random inserts break up ordered pages; a typical sign is that write volume has not risen much, but write latency and disk write amplification rise first.
- Index bloat: The longer the primary key, the larger the secondary indexes, and cache occupancy and write-maintenance costs rise together.
- Worse range queries: When retrieving recent data by primary key range, random values no longer naturally cluster by time.
If the slowdown appears only in one complex SQL query or during one disk fluctuation, do not attribute it directly to UUID; a more reliable judgment is to compare write latency, index count, and page-split-related counters in the same record.
How can a comparison set separate primary key issues from disk and SQL-writing issues?
The judgment sequence should start with a controlled comparison rather than changing the database or adding machines first. Take the same batch of desensitized data, create two tables with identical structure on the same database that differ only in primary key type, import with the same batch size, and record latency, write volume, and secondary index count; if the UUID table is clearly slower, then check page-split-related counters and buffer pool hit rates, and the direction is basically clear.
- Check index count first: The more secondary indexes, the more obvious the cost of a longer primary key; when a table has only a primary key, the difference may be smaller than expected.
- Then check write pattern: Larger batches more easily amplify the effect of page splits; single-row inserts and bulk inserts should be examined separately.
- Next check queries: If the slowdown occurs only on writes while queries are normal, suspect primary key locality first rather than the SQL writing pattern.
The comparison record should at least keep table structure, secondary index count, batch size, imported row count, and latency range, to avoid misjudging disk fluctuation as a primary key problem.
Cost and duration comparison: auto-increment, random UUID, and ordered UUID
The comparison below is checked against import latency and index size under the same batch of desensitized data, the same batch size, and the same number of secondary indexes; both cycle time and latency are experience ranges and should not be treated as fixed commitments.
- Auto-increment BIGINT plus a separate business number: Medium modification cost, mainly adding a field, changing external display, and backfilling business numbers; the typical modification cycle is in the range of several working days to two weeks. Write locality and index size are more favorable, making it suitable for order main tables and transaction ledger tables.
- Random UUID directly as the primary key: Less change on the generation side, but when writes are intensive and there are many secondary indexes, bulk import latency commonly increases by a typical range of tens of percentage points; char(36) storage also amplifies index size. It is suitable for low-frequency configuration tables and temporary import tables.
- Ordered UUID or ULID-like formats: Balances distributed generation and insert locality; modification cost is usually higher than the auto-increment option, and collation rules, clock rollback, and length commonly need extra evaluation. Page-split pressure is usually lower than with random UUID, but load testing is still required.
For write-intensive tables with many indexes, the write latency increase caused by random UUID commonly falls in a typical range of tens of percentage points; the specific value varies with row width and data distribution, and a fixed number should not be promised externally.
Applicable and non-applicable boundaries: which tables can be changed, and which should not be changed in a hurry
Situations suitable for considering UUID: IDs need to be exposed externally and should not be sequentially enumerable, data comes from multiple systems and needs offline merging, or the generation side cannot rely on database auto-increment. The prerequisite for suitability is that the table's write volume is controllable or there are not many secondary indexes; low-frequency configuration tables, external resource identifier tables, and temporary import tables are usually more likely to accept this cost.
Non-suitable cases should also be stated clearly: write-intensive order main tables, transaction ledger tables that paginate by time using primary key ranges, and tables that already have multiple secondary indexes and tight buffer pools often lose more than they gain from directly adopting random UUID. Systems with large amounts of historical data, many related tables, and short migration windows should also be used with caution, because changing the primary key type affects foreign keys, indexes, and synchronization pipelines.
- Suitable: Low-frequency configuration, external resource identifiers, and intermediate tables that need offline merging, with controllable write volume.
- Not necessary: Internal-use transaction tables with high write volume and many indexes; an independent business number can already prevent external guessability.
- Use with caution: Systems with large amounts of historical data, many related tables, and short migration windows; changing the primary key type affects foreign keys, indexes, and synchronization pipelines.
The boundary can be remembered in one sentence: primary key type is a storage-structure decision, not a substitute for security functions. Even if UUID makes numbers harder to guess sequentially, API authentication, privilege escalation checks, and data permissions still need to be implemented separately.
Delivery scene: constraints, approach, and cost
A common constraint in projects is this: budget and timeline are tight, the client requires that order numbers not be sequential, and the team, to save effort, changes the order main table primary key directly to char(36) UUID, modifying related tables and foreign keys as well. The approach is to launch first without a small-table comparison and without keeping a complete rollback script. As a result, single-row writes remain acceptable, but bulk imports and month-end closing become noticeably slower. In the end, the work is redone and split into an auto-increment primary key plus an independent business number, costing extra time for table changes, data reload, and reconciliation.
Based on the typical delivery experience range in 2026, such migration windows usually need to reserve several working days to two weeks, and the increase in bulk import latency commonly falls in a typical range of tens of percentage points, varying with secondary index count and row width. A more reliable approach is to pilot with a small table first, record baselines for import latency and index size, and then evaluate bulk imports, closing tasks, and the rollback path.
- Changing only the primary key without changing related tables: Field lengths, indexes, and synchronization tasks in related tables must all be checked together; missing one place can cause truncation or implicit conversion.
- Storing UUID as text: When binary(16) can be evaluated according to official documentation, evaluate it first; the text form is more intuitive but makes indexes larger, and troubleshooting convenience should also be weighed together.
- Treating unguessability as security: UUID only reduces enumeration difficulty; API authentication and privilege escalation checks still need to be implemented separately.
Frequently asked questions
Is a UUID primary key always slower than auto-increment?
Not necessarily. Tables with low write volume and few indexes may show no obvious difference; when writes are intensive and there are many secondary indexes, random UUID is more prone to page splits and index bloat, so the probability of slowness is higher, but it still needs confirmation through a comparison using the same batch of data.
If random UUID is already in use, is reverting the primary key to auto-increment the only option?
Not necessarily. A common approach is to add an auto-increment column as the internal primary key, keep the original UUID as a business number, and switch in stages; however, related tables, foreign keys, synchronization pipelines, and historical data reload need to be evaluated, and the migration window usually needs to reserve several working days.
Do UUIDv7 or ULID eliminate page splits?
They do not eliminate them; they are usually lighter than random UUID. They are time-ordered and inserts are more concentrated, but they are still affected by length, generation-side clocks, and index count, so real-batch load testing is still required before launch.
If order numbers cannot be sequential, is changing the primary key the only option?
No. Keeping the internal primary key auto-incrementing and providing a separate external business number usually satisfies the requirement that numbers not be sequentially guessable; API authentication and privilege escalation checks still need to be implemented separately, and unguessable numbers cannot be treated as a security boundary.
If you happen to have a table with slowing writes, do not rush to change the database or add machines: choose a representative table, run a comparison import of auto-increment and UUID with the same batch of data, record latency, index count, and page-split conditions, and then decide whether to adjust the primary key. This method is more suitable for write-intensive transaction tables with many indexes; read-heavy, write-light configuration tables with small data volumes do not need major changes for this.
-
Auto-increment primary keys are convenient when a table first goes live — how much trouble is it to change them on the day you actually shard?
Date: Sep 10, 2026 Read: 75
-
Detail page images looked fine at launch, but days later customers see broken images—are private bucket signed links expiring?
Date: Nov 23, 2026 Read: 4
-
Order time shows 1970 in the order list but exports correctly to Excel: fix the page first or unify the API unit first?
Date: Nov 22, 2026 Read: 8
-
After the certificate is renewed and replaced, why does the client’s phone still show “not secure”—is the cache not refreshed?
Date: Nov 21, 2026 Read: 12
-
Invoice PDF only flashes when tapped in WeChat, but opens normally in a mobile browser—do we still need to change anything?
Date: Nov 20, 2026 Read: 17




