MySQL使用ShardingSphere-JDBC實(shí)現(xiàn)數(shù)據(jù)分片
一、前言
當(dāng)單庫(kù)數(shù)據(jù)量達(dá)到千萬(wàn)級(jí)、單表達(dá)到百萬(wàn)級(jí)時(shí),性能會(huì)急劇下降,分庫(kù)分表是解決數(shù)據(jù)存儲(chǔ)瓶頸的核心方案。本文基于 ShardingSphere-JDBC,詳解垂直分庫(kù)、水平分庫(kù)、水平分表的實(shí)現(xiàn)方式。
二、分庫(kù)分表基礎(chǔ):CAP 定理與解決方案
- CAP 定理:分布式系統(tǒng)無(wú)法同時(shí)滿足一致性(C)、可用性(A)、分區(qū)容忍性(P),MySQL 分庫(kù)分表通常選擇:
- AP:保證可用性,犧牲強(qiáng)一致性(最終一致);
- CP:保證強(qiáng)一致性,犧牲可用性(適合金融場(chǎng)景);
- 主流解決方案:
- ShardingSphere-JDBC:輕量級(jí),嵌入應(yīng)用內(nèi),無(wú)中間件依賴;
- ShardingSphere-Proxy:獨(dú)立中間件,支持多語(yǔ)言;
- MyCat:老牌數(shù)據(jù)庫(kù)中間件。
三、ShardingSphere-JDBC 核心概念
- 分片鍵:用于分庫(kù) / 分表的字段(如 user_id、id);
- 分片算法:INLINE(行內(nèi)表達(dá)式)、MOD(取模)、HASH(哈希)等;
- 綁定表:關(guān)聯(lián)表(如 t_order 和 t_order_item),避免笛卡爾乘積,提升查詢效率;
- 廣播表:字典表(如 t_dict),同步到所有分片節(jié)點(diǎn),保證數(shù)據(jù)一致。
四、分庫(kù)分表實(shí)戰(zhàn)(YAML 配置)
1. 垂直分庫(kù)(按業(yè)務(wù)拆分)
將不同業(yè)務(wù)表拆分到不同數(shù)據(jù)庫(kù)(如用戶表→user_ds,訂單表→order_ds):
rules:
- !SHARDING
tables:
t_user:
actualDataNodes: user_ds.t_user # 用戶表在user_ds庫(kù)
t_order:
actualDataNodes: order_ds.t_order # 訂單表在order_ds庫(kù)2. 水平分庫(kù)(按分片鍵拆分到多個(gè)庫(kù))
將訂單表按 user_id 取模拆分到 order_ds_0 和 order_ds_1 兩個(gè)庫(kù):
rules:
- !SHARDING
tables:
t_user:
actualDataNodes: user_ds.t_user
t_order:
actualDataNodes: order_ds_${0..1}.t_order0 # 訂單表在2個(gè)庫(kù)的t_order0表
databaseStrategy:
standard:
shardingColumn: user_id # 分片鍵:user_id
shardingAlgorithmName: userid_inline # 分片算法
shardingAlgorithms:
userid_inline:
type: INLINE
props:
algorithm-expression: order_ds_${user_id % 2} # 取模拆分3. 水平分表(按分片鍵拆分到多個(gè)表)
將訂單表按 id 取模拆分到每個(gè)庫(kù)的 t_order0 和 t_order1 表:
rules:
- !SHARDING
tables:
t_user:
actualDataNodes: user_ds.t_user
t_order:
actualDataNodes: order_ds_${0..1}.t_order${0..1} # 2庫(kù)×2表=4個(gè)物理表
databaseStrategy:
standard:
shardingColumn: user_id
shardingAlgorithmName: userid_inline
tableStrategy:
standard:
shardingColumn: id # 分表鍵:id
shardingAlgorithmName: orderid_inline
shardingAlgorithms:
userid_inline:
type: INLINE
props:
algorithm-expression: order_ds_${user_id % 2}
orderid_inline:
type: INLINE
props:
algorithm-expression: t_order${id % 2} # 取模分表4. 關(guān)聯(lián)表(綁定表)+ 廣播表
rules:
- !SHARDING
tables:
t_user:
actualDataNodes: user_ds.t_user
t_order:
actualDataNodes: order_ds_${0..1}.t_order${0..1}
databaseStrategy:
standard:
shardingColumn: user_id
shardingAlgorithmName: userid_inline
tableStrategy:
standard:
shardingColumn: id
shardingAlgorithmName: orderid_inline
t_order_item: # 訂單項(xiàng)表(關(guān)聯(lián)表)
actualDataNodes: order_ds_${0..1}.t_order_item${0..1}
databaseStrategy:
standard:
shardingColumn: user_id
shardingAlgorithmName: userid_inline
tableStrategy:
standard:
shardingColumn: order_id
shardingAlgorithmName: orderid_item_inline
bindingTables: # 綁定表:t_order和t_order_item
- t_order,t_order_item
shardingAlgorithms:
userid_inline:
type: INLINE
props:
algorithm-expression: order_ds_${user_id % 2}
orderid_inline:
type: INLINE
props:
algorithm-expression: t_order${id % 2}
orderid_item_inline:
type: INLINE
props:
algorithm-expression: t_order_item${order_id % 2}
- !BROADCAST # 廣播表:t_dict(字典表)
tables:
- t_dict五、分庫(kù)分表注意事項(xiàng)
- 分片鍵選擇:優(yōu)先選查詢高頻字段(如 user_id),避免跨分片查詢;
- 避免跨庫(kù)事務(wù):盡量將同一事務(wù)的操作落在同一分片;
- 主鍵生成:使用雪花算法生成分布式 ID(64 位,19 位整數(shù)),避免主鍵沖突;
- 查詢優(yōu)化:
- 避免 SELECT *,使用覆蓋索引;
- 關(guān)聯(lián)查詢使用綁定表,減少笛卡爾乘積。
總結(jié)
- 垂直分庫(kù)按業(yè)務(wù)拆分,水平分庫(kù) / 分表按分片鍵拆分,解決單庫(kù) / 單表數(shù)據(jù)量過(guò)大問(wèn)題;
- ShardingSphere-JDBC 通過(guò) YAML 配置實(shí)現(xiàn)分片,INLINE 算法簡(jiǎn)單高效,適合大部分場(chǎng)景;
- 綁定表減少關(guān)聯(lián)查詢開(kāi)銷(xiāo),廣播表保證字典數(shù)據(jù)一致,分片鍵選擇是分庫(kù)分表的核心。
以上就是MySQL使用ShardingSphere-JDBC實(shí)現(xiàn)數(shù)據(jù)分片的詳細(xì)內(nèi)容,更多關(guān)于MySQL ShardingSphere-JDBC數(shù)據(jù)分片的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
MySQL慢查詢?nèi)罩局械腖ock_time由來(lái)解析
這篇文章主要為大家介紹了慢查詢?nèi)罩局蠰ock_time的由來(lái)解析,以及Lock_time?包含哪些鎖等待時(shí)間、以及是怎么計(jì)算得到的,有需要的朋友可以借鑒參考下,希望能夠有所幫助2023-06-06
MySQL 數(shù)據(jù)庫(kù) binLog 日志的使用操作
binlog是MySQL數(shù)據(jù)庫(kù)中的一種日志類(lèi)型,它記錄了數(shù)據(jù)庫(kù)中的所有更改操作,例如插入、更新、刪除操作,本文給大家介紹MySQL 數(shù)據(jù)庫(kù) binLog 日志的使用,感興趣的朋友一起看看吧2023-08-08
mysql?in索引慢查詢優(yōu)化實(shí)現(xiàn)步驟解析
這篇文章主要為大家介紹了mysql?in慢查詢優(yōu)化實(shí)現(xiàn)步驟的示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-05-05
MySQL中通過(guò)EXPLAIN如何分析SQL的執(zhí)行計(jì)劃詳解
這篇文章主要給大家介紹了關(guān)于MySQL中通過(guò)EXPLAIN如何分析SQL的執(zhí)行計(jì)劃的相關(guān)資料,文中通過(guò)圖文以及示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的安康學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2018-08-08
選擇MySQL數(shù)據(jù)庫(kù)進(jìn)行連接的簡(jiǎn)單示例
這篇文章主要介紹了選擇MySQL數(shù)據(jù)庫(kù)進(jìn)行連接的簡(jiǎn)單示例,是MySQL入門(mén)學(xué)習(xí)中的基礎(chǔ)知識(shí),需要的朋友可以參考下2015-05-05
Mysql調(diào)優(yōu)Explain工具詳解及實(shí)戰(zhàn)演練(推薦)
這篇文章主要介紹了Mysql調(diào)優(yōu)Explain工具詳解及實(shí)戰(zhàn)演練,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2021-03-03
MAC下MYSQL數(shù)據(jù)庫(kù)密碼忘記的解決辦法
這篇文章主要介紹了Mac操作系統(tǒng)下MYSQL數(shù)據(jù)庫(kù)密碼忘記的快速解決辦法,教大家重置MYSQ密碼,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-11-11

