分库算法: $->{columnName%dbNum};
分表算法: $->{columnName/dbNum%tableNum};
数据分布情况测试:
40000条数据被均匀分布在10个库的500张表中:
测试代码:
package com.logbug;
public class Test {
public static void main(String[] args) {
int dbNum=10;
int tableNum=50;
int[][] dbs=new int[dbNum][tableNum];
for (int i = 0; i < tableNum*8000; i++) {
dbs[i%dbNum][i/dbNum%tableNum]++;
}
StringBuilder sb=new StringBuilder();
sb.append(" ");
for (int j = 0; j < tableNum; j++) {
sb.append("t").append(String.format("%02d",j)).append(" ");
}
sb.append("\n");
for (int i = 0; i < dbNum; i++) {
sb.append("db").append(i).append(": ");
for (int j = 0; j < tableNum; j++) {
sb.append(dbs[i][j]).append(" ");
}
sb.append("\n");
}
System.out.println(sb.toString());
}
}