ShardingJDBC实现MySql分库分表

ShardingJDBC作用

作用1、对主从数据库实现数据读写分离
作用2、分库分表
分库场景:并发量高的情况
分表场景:表的数据量太多的情况,数据量达到多少就需要考虑呢?我的答案是500万~2000万

看一下ChatGPT怎么回答:mysql数据量达到多少需要考虑分表
以下是它的回答:

在什么时候开始考虑对MySQL表进行分片(也称为水平分区),并没有具体的数字。切分表的决定应该基于应用程序的特定需求和特性。
以下是决定是否拆分表时需要考虑的一些因素:
1、数据量:如果您的表越来越大,那么管理和维护可能会变得更加困难。共享可以帮助您横向扩展并将数据分布在多个服务器上,使其更易于管理。

2、查询性能:如果您有一个具有大量查询的表,分片可以帮助分配负载并提高性能。

3、数据访问模式:如果应用程序以不均匀分布的方式访问数据(例如,某些行比其他行流行得多),分片可以帮助平衡负载并提高性能。

4、硬件和基础设施:如果您的硬件资源有限或硬件预算有限,分片可以帮助您横向扩展并在多个服务器之间分配负载。

最终,切分表的决定应该基于应用程序的特定需求和可用资源。通常,监控应用程序的性能和可扩展性需求并根据需要决定分片是一个好主意。

依赖

<dependency>
   <groupId>org.apache.shardingsphere</groupId>
    <artifactId>shardingsphere-jdbc-core-spring-boot-starter</artifactId>
    <version>5.1.0</version>
</dependency>

配置

application.properties

#配置多个数据源
spring.shardingsphere.datasource.names=m1,m2,m3
#数据源1
spring.shardingsphere.datasource.m1.type=com.alibaba.druid.pool.DruidDataSource
spring.shardingsphere.datasource.m1.driver-class-name=com.mysql.cj.jdbc.Driver
spring.shardingsphere.datasource.m1.url=jdbc:mysql://115.29.197.4:3306/employee?serverTimezone=GMT%2B8
spring.shardingsphere.datasource.m1.username=root
spring.shardingsphere.datasource.m1.password=root
#数据源2
spring.shardingsphere.datasource.m2.type=com.alibaba.druid.pool.DruidDataSource
spring.shardingsphere.datasource.m2.driver-class-name=com.mysql.cj.jdbc.Driver
spring.shardingsphere.datasource.m2.url=jdbc:mysql://115.29.197.5:3306/employee?serverTimezone=GMT%2B8
spring.shardingsphere.datasource.m2.username=root
spring.shardingsphere.datasource.m2.password=root
#数据源3
spring.shardingsphere.datasource.m3.type=com.alibaba.druid.pool.DruidDataSource
spring.shardingsphere.datasource.m3.driver-class-name=com.mysql.cj.jdbc.Driver
spring.shardingsphere.datasource.m3.url=jdbc:mysql://115.29.197.6:3306/employee?serverTimezone=GMT%2B8
spring.shardingsphere.datasource.m3.username=root
spring.shardingsphere.datasource.m3.password=root

# 定义分片规则,根据取模的值判断放到哪一个数据库
spring.shardingsphere.rules.sharding.sharding-algorithms.databases-inline.type=INLINE
spring.shardingsphere.rules.sharding.sharding-algorithms.databases-inline.props.algorithm-expression=m$->(id%3 + 1)
# 指定哪一个列作为主键,不能用原来的自增主键作为主键,因为id会重复
# 注意:中间的employee为表名
spring.shardingsphere.rules.sharding.tables.employee.key-generate-strategy.column=id
# 定义哪一个列作为分片键
spring.shardingsphere.rules.sharding.tables.employee.database-strategy.standard.sharding-column=id
# 绑定分片规则
spring.shardingsphere.rules.sharding.tables.employee.database-strategy.standard.sharding-algorithm-name=databases-inline
# 配置主键默认生成策略,雪花算法
spring.shardingsphere.sharding.default-key-generate-strategy.xxx=snowflake
# 雪花算法配置
spring.shardingsphere.rules.sharding.key-generators.snowflake.type=SNOWFLAKE	
# 配置机器唯一编码
spring.shardingsphere.rules.sharding.key-generators.snowflake.props.worker-id=666
 # 打印分库分表的sql语句
  spring.shardingsphere.props.sql-show=true
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值