使用雪花id或uuid作为Mysql主键,被老板怼了一顿!
互联网架构师
共 8566字,需浏览 18分钟
· 2022-03-09
点击关注公众号,回复“2T”获取2TB学习资源!
互联网架构师后台回复 2T 有特别礼包
上一篇:再说一次,别去外包!
作者:老男孩的架构路
链接:https://www.jianshu.com/p/b0602394869e
一:mysql和程序实例
![](https://filescdn.proginn.com/fb225bb403e95dd757ea0ae6b876dc3e/16e1d9bc6a19ef4cc98494cedb515cc3.webp)
用户uuid表
![](https://filescdn.proginn.com/404e539f3a9477b359126855e949d3e2/3db4ec1e4dbfc4133c5ef827e4263df6.webp)
![](https://filescdn.proginn.com/aefffcce7c69e6aba35b58169b7f3c3c/f1349da4ff4de7e8b302e7541c6d263a.webp)
1.2:光有理论不行,直接上程序,使用spring的jdbcTemplate来实现增查测试:
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.3:程序写入结果
user_key_auto写入结果:
![](https://filescdn.proginn.com/f86877d165085b78f6f9d0f6019ed607/ce0c51c101ae60ceab23ae7125d839f1.webp)
![](https://filescdn.proginn.com/422df718bbdb5feb5bb87e35f2613ecd/bf47936ca41d4982eb5f74a246d29d38.webp)
![](https://filescdn.proginn.com/d4d3865a34e03074f47258af9b52ff1d/896563ed3ec13fcea449a4ef39ab9bb0.webp)
1.4:效率测试结果
![](https://filescdn.proginn.com/571555ca3f06d4c0bb0acb8fe006010a/2935acc6669d79eba7407c78f8bb0bed.webp)
在已有数据量为130W的时候:我们再来测试一下插入10w数据,看看会有什么结果:
![](https://filescdn.proginn.com/18c64f4e25158ecdddc459b59410579d/b39e7fe19730d79271f07116072eb752.webp)
二:使用uuid和自增id的索引结构对比
2.1:使用自增id的内部结构
![](https://filescdn.proginn.com/4fe9ca29a719d899064129bc1e301a1f/652f7603b002c956a58d7ed80eb88808.webp)
2.2:使用uuid的索引内部结构
![](https://filescdn.proginn.com/548d09383aad2dd5d2075b9a03ba1b4a/b68fbefdcc331f57c03ff5247043f867.webp)
2.3:使用自增id的缺点
附:Auto_increment的锁争抢问题,如果要改善需要调优innodb_autoinc_lock_mode的配置
三:总结
-End-
正文结束
1.心态崩了!税前2万4,到手1万4,年终奖扣税方式1月1日起施行~
评论