栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

Mysql分库分表的计算公式

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Mysql分库分表的计算公式

一直在说mysql数据大了要分库分表,但是一直都不知道分库分表如何计算表和库。

作用

分表:降低单个表数据量太大带来性能的损失
分库:降低单个数据库机器带来性能的损失
分库分表:多个机器、多个表实现性能的提升

常用公式

前提是库数量 * 每个库的表数量的总数不变

midValue = hash(分库分表的值,例如订单ID) % (库数量 * 每个库的表数量)
库序号 = midValue / 每个库的表数量
表序号 = midValue % 每个库的表数量
简单计算
package com.tom.tom.mysql;

import java.util.function.Function;
import java.util.function.UnaryOperator;

public class MysqlDBTable {
    public static void main(String[] args) {
        UnaryOperator myHash = UnaryOperator.identity();
        System.out.println("现在1个库,每个库256张表");
        int dbNum = 1;
        int tableNumInDb = 256;

        int orderId = 1000;
        System.out.println(orderId + " db    index: " + getDbIndex(myHash, orderId, dbNum, tableNumInDb));
        System.out.println(orderId + " table index: " + getTableIndex(myHash, orderId, dbNum, tableNumInDb));
        orderId = 1100;
        System.out.println(orderId + " db    index: " + getDbIndex(myHash, orderId, dbNum, tableNumInDb));
        System.out.println(orderId + " table index: " + getTableIndex(myHash, orderId, dbNum, tableNumInDb));
        orderId = 128 + 256 * 23;
        System.out.println(orderId + " db    index: " + getDbIndex(myHash, orderId, dbNum, tableNumInDb));
        System.out.println(orderId + " table index: " + getTableIndex(myHash, orderId, dbNum, tableNumInDb));

        System.out.println("现在变成两个库,每个库128张表");
        dbNum = 2;
        tableNumInDb = 128;
        orderId = 1000;
        System.out.println(orderId + " db    index: " + getDbIndex(myHash, orderId, dbNum, tableNumInDb));
        System.out.println(orderId + " table index: " + getTableIndex(myHash, orderId, dbNum, tableNumInDb));
        orderId = 1100;
        System.out.println(orderId + " db    index: " + getDbIndex(myHash, orderId, dbNum, tableNumInDb));
        System.out.println(orderId + " table index: " + getTableIndex(myHash, orderId, dbNum, tableNumInDb));
        orderId = 128 + 256 * 23;
        System.out.println(orderId + " db    index: " + getDbIndex(myHash, orderId, dbNum, tableNumInDb));
        System.out.println(orderId + " table index: " + getTableIndex(myHash, orderId, dbNum, tableNumInDb));

    }

    public static  int getDbIndex(Function hash, R r, int dbNum, int tableNumInDb) {
        int mid = hash.apply(r) % (dbNum * tableNumInDb);
        return mid / tableNumInDb;
    }

    public static  int getTableIndex(Function hash, R r, int dbNum, int tableNumInDb) {
        int mid = hash.apply(r) % (dbNum * tableNumInDb);
        return mid % tableNumInDb;
    }
}
现在1个库,每个库256张表
1000 db    index: 0
1000 table index: 232
1100 db    index: 0
1100 table index: 76
6016 db    index: 0
6016 table index: 128
现在变成两个库,每个库128张表
1000 db    index: 1
1000 table index: 104
1100 db    index: 0
1100 table index: 76
6016 db    index: 1
6016 table index: 0

可以看到在总的表数量不变的情况下,只迁移一半的表数据就可以了。
当数据库从2个变成4个的情况下,需要把第二个数据库中的表迁移到新的第三个和第4个表中。把第一个数据库中的表迁移一半到第二个表中。

现在1个库,每个库256张表
1000 db    index: 0
1000 table index: 232
1100 db    index: 0
1100 table index: 76
6016 db    index: 0
6016 table index: 128
现在变成两个库,每个库128张表
1000 db    index: 1
1000 table index: 104
1100 db    index: 0
1100 table index: 76
6016 db    index: 1
6016 table index: 0
现在变成4个库,每个库64张表
1000 db    index: 3
1000 table index: 40
1100 db    index: 1
1100 table index: 12
6016 db    index: 2
6016 table index: 0
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/711290.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号