分库分表简单介绍

分库分表是一种数据库管理方法,它的目的是提高数据库的性能、扩展性和可用性,无论是分库还是分表,都是从水平与垂直两个维度进行切分。

shardingsphere 组件

Apache ShardingSphere

1.引入maven依赖

XML 复制代码
<dependency>
            <groupId>org.apache.shardingsphere</groupId>
            <artifactId>sharding-jdbc-spring-boot-starter</artifactId>
            <version>4.0.0-RC1</version>
</dependency>

2.没有用垂直,只有在业务上垂直

只用到了水平,水平拆表

cls=provide,order,user,

=order.orderMaster${0...9}

配置 针对规则配置的模式

单机模式(默认)内存模式 集群模式

配置项说明:

在application.properties中配置选项

XML 复制代码
#此配置将默认后面的bean覆盖前面的同名bean,否则会报错
spring.main.allow-bean-definition-overriding=true

#sharding-jdbc配置
##配置数据源名称
spring.shardingsphere.datasource.names=order

##配置数据源g1具体内容
spring.shardingsphere.datasource.order.type=com.alibaba.druid.pool.DruidDataSource
spring.shardingsphere.datasource.order.driver-class-name=com.mysql.cj.jdbc.Driver
spring.shardingsphere.datasource.order.url=jdbc:mysql://192.168.14.2:3306/goods_db1 useUnicode=true&characterEncoding=utf8&useSSL=false&serverTimezone=UTC
spring.shardingsphere.datasource.order.username=root
spring.shardingsphere.datasource.order.password=123456

##配置公共表
spring.shardingsphere.sharding.broadcast-tables=dict
##配置公共表的主键及生成策略
spring.shardingsphere.sharding.tables.dict.key-generator.column=id
spring.shardingsphere.sharding.tables.dict.key-generator.type=SNOWFLAKE



##配置order_master数据库分布以及表分布
spring.shardingsphere.sharding.tables.order_master.actual-data-nodes=order.order_master_${0..9}


spring.shardingsphere.sharding.tables.order_master.table-strategy.inline.sharding-column=id
spring.shardingsphere.sharding.tables.order_master.table-strategy.inline.algorithm-expression=order_master_${id%10}

##配置order_item数据库分布以及表分布
spring.shardingsphere.sharding.tables.order_item.actual-data-nodes=order.order_item_${0..9}


spring.shardingsphere.sharding.tables.order_item.table-strategy.inline.sharding-column=orderId
spring.shardingsphere.sharding.tables.order_item.table-strategy.inline.algorithm-expression=order_item_${orderId%10}




##打印sql
spring.shardingsphere.props.sql.show=true

写model和mapper

java 复制代码
@Data
public class Goods {
    private Long id;
    private String name;
    private Long uid;
    private String status;
}

@Data
public class Dict {
    private Long id;
    private String name;
    private String status;
}

@Mapper
public interface GoodsMapper extends BaseMapper<Goods> {
}

@Mapper
public interface DictMapper extends BaseMapper<Dict> {
}

测试方法

java 复制代码
@Test
    void addGoods(){
        for (int i = 0; i < 10; i++) {
            Goods goods = new Goods();
            goods.setName("商品" + i);
            goods.setUid(RandomUtil.randomLong(1000L, 9999L));
            goods.setStatus("备注 " + i);
            goodsMapper.insert(goods);
        }
    }

    @Test
    void getGoods(){
        QueryWrapper<Goods> wrapper = new QueryWrapper<>();
//        wrapper.eq("id", "1714939259473723393");
        Goods goods = goodsMapper.selectById(1716273265985294340L);

        System.out.println(goods.toString());
    }

	@Test
	void addDict(){
        for (int i = 0; i < 10; i++) {
            Dict dict = new Dict();
            dict.setName("字典" + i);
            dict.setStatus("备注 " + i);
            dictMapper.insert(dict);
        }
    }

    @Test
    void deleteDict(){
        QueryWrapper<Dict> wrapper = new QueryWrapper<>();
        wrapper.eq("id", "1716276779037872130");
        dictMapper.delete(wrapper);
    }

3.根据id ={id%10}

加上用户的后一位

4.order.orderItem${0...9}

=orderId

={orderId%10}

5.能不分就不分,根据业务走,

根据规则走

相关推荐
洛_尘1 分钟前
MySQL 5:增删改查操作
数据库·mysql
老邓计算机毕设12 分钟前
SSM养老院老人健康信息管理系统t4p4x(程序+源码+数据库+调试部署+开发环境)带论文文档1万字以上,文末可获取,系统界面在最后面
数据库·计算机毕业设计·ssm 框架·养老院老人健康管理系统
AC赳赳老秦14 分钟前
跨境科技服务的基石:DeepSeek赋能多语言技术文档与合规性说明的深度实践
android·大数据·数据库·人工智能·科技·deepseek·跨境
理智的煎蛋22 分钟前
达梦数据库全流程操作指南
数据库·oracle
FreeBuf_31 分钟前
欧盟漏洞数据库正式上线,采用去中心化模式运营
数据库·去中心化·区块链
东方轧线34 分钟前
给 AI 安装高速缓存:实战 MCP 对接 Redis,实现热点数据的毫秒级读取与状态共享
数据库·人工智能·redis
2401_832298101 小时前
腾讯云TSearch存算分离,破解日志分析算力瓶颈
大数据·运维·数据库
Leo July1 小时前
【MySQL】MySQL数据库调优实战指南:从基础优化到架构升级
数据库·mysql·架构
l1t1 小时前
DeepSeek总结的在单节点上处理 1TB Parquet 数据方法
数据库·人工智能·duckdb
么么...1 小时前
系统性 MySQL 优化:性能分析、索引设计与失效场景全解
数据库·经验分享·sql·mysql