您的当前位置:首页 >探索 >为什么MySQL不推荐使用uuid或者雪花id作为主键? 正文

为什么MySQL不推荐使用uuid或者雪花id作为主键?

时间:2025-11-03 20:33:37 来源:网络整理编辑:探索

核心提示

复制packagecom.wyq.mysqldemo; importcn.hutool.core.collection.CollectionUtil; im

为什么MySQL不推荐使用uuid或者雪花id作为主键?
复制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时间消耗");          /**           * auto_increment key任务           */          final String insertSql = "INSERT INTO user_key_auto(user_id,推荐user_name,sex,address,city,email,state) VALUES(?,?,?,?,?,?,?)";          List<UserKeyAuto>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();          /**           * uudID的key           */          final String insertSql2 = "INSERT INTO user_uuid(id,user_id,user_name,sex,address,city,email,state) VALUES(?,?,?,?,?,?,?,?)";          List<UserKeyUUID>insertData2 = uuidKeyTableService.getInsertData();          stopwatch.start("UUID的WordPress模板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();          /**           * 随机的网站模板long值key           */          final String insertSql3 = "INSERT INTO user_random_key(id,user_id,user_name,sex,address,city,email,state) VALUES(?,?,?,?,?,?,?,?)";          List<UserKeyRandom>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);      }  1.2.3.4.5.6.7.8.9.10.11.12.13.14.15.16.17.18.19.20.21.22.23.24.25.26.27.28.29.30.31.32.33.34.35.36.37.38.39.40.41.42.43.44.45.46.47.48.49.50.51.52.53.54.55.56.57.58.59.60.61.62.63.64.65.66.67.68.69.70.71.72.b2b信息网