最新消息: 你生产环境的Composer是怎么样的?
您现在的位置是:群英 > 数据库 > MySQL数据库 >
为什么MySQL不使用雪花id或uuid为主键?
Java知音发表于 2020-09-01 15:43 次浏览

为什么MySQL不使用雪花id或uuid为主键?今天小编给大家详细的讲述一下,他们里面的关系:

前言

在mysql中设计表的时候,mysql官方推荐不要使用uuid或者不连续不重复的雪花id(long形且唯一,单机递增),而是推荐连续自增的主键id,官方的推荐是auto_increment,那么为什么不建议采用uuid,使用uuid究竟有什么坏处?

本篇博客我们就来分析这个问题,探讨一下内部的原因。

本篇博客的目录

  •  mysql程序实例
  •  使用uuid和自增id的索引结构对比
  •  总结

一、mysql和程序实例

1.1.要说明这个问题,我们首先来建立三张表

分别是user_auto_key,user_uuid,user_random_key,分别表示自动增长的主键,uuid作为主键,随机key作为主键,其它我们完全保持不变.

根据控制变量法,我们只把每个表的主键使用不同的策略生成,而其他的字段完全一样,然后测试一下表的插入速度和查询速度:

注:这里的随机key其实是指用雪花算法算出来的前后不连续不重复无规律的id:一串18位长度的long值

id自动生成表:

用户uuid表

随机主键表:

 

1.2.光有理论不行,直接上程序,使用spring的jdbcTemplate来实现增查测试:

技术框架:springboot+jdbcTemplate+junit+hutool,程序的原理就是连接自己的测试数据库,然后在相同的环境下写入同等数量的数据,来分析一下insert插入的时间来进行综合其效率,为了做到最真实的效果,所有的数据采用随机生成,比如名字、邮箱、地址都是随机生成。

搜索Java知音公众号,回复“后端面试”,送你一份Java面试题宝典

 

  1. package com.wyq.mysqldemo;  
  2. import cn.hutool.core.collection.CollectionUtil;  
  3. import com.wyq.mysqldemo.databaseobject.UserKeyAuto;  
  4. import com.wyq.mysqldemo.databaseobject.UserKeyRandom;  
  5. import com.wyq.mysqldemo.databaseobject.UserKeyUUID;  
  6. import com.wyq.mysqldemo.diffkeytest.AutoKeyTableService;  
  7. import com.wyq.mysqldemo.diffkeytest.RandomKeyTableService;  
  8. import com.wyq.mysqldemo.diffkeytest.UUIDKeyTableService;  
  9. import com.wyq.mysqldemo.util.JdbcTemplateService;  
  10. import org.junit.jupiter.api.Test;  
  11. import org.springframework.beans.factory.annotation.Autowired;  
  12. import org.springframework.boot.test.context.SpringBootTest;  
  13. import org.springframework.util.StopWatch;  
  14. import java.util.List;  
  15. @SpringBootTest  
  16. class MysqlDemoApplicationTests {  
  17.     @Autowired  
  18.     private JdbcTemplateService jdbcTemplateService; 
  19.      @Autowired  
  20.     private AutoKeyTableService autoKeyTableService;  
  21.     @Autowired  
  22.     private UUIDKeyTableService uuidKeyTableService;  
  23.     @Autowired  
  24.     private RandomKeyTableService randomKeyTableService;  
  25.     @Test  
  26.     void testDBTime() {  
  27.         StopWatch stopwatch = new StopWatch("执行sql时间消耗");  
  28.         /**  
  29.          * auto_increment key任务  
  30.          */  
  31.         final String insertSql = "INSERT INTO user_key_auto(user_id,user_name,sex,address,city,email,state) VALUES(?,?,?,?,?,?,?)" 
  32.         List<UserKeyAuto> insertData = autoKeyTableService.getInsertData();  
  33.         stopwatch.start("自动生成key表任务开始");  
  34.         long start1 = System.currentTimeMillis();  
  35.         if (CollectionUtil.isNotEmpty(insertData)) {  
  36.             boolean insertResult = jdbcTemplateService.insert(insertSql, insertData, false);  
  37.             System.out.println(insertResult);  
  38.         }  
  39.         long end1 = System.currentTimeMillis();  
  40.         System.out.println("auto key消耗的时间:" + (end1 - start1));  
  41.         stopwatch.stop();  
  42.         /**  
  43.          * uudID的key  
  44.          */  
  45.         final String insertSql2 = "INSERT INTO user_uuid(id,user_id,user_name,sex,address,city,email,state) VALUES(?,?,?,?,?,?,?,?)" 
  46.         List<UserKeyUUID> insertData2 = uuidKeyTableService.getInsertData();  
  47.         stopwatch.start("UUID的key表任务开始");  
  48.         long begin = System.currentTimeMillis(); 
  49.          if (CollectionUtil.isNotEmpty(insertData)) {  
  50.             boolean insertResult = jdbcTemplateService.insert(insertSql2, insertData2, true);  
  51.             System.out.println(insertResult);  
  52.         }  
  53.         long over = System.currentTimeMillis();  
  54.         System.out.println("UUID key消耗的时间:" + (over - begin));  
  55.         stopwatch.stop();  
  56.         /**  
  57.          * 随机的long值key  
  58.          */  
  59.         final String insertSql3 = "INSERT INTO user_random_key(id,user_id,user_name,sex,address,city,email,state) VALUES(?,?,?,?,?,?,?,?)" 
  60.         List<UserKeyRandom> insertData3 = randomKeyTableService.getInsertData();  
  61.         stopwatch.start("随机的long值key表任务开始");  
  62.         Long start = System.currentTimeMillis();  
  63.         if (CollectionUtil.isNotEmpty(insertData)) {  
  64.             boolean insertResult = jdbcTemplateService.insert(insertSql3, insertData3, true);  
  65.             System.out.println(insertResult);  
  66.         }  
  67.         Long end = System.currentTimeMillis();  
  68.         System.out.println("随机key任务消耗时间:" + (end - start));  
  69.         stopwatch.stop();  
  70.         String result = stopwatch.prettyPrint();  
  71.         System.out.println(result); 
  72.      }

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的内部结构

 

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

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

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

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

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

在把随机值(uuid和雪花id)载入到聚簇索引(innodb默认的索引类型)以后,有时候会需要做一次OPTIMEIZE TABLE来重建表并优化页的填充,这将又需要一定的时间消耗。

结论:使用innodb应该尽可能的按主键的自增顺序插入,并且尽可能使用单调的增加的聚簇键的值来插入新行

搜索Java知音公众号,回复“后端面试”,送你一份Java面试题宝典

2.3.使用自增id的缺点

那么使用自增的id就完全没有坏处了吗?并不是,自增id也会存在以下几点问题:

①别人一旦爬取你的数据库,就可以根据数据库的自增id获取到你的业务增长信息,很容易分析出你的经营情况

②对于高并发的负载,innodb在按主键进行插入的时候会造成明显的锁争用,主键的上界会成为争抢的热点,因为所有的插入都发生在这里,并发插入会导致间隙锁竞争

③Auto_Increment锁机制会造成自增锁的抢夺,有一定的性能损失

附:Auto_increment的锁争抢问题,如果要改善需要调优innodb_autoinc_lock_mode的配置

三、总结

本篇博客首先从开篇的提出问题,建表到使用jdbcTemplate去测试不同id的生成策略在大数据量的数据插入表现,然后分析了id的机制不同在mysql的索引结构以及优缺点,深入的解释了为何uuid和随机不重复id在数据插入中的性能损耗,详细的解释了这个问题。

在实际的开发中还是根据mysql的官方推荐最好使用自增id,mysql博大精深,内部还有很多值得优化的点需要我们学习。


 


 


 

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:mmqy2019@163.com进行举报,并提供相关证据,查实之后,将立刻删除涉嫌侵权内容。
上一篇:没有了
相关信息推荐
2020-09-01 17:47:21 关键词:MySQL数据库应用教程
摘要:MYSQL数据库查询的数次轮回是什么?很多人可能不太清楚数次轮回大概的意思,现在给大家普及一下:..
2020-09-01 17:33:51 关键词:MySQL数据库应用教程
摘要:半成品的MYSQL,有时候出现经常让人觉有点费解,如何去解决完善这个半成品呢?..
2020-09-01 16:15:46 关键词:MySQL数据库应用教程
摘要:MySQL数据库应用教程..