自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

為啥不能用uuid做MySQL的主鍵?

數(shù)據(jù)庫 MySQL
在 MySQL 中設(shè)計表的時候,MySQL 官方推薦不要使用 uuid 或者不連續(xù)不重復(fù)的雪花 id(long 形且唯一,單機遞增),而是推薦連續(xù)自增的主鍵 id,官方的推薦是 auto_increment。

 在 MySQL 中設(shè)計表的時候,MySQL 官方推薦不要使用 uuid 或者不連續(xù)不重復(fù)的雪花 id(long 形且唯一,單機遞增),而是推薦連續(xù)自增的主鍵 id,官方的推薦是 auto_increment。

[[341150]]
圖片來自 Pexels

 

那么為什么不建議采用 uuid,使用 uuid 究竟有什么壞處?本問我們從以下幾個部分來分析這個問題,探討一下內(nèi)部的原因:

  • MySQL 程序?qū)嵗?/strong>
  • 使用 uuid 和自增 id 的索引結(jié)構(gòu)對比
  • 總結(jié)

MySQL 程序?qū)嵗?/span>

要說明這個問題,我們首先來建立三張表,分別是:

  • user_auto_key
  • user_uuid
  • user_random_key

他們分別表示自動增長的主鍵,uuid 作為主鍵,隨機 key 作為主鍵,其他我們完全保持不變。

根據(jù)控制變量法,我們只把每個表的主鍵使用不同的策略生成,而其他的字段完全一樣,然后測試一下表的插入速度和查詢速度。

注:這里的隨機 key 其實是指用雪花算法算出來的前后不連續(xù)不重復(fù)無規(guī)律的id:一串 18 位長度的 long 值。

id 自動生成表:

 

用戶 uuid 表:

 

隨機主鍵表:

 

光有理論不行,直接上程序,使用 Spring 的 jdbcTemplate 來實現(xiàn)增查測試。

技術(shù)框架:Spring Boot+jdbcTemplate+junit+hutool,程序的原理就是連接自己的測試數(shù)據(jù)庫,然后在相同的環(huán)境下寫入同等數(shù)量的數(shù)據(jù),來分析一下 insert 插入的時間來進行綜合其效率。

為了做到最真實的效果,所有的數(shù)據(jù)采用隨機生成,比如名字、郵箱、地址都是隨機生成:

  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.  
  16. @SpringBootTest 
  17. class MysqlDemoApplicationTests { 
  18.  
  19.     @Autowired 
  20.     private JdbcTemplateService jdbcTemplateService; 
  21.  
  22.     @Autowired 
  23.     private AutoKeyTableService autoKeyTableService; 
  24.  
  25.     @Autowired 
  26.     private UUIDKeyTableService uuidKeyTableService; 
  27.  
  28.     @Autowired 
  29.     private RandomKeyTableService randomKeyTableService; 
  30.  
  31.  
  32.     @Test 
  33.     void testDBTime() { 
  34.  
  35.         StopWatch stopwatch = new StopWatch("執(zhí)行sql時間消耗"); 
  36.  
  37.  
  38.         /** 
  39.          * auto_increment key任務(wù) 
  40.          */ 
  41.         final String insertSql = "INSERT INTO user_key_auto(user_id,user_name,sex,address,city,email,state) VALUES(?,?,?,?,?,?,?)"
  42.  
  43.         List<UserKeyAuto> insertData = autoKeyTableService.getInsertData(); 
  44.         stopwatch.start("自動生成key表任務(wù)開始"); 
  45.         long start1 = System.currentTimeMillis(); 
  46.         if (CollectionUtil.isNotEmpty(insertData)) { 
  47.             boolean insertResult = jdbcTemplateService.insert(insertSql, insertData, false); 
  48.             System.out.println(insertResult); 
  49.         } 
  50.         long end1 = System.currentTimeMillis(); 
  51.         System.out.println("auto key消耗的時間:" + (end1 - start1)); 
  52.  
  53.         stopwatch.stop(); 
  54.  
  55.  
  56.         /** 
  57.          * uudID的key 
  58.          */ 
  59.         final String insertSql2 = "INSERT INTO user_uuid(id,user_id,user_name,sex,address,city,email,state) VALUES(?,?,?,?,?,?,?,?)"
  60.  
  61.         List<UserKeyUUID> insertData2 = uuidKeyTableService.getInsertData(); 
  62.         stopwatch.start("UUID的key表任務(wù)開始"); 
  63.         long begin = System.currentTimeMillis(); 
  64.         if (CollectionUtil.isNotEmpty(insertData)) { 
  65.             boolean insertResult = jdbcTemplateService.insert(insertSql2, insertData2, true); 
  66.             System.out.println(insertResult); 
  67.         } 
  68.         long over = System.currentTimeMillis(); 
  69.         System.out.println("UUID key消耗的時間:" + (over - begin)); 
  70.  
  71.         stopwatch.stop(); 
  72.  
  73.  
  74.         /** 
  75.          * 隨機的long值key 
  76.          */ 
  77.         final String insertSql3 = "INSERT INTO user_random_key(id,user_id,user_name,sex,address,city,email,state) VALUES(?,?,?,?,?,?,?,?)"
  78.         List<UserKeyRandom> insertData3 = randomKeyTableService.getInsertData(); 
  79.         stopwatch.start("隨機的long值key表任務(wù)開始"); 
  80.         Long start = System.currentTimeMillis(); 
  81.         if (CollectionUtil.isNotEmpty(insertData)) { 
  82.             boolean insertResult = jdbcTemplateService.insert(insertSql3, insertData3, true); 
  83.             System.out.println(insertResult); 
  84.         } 
  85.         Long end = System.currentTimeMillis(); 
  86.         System.out.println("隨機key任務(wù)消耗時間:" + (end - start)); 
  87.         stopwatch.stop(); 
  88.  
  89.  
  90.         String result = stopwatch.prettyPrint(); 
  91.         System.out.println(result); 
  92.     } 

程序?qū)懭虢Y(jié)果

user_key_auto 寫入結(jié)果:

 

user_random_key 寫入結(jié)果:

 

user_uuid 表寫入結(jié)果:

 

效率測試結(jié)果

 

在已有數(shù)據(jù)量為 130W 的時候:我們再來測試一下插入 10w 數(shù)據(jù),看看會有什么結(jié)果:

 

可以看出在數(shù)據(jù)量 100W 左右的時候,uuid 的插入效率墊底,并且在后序增加了 130W 的數(shù)據(jù),uuid 的時間又直線下降。

時間占用量總體可以打出的效率排名為:auto_key>random_key>uuid。

uuid 的效率最低,在數(shù)據(jù)量較大的情況下,效率直線下滑。那么為什么會出現(xiàn)這樣的現(xiàn)象呢?帶著疑問,我們來探討一下這個問題:

使用 uuid 和自增 id 的索引結(jié)構(gòu)對比

使用自增 id 的內(nèi)部結(jié)構(gòu)

 

自增的主鍵的值是順序的,所以 InnoDB 把每一條記錄都存儲在一條記錄的后面。

當(dāng)達到頁面的最大填充因子時候(InnoDB 默認的最大填充因子是頁大小的 15/16,會留出 1/16 的空間留作以后的修改)。

①下一條記錄就會寫入新的頁中,一旦數(shù)據(jù)按照這種順序的方式加載,主鍵頁就會近乎于順序的記錄填滿,提升了頁面的最大填充率,不會有頁的浪費。

②新插入的行一定會在原有的最大數(shù)據(jù)行下一行,MySQL 定位和尋址很快,不會為計算新行的位置而做出額外的消耗。

③減少了頁分裂和碎片的產(chǎn)生。

使用 uuid 的索引內(nèi)部結(jié)構(gòu)

 

因為 uuid 相對順序的自增 id 來說是毫無規(guī)律可言的,新行的值不一定要比之前的主鍵的值要大,所以 innodb 無法做到總是把新行插入到索引的最后,而是需要為新行尋找新的合適的位置從而來分配新的空間。

這個過程需要做很多額外的操作,數(shù)據(jù)的毫無順序會導(dǎo)致數(shù)據(jù)分布散亂,將會導(dǎo)致以下的問題:

①寫入的目標頁很可能已經(jīng)刷新到磁盤上并且從緩存上移除,或者還沒有被加載到緩存中,innodb 在插入之前不得不先找到并從磁盤讀取目標頁到內(nèi)存中,這將導(dǎo)致大量的隨機 IO。

②因為寫入是亂序的,innodb 不得不頻繁的做頁分裂操作,以便為新的行分配空間,頁分裂導(dǎo)致移動大量的數(shù)據(jù),一次插入最少需要修改三個頁以上。

③由于頻繁的頁分裂,頁會變得稀疏并被不規(guī)則的填充,最終會導(dǎo)致數(shù)據(jù)會有碎片。

在把隨機值(uuid 和雪花 id)載入到聚簇索引(InnoDB 默認的索引類型)以后,有時候會需要做一次 OPTIMEIZE TABLE 來重建表并優(yōu)化頁的填充,這將又需要一定的時間消耗。

結(jié)論:使用 InnoDB 應(yīng)該盡可能的按主鍵的自增順序插入,并且盡可能使用單調(diào)的增加的聚簇鍵的值來插入新行。

使用自增 id 的缺點

那么使用自增的 id 就完全沒有壞處了嗎?并不是,自增 id 也會存在以下幾點問題:

①別人一旦爬取你的數(shù)據(jù)庫,就可以根據(jù)數(shù)據(jù)庫的自增 id 獲取到你的業(yè)務(wù)增長信息,很容易分析出你的經(jīng)營情況。

②對于高并發(fā)的負載,InnoDB 在按主鍵進行插入的時候會造成明顯的鎖爭用,主鍵的上界會成為爭搶的熱點,因為所有的插入都發(fā)生在這里,并發(fā)插入會導(dǎo)致間隙鎖競爭。

③Auto_Increment 鎖機制會造成自增鎖的搶奪,有一定的性能損失。

附:Auto_increment的鎖爭搶問題,如果要改善需要調(diào)優(yōu) innodb_autoinc_lock_mode 的配置。

總結(jié)

本篇博客首先從開篇的提出問題,建表到使用 jdbcTemplate 去測試不同 id 的生成策略在大數(shù)據(jù)量的數(shù)據(jù)插入表現(xiàn),然后分析了 id 的機制不同在 MySQL 的索引結(jié)構(gòu)以及優(yōu)缺點,深入的解釋了為何 uuid 和隨機不重復(fù) id 在數(shù)據(jù)插入中的性能損耗,詳細的解釋了這個問題。

在實際的開發(fā)中還是根據(jù) MySQL 的官方推薦最好使用自增 id,MySQL 博大精深,內(nèi)部還有很多值得優(yōu)化的點需要我們學(xué)習(xí)。

作者:Yrion

編輯:陶家龍

出處:cnblogs.com/wyq178/p/12548864.html

 

責(zé)任編輯:武曉燕 來源: 博客園
相關(guān)推薦

2018-06-04 15:17:10

編程語言中文編程

2023-08-28 12:07:06

UUIDMySQL

2020-08-31 11:20:53

MySQLuuidid

2010-10-15 10:52:04

跳槽

2024-10-24 09:22:30

2010-10-11 11:25:26

MySQL主鍵

2022-11-14 08:12:34

2011-05-06 15:56:38

打印機故障

2015-04-02 11:25:39

2016-10-20 08:57:17

網(wǎng)頁ChromeIE

2023-06-07 08:00:00

MySQL批量插入

2021-05-20 10:11:51

固態(tài)硬盤碎片系統(tǒng)

2010-10-11 11:46:20

MySQL主鍵

2023-09-14 13:23:42

Llama-2模型參數(shù)

2010-10-08 11:52:29

2022-08-03 08:10:43

零信任網(wǎng)絡(luò)安全防御體系

2012-11-12 10:15:48

ATM安全安全認證手勢技術(shù)

2021-03-15 08:24:32

Windows 10Windows微軟

2017-08-07 10:17:06

2020-02-07 19:24:47

APP權(quán)限移動應(yīng)用
點贊
收藏

51CTO技術(shù)棧公眾號