shardingsphere-jdbc 水平分表學習記錄( 三 )


分表分庫的規則思考最開始的時候對于分庫分表無腦兩個都用了MOD, 但因為分區數和分表數是一樣的(都是2).所以mod 2數據的分布也是一樣的,這就導致了sharding_test_0user_1是沒有數據的,sharding_test_1user_0也是沒有數據的.分表了個寂寞.
不只是一樣,其實只要分庫和分表數最大公約數不為1如果無腦MOD都會有傾斜的問題.可以代碼驗證下:
int dbShard = 6;int tableShard = 32;Map<Tuple2<Integer, Integer>, Integer> count = new TreeMap<>();for (int i = 0; i < dbShard; i++) {for (int j = 0; j < tableShard; j++) {count.put(Tuple.tuple(i, j), 0);}}for (int i = 0; i < 100000; i++) {count.computeIfPresent(Tuple.tuple(i % dbShard, i % tableShard), (k, v) -> v + 1);}count.forEach((k,v) -> {System.out.println(k + ":" + v);});因為前司我經手的項目用的都是分表,還沒有到分庫,沒有意識到這個問題,也算是一點點小經驗吧,要考慮下分庫分表的規則組合會不會導致數據傾斜.
其他還有些實踐中的問題,當時沒有記錄把配置整對之后也不知道怎么復現了.不得不說shardingsphere-jdbc的易用性是非常高了,通俗易懂.
參考shardingsphere官網: https://shardingsphere.apache.orgshardingsphere-jdbc配置: https://shardingsphere.apache.org/document/current/cn/user-manual/shardingsphere-jdbc/yaml-config/shardingsphere FAQ: https://shardingsphere.apache.org/document/current/cn/faq/How to get generated ID after I inserted into a new data record in database using Spring JDBCTemplate?
github page的博客原文:https://bingowith.me/2022/11/05/shardingsphere-jdbc-learn-note/

推薦閱讀