写点什么

官方都不推荐?为什么 MySQL 不推荐使用 uuid 作为主键?究竟有什么坏处

  • 2021 年 11 月 11 日
  • 本文字数:2268 字

    阅读完需:约 7 分钟

`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;@SpringBootTestclass?MysqlDemoApp


【一线大厂Java面试题解析+后端开发学习笔记+最新架构讲解视频+实战项目源码讲义】
浏览器打开:qq.cn.hn/FTf 免费领取
复制代码


licationTests?{


@Autowiredprivate?JdbcTemplateService?jdbcTemplateService;


@Autowiredprivate?AutoKeyTableService?autoKeyTableService;


@Autowiredprivate?UUIDKeyTableService?uuidKeyTableService;


@Autowiredprivate?RandomKeyTableService?randomKeyTableService;


@Testvoid?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 写入结果:



user_random_key 写入结果:



user_uuid 表写入结果:


1.4.效率测试结果


在已有数据量为 130W 的时候:我们再来测试一下插入 10w 数据,看看会有什么结果:



可以看出在数据量 100W 左右的时候,uuid 的插入效率垫底,并且在后序增加了 130W 的数据,uudi 的时间又直线下降。


时间占用量总体可以打出的效率排名为:auto_key>random_key>uuid,uuid 的效率最低,在数据量较大的情况下,效率直线下滑。那么为什么会出现这样的现象呢?带着疑问,我们来探讨一下这个问题:

二、使用 uuid 和自增 id 的索引结构对比

2.1.使用自增 id 的内部结构


自增的主键的值是顺序的,所以 Innodb 把每一条记录都存储在一条记录的后面。当达到页面的最大填充因子时候(innodb 默认的最大填充因子是页大小的 15/16,会留出 1/16 的空间留作以后的修改):


①下一条记录就会写入新的页中,一旦数据按照这种顺序的方式加载,主键页就会近乎于顺序的记录填满,提升了页面的最大填充率,不会有页的浪费


②新插入的行一定会在原有的最大数据行下一行,mysql 定位和寻址很快,不会为计算新行的位置而做出额外的消耗


③减少了页分裂和碎片的产生

2.2.使用 uuid 的索引内部结构


因为 uuid 相对顺序的自增 id 来说是毫无规律可言的,新行的值不一定要比之前的主键的值要大,所以 innodb 无法做到总是把新行插入到索引的最后,而是需要为新行寻找新的合适的位置从而来分配新的空间。


这个过程需要做很多额外的操作,数据的毫无顺序会导致数据分布散乱,将会导致以下的问题:


①写入的目标页很可能已经刷新到磁盘上并且从缓存上移除,或者还没有被加载到缓存中,innodb 在插入之前不得不先找到并从磁盘读取目标页到内存中,这将导致大量的随机 IO


②因为写入是乱序的,innodb 不得不频繁的做页分裂操作,以便为新的行分配空间,页分裂导致移动大量的数据,一次插入最少需要修改三个页以上


③由于频繁的页分裂,页会变得稀疏并被不规则的填充,最终会导致数据会有碎片

评论

发布
暂无评论
官方都不推荐?为什么MySQL不推荐使用uuid作为主键?究竟有什么坏处