Mysql分库分表
分库分表是为了解决数据量过大而导致数据库性能降低的问题,将原来独立的数据库拆分成若干个数据库组成,将数据库大表拆分成多个数据库表组成,使单一的数据库,单一的数据表的内容变小,从而达到提升数据库性能的目的
分库分表的方式
分库分表分为分库和分表两个部分,而这两个部分都可以统称为数据分片,其目的都是讲数据库分成不同的存储单员,另外从分拆的角度上可以分为垂直分片和水平分片
- 垂直分片:按照业务来对数据进行分片,又称为纵向分片。他的核心理念就是转库专用。在拆分之前,一个数据库由多个数据表组成,每个表对应不同的业务。 而拆分之后,则是按照业务将表进行归类,分布到不同的数据库或表中,从而将压力分散至不同的数据库或表
- 水平分片:又称横向分片。相对于垂直分片,它不再将数据根据业务逻辑分类,而是通过某个字段(或某几个字段),根据某种规则将数据分散至多个库或表中,每个分片仅包含数据的一部分。
Sharding-JDBC快速实战
核心概念:
- 逻辑表:水平拆分的数据库的相同逻辑和数据结构表的总称 (就是一个虚表,用来操作已经分开的数据库表文件)
- 真实表:在分片的数据库中真实存在的物理表。
- 数据节点:数据分片的最小单元。由数据源名称和数据表组成
- 绑定表:分片规则一致的主表和子表。
- 广播表:也叫公共表,指素有的分片数据源中都存在的表,表结构和表中的数据在每个数据库中都完全一致。例如字典表。
- 分片键:用于分片的数据库字段,是将数据库(表)进行水平拆分的关键字段。SQL中若没有分片字段,将会执行全路由,性能会很差。
- 分片算法:通过分片算法将数据进行分片,支持通过=、BETWEEN和IN分片。分片算法需要由应用开发者自行实现,可实现的灵活度非常高。
- 分片策略:真正用于进行分片操作的是分片键+分片算法,也就是分片策略。在 ShardingJDBC中一般采用基于Groovy表达式的inline分片策略
快速实战
maven
<!--数据库 连接池-->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.1.22</version>
</dependency>
<!--ShardingSphere 依赖-->
<dependency>
<groupId>org.apache.shardingsphere</groupId>
<artifactId>sharding-jdbc-spring-boot-starter</artifactId>
<version>4.1.1</version>
</dependency>
分表不分库
application.propertise
#配置一个数据源
spring.shardingsphere.datasource.names=m1
spring.shardingsphere.datasource.m1.type=com.alibaba.druid.pool.DruidDataSource
spring.shardingsphere.datasource.m1.drive-class-name=com.mysql.cj.jdbc.Driver
spring.shardingsphere.datasource.m1.url=jdbc:mysql://localhost:3306/coursedb?serverTimezone=GMT%2B8
spring.shardingsphere.datasource.m1.username=root
spring.shardingsphere.datasource.m1.password=root
#配置绑定表 course表两个真实表的分布情况 m1数据源内的 course1 course2
spring.shardingsphere.sharding.tables.course.actual-data-nodes=m1.course_$->{1..2}
# 配置 cid这个列需要自动生成 type采用雪花算法(推特的算法) props配置算法 可以不用配置 有默认
spring.shardingsphere.sharding.tables.course.key-generator.column=cid
spring.shardingsphere.sharding.tables.course.key-generator.type=SNOWFLAKE
spring.shardingsphere.sharding.tables.course.key-generator.props.worker.id=1
#表策略 分片建采用cid 分片算法采用后面的表达式
spring.shardingsphere.sharding.tables.course.table-strategy.inline.sharding-column=cid
spring.shardingsphere.sharding.tables.course.table-strategy.inline.algorithm-expression=course_$->{cid%2+1}
#sql执行语句打印
spring.shardingsphere.props.sql.show=true
spring.main.allow-bean-definition-overriding=true
分库分表配置
#配置第一个数据源 名字随意起 m1
spring.shardingsphere.datasource.names=m1,m2
spring.shardingsphere.datasource.m1.type=com.alibaba.druid.pool.DruidDataSource
spring.shardingsphere.datasource.m1.drive-class-name=com.mysql.cj.jdbc.Driver
spring.shardingsphere.datasource.m1.url=jdbc:mysql://localhost:3306/coursedb?serverTimezone=GMT%2B8
spring.shardingsphere.datasource.m1.username=root
spring.shardingsphere.datasource.m1.password=root
#配置第二个数据源 名字是m2
spring.shardingsphere.datasource.m2.type=com.alibaba.druid.pool.DruidDataSource
spring.shardingsphere.datasource.m2.drive-class-name=com.mysql.cj.jdbc.Driver
spring.shardingsphere.datasource.m2.url=jdbc:mysql://localhost:3306/coursedb_1?serverTimezone=GMT%2B8
spring.shardingsphere.datasource.m2.username=root
spring.shardingsphere.datasource.m2.password=root
#配置绑定表 course表两个真实表的分布情况 m1数据源内的 course1 course2 或m2 内的course_1 _2
spring.shardingsphere.sharding.tables.course.actual-data-nodes=m$->{1..2}.course_$->{1..2}
# 配置 cid这个列需要自动生成 type采用雪花算法(推特的算法) props配置算法 可以不用配置 有默认
spring.shardingsphere.sharding.tables.course.key-generator.column=cid
spring.shardingsphere.sharding.tables.course.key-generator.type=SNOWFLAKE
spring.shardingsphere.sharding.tables.course.key-generator.props.worker.id=1
#表策略 分片建采用cid 分片算法采用后面的表达式
spring.shardingsphere.sharding.tables.course.table-strategy.inline.sharding-column=cid
spring.shardingsphere.sharding.tables.course.table-strategy.inline.algorithm-expression=course_$->{cid%2+1}
#分库策略 根据cid分库 分片算法是cid按2取模1就去m1数据库 2就去m2数据库
spring.shardingsphere.sharding.tables.course.database-strategy.inline.sharding-column=cid
spring.shardingsphere.sharding.tables.course.database-strategy.inline.algorithm-expression=m$->{cid%2+1}
#sql执行语句打印
spring.shardingsphere.props.sql.show=true
spring.main.allow-bean-definition-overriding=true
Sharding-JDBC读写分离
spring.shardingsphere.datasource.names=m1,m2
#主库
spring.shardingsphere.datasource.m1.type=com.alibaba.druid.pool.DruidDataSource
spring.shardingsphere.datasource.m1.drive-class-name=com.mysql.cj.jdbc.Driver
spring.shardingsphere.datasource.m1.url=jdbc:mysql://localhost:3306/coursedb?serverTimezone=GMT%2B8
spring.shardingsphere.datasource.m1.username=root
spring.shardingsphere.datasource.m1.password=root
#从库
spring.shardingsphere.datasource.m2.type=com.alibaba.druid.pool.DruidDataSource
spring.shardingsphere.datasource.m2.drive-class-name=com.mysql.cj.jdbc.Driver
spring.shardingsphere.datasource.m2.url=jdbc:mysql://localhost:3306/coursedb_1?serverTimezone=GMT%2B8
spring.shardingsphere.datasource.m2.username=root
spring.shardingsphere.datasource.m2.password=root
#配置 主从规则 ds0是规则名字 主库的数据源
spring.shardingsphere.sharding.master-slave-rules.ds0.master-data-source-name=m1
#配置 主从规则 从库的数据源
spring.shardingsphere.sharding.master-slave-rules.ds0.slave-data-source-names[0]=m2
#配置数据表的真实表分布情况
spring.shardingsphere.sharding.tables.t_dict.actual-data-nodes=ds0.t_dist
# 配置 cid这个列需要自动生成 type采用雪花算法(推特的算法) props配置算法 可以不用配置 有默认
spring.shardingsphere.sharding.tables.t_dict.key-generator.column=t_id
spring.shardingsphere.sharding.tables.t_dict.key-generator.type=SNOWFLAKE
spring.shardingsphere.sharding.tables.t_dict.key-generator.props.worker.id=1
##绑定表
#spring.shardingsphere.sharding.binding-tables[0]=t_user,t_dict
#sql执行语句打印
spring.shardingsphere.props.sql.show=true
spring.main.allow-bean-definition-overriding=true