import?com.wyq.mysqldemo.diffkeytest.RandomKeyTableService;br/>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://s2.51cto.com/images/20210829/1630170483797771.jpg)

user_random_key写入结果:

![](https://s2.51cto.com/images/20210829/1630170483750726.jpg)

user_uuid表写入结果:

![](https://s2.51cto.com/images/20210829/1630170484712342.jpg)

## 1.4.效率测试结果

![](https://s2.51cto.com/images/20210829/1630170484767429.jpg)

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

![](https://s2.51cto.com/images/20210829/1630170484165875.jpg)

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

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

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

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

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

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

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

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