為啥要分庫分表
業(yè)務隨著變化,表的內容變得越來越多,一個表里面的數(shù)據(jù)會日積月累的增加,而且之前的數(shù)據(jù)很少在看了,并降低了查詢的效率。業(yè)務只關注前幾個月的數(shù)據(jù),統(tǒng)計數(shù)據(jù),所以很多數(shù)據(jù)現(xiàn)代沒有用了,所以可以在后期再去定時生成匯總。目前要分表分庫只有一個業(yè)務庫,工單工作量,把每天的項目工作情況進行分析下來,并記錄下來,也就是日工作量。出現(xiàn)了大量無用項目信息工單信息。
已有數(shù)據(jù)表結構
已經(jīng)跑了幾個月的數(shù)據(jù),表突然涌現(xiàn)出一天千萬數(shù)據(jù),原來業(yè)務數(shù)據(jù)上來了,一些不看好的項目開始錄入系統(tǒng)了,系統(tǒng)突然卡死了,被大屏系統(tǒng)每5秒就拉去數(shù)據(jù),把系統(tǒng)搞垮了,查詢一下子慢了30秒。分析原因,找到事故接口。
開始操作了
大屏里面的數(shù)據(jù)進行查詢,你頻繁我就加緩存,數(shù)據(jù)來的并不快,可是他們查詢的快,導致無效查詢次數(shù)太多,這樣我加入緩存,減少了數(shù)據(jù)庫查詢的次數(shù)。
統(tǒng)計和業(yè)務功能界面,我添加了日期查詢的策略,先看到是一周的日期,進行日期添加索引,原來的20秒,瞬間回到1秒左右。
總算扛過了。但是業(yè)務還在漲啊,數(shù)據(jù)還在增加啊。
我提出,可以把已完成的項目數(shù)據(jù)進行歸檔處理可以嗎?業(yè)務部門不樂意,他們覺得現(xiàn)在查詢就符合他們的業(yè)務情況。
我的單表讓我如何撐住。
引入ShardingSphere 將它原來不變,進行季度分表處理吧,當這個庫10個的時候在分表,這個還有點時間,先把季度庫創(chuàng)建,當然表結構一樣。
為啥用它ShardingSphere ,就是覺得它基本上業(yè)務代碼和庫表都不用改,只改配置即可。
ShardingSphere 很坑
1、demo example基本上不全。
2、官網(wǎng) https://shardingsphere.apache.org/document/current/cn/quick-start/shardingsphere-jdbc-quick-start/ 簡單的如沒有寫一樣。
3、版本坑,我使用4x 還是5x 呢? 有啥區(qū)別呢?
4、我用的還是國產(chǎn)數(shù)據(jù)庫,是否支持。
5、我使用的dynamic 已經(jīng)查詢了好多庫了,是否還能支持。讓我感覺道路一片空白。
ShardingSphere 真好用
使用ShardingSphere-JDBC數(shù)據(jù)分片功能即可。其他 ShardingSphere-Proxy 它支持性不好,數(shù)據(jù)庫基本不用想了放棄。
使用 ShardingSphere-JDBC 先正常的 配置pom
<dependency>
<groupId>org.apache.shardingsphere</groupId>
<artifactId>shardingsphere-jdbc</artifactId>
<version>${latest.release.version}</version>
<exclusions>
<exclusion>
<groupId>org.yaml</groupId>
<artifactId>snakeyaml</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.yaml</groupId>
<artifactId>snakeyaml</artifactId>
<version>1.33</version>
</dependency>
yml 里面配置
datasource:
dynamic:
datasource:
master: ##原來的
url: ‘’
driver-class-name: com.microsoft.sqlserver.jdbc.SQLServerDriver
username:
password:
master2: ## 新增的
driver-class-name: org.apache.shardingsphere.driver.ShardingSphereDriver
url: jdbc:shardingsphere:classpath:shading-auto-tables-algorithm.yaml
shading-auto-tables-algorithm.yaml的添加即可
# JDBC 邏輯庫名稱。在集群模式中,使用該參數(shù)來聯(lián)通 ShardingSphere-JDBC 與 ShardingSphere-Proxy。
# 默認值:logic_db
databaseName (?):
mode:
dataSources:
rules:
- !FOO_XXX
...
- !BAR_XXX
...
props:
key_1: value_1
key_2: value_2
這個就是官網(wǎng)給的,我反增思考了半天
我不把我的yaml 貼出來了,但是把坑讀者也看不到,我找到一個很全的,分庫還分表的情況了,還有多表進行同步并讀寫分離的情況都分析到了,比我的好多了。
dataSources:
ds0:
dataSourceClassName: com.zaxxer.hikari.HikariDataSource
driverClassName: com.mysql.cj.jdbc.Driver
jdbcUrl: jdbc:mysql://127.0.0.1:3306/ds_0?useUnicode=true&characterEncoding=utf-8&useSSL=false&serverTimezone=GMT%2B8&useTimezone=true
username: root
password: ilxw
ds1:
dataSourceClassName: com.zaxxer.hikari.HikariDataSource
driverClassName: com.mysql.cj.jdbc.Driver
jdbcUrl: jdbc:mysql://127.0.0.1:3306/ds_1?useUnicode=true&characterEncoding=utf-8&useSSL=false&serverTimezone=GMT%2B8&useTimezone=true
username: root
password: ilxw
ds2:
dataSourceClassName: com.zaxxer.hikari.HikariDataSource
driverClassName: com.mysql.cj.jdbc.Driver
jdbcUrl: jdbc:mysql://127.0.0.1:3306/ds_2?useUnicode=true&characterEncoding=utf-8&useSSL=false&serverTimezone=GMT%2B8&useTimezone=true
username: root
password: ilxw
ds3:
dataSourceClassName: com.zaxxer.hikari.HikariDataSource
driverClassName: com.mysql.cj.jdbc.Driver
jdbcUrl: jdbc:mysql://127.0.0.1:3306/ds_3?useUnicode=true&characterEncoding=utf-8&useSSL=false&serverTimezone=GMT%2B8&useTimezone=true
username: root
password: ilxw
rules:
- !SHARDING
tables:
# 訂單表基礎表
t_ent_order:
# 真實表
actualDataNodes: ds$->{0..3}.t_ent_order
# 分庫策略
databaseStrategy:
complex:
shardingColumns: id,ent_id
shardingAlgorithmName: hash-slot-algorithm
# 訂單詳情表
t_ent_order_detail:
actualDataNodes: ds$->{0..3}.t_ent_order_detail
# 分庫策略
databaseStrategy:
complex:
shardingColumns: id,ent_id
shardingAlgorithmName: hash-slot-algorithm
# 訂單條目表
t_ent_order_item:
actualDataNodes: ds$->{0..3}.t_ent_order_item_$->{0..7}
# 分庫策略
databaseStrategy:
complex:
shardingColumns: id,ent_id
shardingAlgorithmName: hash-slot-algorithm
# 分表策略
tableStrategy:
complex:
shardingColumns: id,ent_id
shardingAlgorithmName: hash-slot-table-algorithm
# 綁定表
bindingTables:
- t_ent_order,t_ent_order_detail
# 分片算法
shardingAlgorithms:
hash-slot-algorithm:
type: CLASS_BASED
props:
strategy: complex
algorithmClassName: cn.javayong.shardingjdbc5.spring.common.sharding.HashSlotAlgorithm
hash-slot-table-algorithm:
type: CLASS_BASED
props:
strategy: complex
algorithmClassName: cn.javayong.shardingjdbc5.spring.common.sharding.HashSlotAlgorithm
# 非默認值,直接對分片數(shù)取余
directIndex: true
# 廣播
- !BROADCAST
tables:
- t_city
props:
# 日志顯示 SQL
sql-show: true
我只需要改的地方還有一個在哪個方法的service里面添加@DS("master2") 即可。啟動失敗問題收集
1、找不到表提示不存在表 配置
actualDataNodes: ds$->{0..3}.t_ent_order_detail
這個參數(shù)錯誤。
2、HashSlotAlgorithm 這個算法
如果沒有什么特殊表,基本初始化即可。
3、版本和spring 不適應怎么辦。那就從低到高測試一遍,基本不是代碼問題,都是版本問題。
4、druid 的版本問題。只有升級升級。
其他
基本穩(wěn)定運行了,按照時間查詢解決我一半的問題。
actualDataNodes 有個問題,我只能創(chuàng)建一年的四個表結構,如何我讓程序生成表結構,插入數(shù)據(jù)沒有問題,就是查詢不出來,就得手動添加actualDataNodes 里面的表結構重啟程序就可以了,好難受啊。
當然這個框架好牛的,影子庫提供的影子算法,然后測試了好多次都沒有成功。學習還在路上。加油各位。