用雪花 id 和 uuid 做 MySQL 主鍵,被領(lǐng)導(dǎo)懟了
點(diǎn)擊關(guān)注公眾號:互聯(lián)網(wǎng)架構(gòu)師,后臺回復(fù) 2T獲取2TB學(xué)習(xí)資源!
上一篇:2T架構(gòu)師學(xué)習(xí)資料干貨分享
MySQL和程序?qū)嵗?/span>
1、要說明這個問題,我們首先來建立三張表
2、光有理論不行,直接上程序,使用spring的jdbcTemplate來實(shí)現(xiàn)增查測試
技術(shù)框架:
springboot+jdbcTemplate+junit+hutool,
程序的原理就是連接自己的測試數(shù)據(jù)庫,然后在相同的環(huán)境下寫入同等數(shù)量的數(shù)據(jù),來分析一下insert插入的時間來進(jìn)行綜合其效率,為了做到最真實(shí)的效果,所有的數(shù)據(jù)采用隨機(jī)生成,比如名字、郵箱、地址都是隨機(jī)生成,程序已上傳自gitee,地址鏈接在文底。
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 MysqlDemoApplicationTests {@Autowiredprivate JdbcTemplateService jdbcTemplateService;@Autowiredprivate AutoKeyTableService autoKeyTableService;@Autowiredprivate UUIDKeyTableService uuidKeyTableService;@Autowiredprivate RandomKeyTableService randomKeyTableService;@Testvoid testDBTime() {StopWatch stopwatch = new StopWatch("執(zhí)行sql時間消耗");/*** auto_increment key任務(wù)*/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表任務(wù)開始");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表任務(wù)開始");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();/*** 隨機(jī)的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("隨機(jī)的long值key表任務(wù)開始");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("隨機(jī)key任務(wù)消耗時間:" + (end - start));stopwatch.stop();String result = stopwatch.prettyPrint();System.out.println(result);}}
3、程序?qū)懭虢Y(jié)果
4、效率測試結(jié)果
在已有數(shù)據(jù)量為130W的時候:我們再來測試一下插入10w數(shù)據(jù),看看會有什么結(jié)果:
使用uuid和自增id的索引結(jié)構(gòu)對比
1、使用自增id的內(nèi)部結(jié)構(gòu)
自增的主鍵的值是順序的,所以Innodb把每一條記錄都存儲在一條記錄的后面。當(dāng)達(dá)到頁面的最大填充因子時候(innodb默認(rèn)的最大填充因子是頁大小的15/16,會留出1/16的空間留作以后的 修改):
2、使用uuid的索引內(nèi)部結(jié)構(gòu)
因?yàn)閡uid相對順序的自增id來說是毫無規(guī)律可言的,新行的值不一定要比之前的主鍵的值要大,所以innodb無法做到總是把新行插入到索引的最后,而是需要為新行尋找新的合適的位置從而來分配新的空間。這個過程需要做很多額外的操作,數(shù)據(jù)的毫無順序會導(dǎo)致數(shù)據(jù)分布散亂,將會導(dǎo)致以下的問題:
結(jié)論:使用innodb應(yīng)該盡可能的按主鍵的自增順序插入,并且盡可能使用單調(diào)的增加的聚簇鍵的值來插入新行
3、使用自增id的缺點(diǎn)
那么使用自增的id就完全沒有壞處了嗎?并不是,自增id也會存在以下幾點(diǎn)問題:
①別人一旦爬取你的數(shù)據(jù)庫,就可以根據(jù)數(shù)據(jù)庫的自增id獲取到你的業(yè)務(wù)增長信息,很容易分析出你的經(jīng)營情況
②對于高并發(fā)的負(fù)載,innodb在按主鍵進(jìn)行插入的時候會造成明顯的鎖爭用,主鍵的上界會成為爭搶的熱點(diǎn),因?yàn)樗械牟迦攵及l(fā)生在這里,并發(fā)插入會導(dǎo)致間隙鎖競爭
③Auto_Increment鎖機(jī)制會造成自增鎖的搶奪,有一定的性能損失
附:
Auto_increment的鎖爭搶問題,如果要改善需要調(diào)優(yōu)innodb_autoinc_lock_mode的配置
總結(jié)
正文結(jié)束
推薦閱讀 ↓↓↓
正文結(jié)束
