搜索

为啥不能用uuid做MySQL的主键?

发表于 2025-11-04 07:53:44 来源:全栈开发
为啥不能用uuid做MySQL的主键?
复制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的免费信息发布网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.73.74.75.76.77.78.79.80.81.82.83.84.85.86.87.88.89.90.91.92.云服务器提供商
随机为您推荐
版权声明:本站资源均来自互联网,如果侵犯了您的权益请与我们联系,我们将在24小时内删除。

Copyright © 2016 Powered by 为啥不能用uuid做MySQL的主键?,全栈开发  滇ICP备2023006006号-32sitemap

回顶部