Databases 12 min read

Performance Comparison of Auto‑Increment, UUID, and Random Keys in MySQL

This article evaluates MySQL's recommendation to avoid UUID or non‑sequential snowflake IDs by benchmarking three tables—auto‑increment, UUID, and random key—using Spring Boot and JdbcTemplate, then analyzes their index structures, insertion speed, and trade‑offs.

Top Architect
Top Architect
Top Architect
Performance Comparison of Auto‑Increment, UUID, and Random Keys in MySQL

MySQL officially recommends using auto_increment primary keys instead of UUID or non‑sequential snowflake IDs; this article investigates the reasons behind that recommendation.

Three tables (user_auto_key, user_uuid, user_random_key) are created, each identical except for the primary‑key generation strategy. Insert and query performance are measured under the same environment using Spring Boot, JdbcTemplate, JUnit, and Hutool.

The benchmark code (shown below) obtains test data from service classes, inserts it with jdbcTemplateService.insert , and records elapsed time with a StopWatch instance.

package com.wyq.mysqldemo;
import cn.hutool.core.collection.CollectionUtil;
import com.wyq.mysqldemo.databaseobject.UserKeyAuto;
import com.wyq.mysqldemo.databaseobject.UserKeyRandom;
import com.wyq.mysqldemo.databaseobject.UserKeyUUID;
import com.wyq.mysqldemo.diffkeytest.AutoKeyTableService;
import com.wyq.mysqldemo.diffkeytest.RandomKeyTableService;
import com.wyq.mysqldemo.diffkeytest.UUIDKeyTableService;
import com.wyq.mysqldemo.util.JdbcTemplateService;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.util.StopWatch;
import java.util.List;
@SpringBootTest
class MysqlDemoApplicationTests {
    @Autowired
    private JdbcTemplateService jdbcTemplateService;
    @Autowired
    private AutoKeyTableService autoKeyTableService;
    @Autowired
    private UUIDKeyTableService uuidKeyTableService;
    @Autowired
    private RandomKeyTableService randomKeyTableService;
    @Test
    void testDBTime() {
        StopWatch stopwatch = new StopWatch("执行sql时间消耗");
        final String insertSql = "INSERT INTO user_key_auto(user_id,user_name,sex,address,city,email,state) VALUES(?,?,?,?,?,?,?)";
        List
insertData = autoKeyTableService.getInsertData();
        stopwatch.start("自动生成key表任务开始");
        long start1 = System.currentTimeMillis();
        if (CollectionUtil.isNotEmpty(insertData)) {
            boolean insertResult = jdbcTemplateService.insert(insertSql, insertData, false);
            System.out.println(insertResult);
        }
        long end1 = System.currentTimeMillis();
        System.out.println("auto key消耗的时间:" + (end1 - start1));
        stopwatch.stop();
        final String insertSql2 = "INSERT INTO user_uuid(id,user_id,user_name,sex,address,city,email,state) VALUES(?,?,?,?,?,?,?,?)";
        List
insertData2 = uuidKeyTableService.getInsertData();
        stopwatch.start("UUID的key表任务开始");
        long begin = System.currentTimeMillis();
        if (CollectionUtil.isNotEmpty(insertData)) {
            boolean insertResult = jdbcTemplateService.insert(insertSql2, insertData2, true);
            System.out.println(insertResult);
        }
        long over = System.currentTimeMillis();
        System.out.println("UUID key消耗的时间:" + (over - begin));
        stopwatch.stop();
        final String insertSql3 = "INSERT INTO user_random_key(id,user_id,user_name,sex,address,city,email,state) VALUES(?,?,?,?,?,?,?,?)";
        List
insertData3 = randomKeyTableService.getInsertData();
        stopwatch.start("随机的long值key表任务开始");
        Long start = System.currentTimeMillis();
        if (CollectionUtil.isNotEmpty(insertData)) {
            boolean insertResult = jdbcTemplateService.insert(insertSql3, insertData3, true);
            System.out.println(insertResult);
        }
        Long end = System.currentTimeMillis();
        System.out.println("随机key任务消耗时间:" + (end - start));
        stopwatch.stop();
        String result = stopwatch.prettyPrint();
        System.out.println(result);
    }
}

Insertion results (shown in screenshots) indicate that with 1.3 million existing rows, inserting 100 k new rows yields the ranking: auto_increment fastest, random_key second, UUID slowest; UUID performance drops sharply as data volume grows.

Index‑structure analysis explains that auto_increment keys generate sequential clustered index pages, minimizing page splits, random I/O, and fragmentation, while UUID keys are random, causing frequent page splits, random disk reads, and overall fragmentation that harms performance.

Despite its speed advantages, auto_increment has drawbacks: it can expose business growth, become a hotspot under high concurrency due to lock contention, and incur auto_increment lock overhead.

Conclusion: For InnoDB tables, prefer monotonic increasing primary keys (auto_increment) for optimal insert performance, while being aware of the security and concurrency trade‑offs.

performancedatabaseMySQLauto-incrementUUIDindexprimary 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

login 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.