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

