Sharding-JDBC:垂直拆分怎么做?
经过读写分离的优化后,小王可算是轻松了一段时间,读写分离具体的方案请查看这篇文章:
Sharding-JDBC:查询量大如何优化?
可是好景不长,业务发展是在太快了。数据库中的数据量猛增,由于所有表都在一个数据库中,导致服务器本地存储快满了。

从上图我们可以看的出来,由于表的数量较多,每个表的数据量也较大,但是还没到水平拆分的地步。目前遇到的问题是服务器的存储不够了,短期内还不用水平拆分,那么方案呼之欲出了:垂直拆分。
解释下什么是垂直拆分?
我们都知道,一个数据库它是由N张表构成,每个表存储的数据都不一样,都对应着各自的业务。
所谓的垂直切分其实就是分类存储,大部分都是按业务类型进行分类。相同的类型存储在相同的库上,不同的类型存储在不同的库上,这样也就将数据或者说压力分担到不同的库上面 。
比如我们可以将用户相关的放一起,订单相关的放一起,行为日志相关的放一起,依次来推下去。
- 优点:
拆分之后业务规划清晰,数据维护简单,分担了数据集中存储的压力。
- 缺点:
缺点也很明显,多表join查询无法实现,只能通过接口方式解决,提高了系统复杂度等问题。

做垂直拆分其实跟读写分离是一样的,本质上还是多数据源的问题,本文中先考虑最简单的垂直拆分方式,垂直拆分+读写分离我们下篇文章进行讲解。
垂直拆分步骤
至于怎么整合Sharding-JDBC就不在讲解了,上篇文章有讲解过,直接开始和兴步骤。
假设我们拆分成了2个库,分别是ds_0和ds_1,每个库中的表不同,ds_0中放了user表,SQL脚本如下:
CREATE DATABASE `ds_0` CHARACTER SET 'utf8' COLLATE 'utf8_general_ci';
CREATE TABLE `user`(
id bigint(64) not null,
city varchar(20) not null,
name varchar(20) not null,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
ds_1中放了loudong表,SQL脚本如下:
CREATE DATABASE `ds_1` CHARACTER SET 'utf8' COLLATE 'utf8_general_ci';
CREATE TABLE `loudong` (
`id` varchar(20) NOT NULL,
`city` varchar(20) NOT NULL,
`region` varchar(20) NOT NULL,
`name` varchar(20) NOT NULL,
`ld_num` varchar(10) NOT NULL,
`unit_num` varchar(10) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8
最核心的还是数据源的配置以及绑定:
spring.shardingsphere.datasource.names=ds0,ds1
# ds0数据源
spring.shardingsphere.datasource.ds0.type=com.alibaba.druid.pool.DruidDataSource
spring.shardingsphere.datasource.ds0.driver-class-name=com.mysql.jdbc.Driver
spring.shardingsphere.datasource.ds0.url=jdbc:mysql://localhost:3306/ds_0?characterEncoding=utf-8
spring.shardingsphere.datasource.ds0.username=root
spring.shardingsphere.datasource.ds0.password=123456
# ds1数据源
spring.shardingsphere.datasource.ds1.type=com.alibaba.druid.pool.DruidDataSource
spring.shardingsphere.datasource.ds1.driver-class-name=com.mysql.jdbc.Driver
spring.shardingsphere.datasource.ds1.url=jdbc:mysql://localhost:3306/ds_1?characterEncoding=utf-8
spring.shardingsphere.datasource.ds1.username=root
spring.shardingsphere.datasource.ds1.password=123456
# 绑定loudong表所在节点
spring.shardingsphere.sharding.tables.loudong.actual-data-nodes=ds1.loudong
# 绑定user表所在节点
spring.shardingsphere.sharding.tables.user.actual-data-nodes=ds0.user
# 设置自增ID
spring.shardingsphere.sharding.tables.user.key-generator.column=id
# 设置自增ID算法
spring.shardingsphere.sharding.tables.user.key-generator.type=SNOWFLAKE
配置完之后该怎么用还是怎么用,完全不用改变一行代码。sharding-jdbc底层会对数据源进行接管。
如果我们不用sharding-jdbc的话,你同样需要配置2个数据源,这个其实差不多,最复杂的就是你在操作数据库的时候需要知道当前的操作是哪个数据源,因为每个数据源中的表都不一样,通过sharding-jdbc框架屏蔽了这些复杂的操作。
垂直拆分下的读写分离步骤
从最开始的单库多表,到读写分离,再到垂直拆分多个库。
循序渐进的为大家讲解高并发,大数据量下的数据库解决方案。并引入开源的Sharding-JDBC来实现具体的方案。
垂直拆分后进一步提升性能的方式就是垂直拆分多库的读写分离,如下图:

要实习这个功能,我们只需要在上面的基础上,为每个库增加一个从节点的配置就可以了,然后用master-slave-rules将主从数据源进行绑定,如下:
spring.shardingsphere.datasource.names=ds0,ds0slave,ds1,ds1slave
# ds0主数据源
spring.shardingsphere.datasource.ds0.type=com.alibaba.druid.pool.DruidDataSource
spring.shardingsphere.datasource.ds0.driver-class-name=com.mysql.jdbc.Driver
spring.shardingsphere.datasource.ds0.url=jdbc:mysql://localhost:3306/ds_0?characterEncoding=utf-8
spring.shardingsphere.datasource.ds0.username=root
spring.shardingsphere.datasource.ds0.password=123456
# ds0从数据源
spring.shardingsphere.datasource.ds0slave.type=com.alibaba.druid.pool.DruidDataSource
spring.shardingsphere.datasource.ds0slave.driver-class-name=com.mysql.jdbc.Driver
spring.shardingsphere.datasource.ds0slave.url=jdbc:mysql://localhost:3306/ds0slave?characterEncoding=utf-8
spring.shardingsphere.datasource.ds0slave.username=root
spring.shardingsphere.datasource.ds0slave.password=123456
# ds1主数据源
spring.shardingsphere.datasource.ds1.type=com.alibaba.druid.pool.DruidDataSource
spring.shardingsphere.datasource.ds1.driver-class-name=com.mysql.jdbc.Driver
spring.shardingsphere.datasource.ds1.url=jdbc:mysql://localhost:3306/ds_1?characterEncoding=utf-8
spring.shardingsphere.datasource.ds1.username=root
spring.shardingsphere.datasource.ds1.password=123456
# ds1从数据源
spring.shardingsphere.datasource.ds1slave.type=com.alibaba.druid.pool.DruidDataSource
spring.shardingsphere.datasource.ds1slave.driver-class-name=com.mysql.jdbc.Driver
spring.shardingsphere.datasource.ds1slave.url=jdbc:mysql://localhost:3306/ds1slave?characterEncoding=utf-8
spring.shardingsphere.datasource.ds1slave.username=root
spring.shardingsphere.datasource.ds1slave.password=123456
# 绑定loudong表所在节点
spring.shardingsphere.sharding.tables.loudong.actual-data-nodes=ds1.loudong
# 绑定user表所在节点
spring.shardingsphere.sharding.tables.user.actual-data-nodes=ds0.user
spring.shardingsphere.sharding.tables.user.key-generator.column=id
spring.shardingsphere.sharding.tables.user.key-generator.type=SNOWFLAKE
# 读写分离
spring.shardingsphere.sharding.master-slave-rules.ds0.master-data-source-name=ds0
spring.shardingsphere.sharding.master-slave-rules.ds0.slave-data-source-names=ds0slave
spring.shardingsphere.sharding.master-slave-rules.ds1.master-data-source-name=ds1
spring.shardingsphere.sharding.master-slave-rules.ds1.slave-data-source-names=ds1slave
源码参考:https://github.com/yinjihuan/sharding-jdbc
觉得不错的记得关注下哦,给个Star吧!

Sharding-JDBC:垂直拆分怎么做?的更多相关文章
- 数据库垂直拆分,水平拆分利器,cobar升级版mycat(转)
原文:数据库垂直拆分,水平拆分利器,cobar升级版mycat 1,关于Mycat Mycat情报 基于阿里的开源cobar ,可以用于生产系统中,目前在做如下的一些改进: 非阻塞IO的实现,相对于目 ...
- Sharding-JDBC实现垂直拆分
参考资料:猿天地 https://mp.weixin.qq.com/s/wl8h6LIQUHztVuVbjfsU3Q 作者:尹吉欢 当一个项目量增大,数据表数量增多时,就需要对数据表进行垂直拆分, ...
- 记录因Sharding Jdbc批量操作引发的一次fullGC
周五晚上告警群突然收到了一条告警消息,点开一看,应用 fullGC 了. 于是赶紧联系运维下载堆内存快照,进行分析. 内存分析 使用 MemoryAnalyzer 打开堆文件 mat 下载地址:htt ...
- mysql水平拆分与垂直拆分的详细介绍(转载http://www.cnblogs.com/nixi8/p/4524082.html)
垂直 垂直拆分是指数据表列的拆分,把一张列比较多的表拆分为多张表 通常我们按以下原则进行垂直拆分: 把不常用的字段单独放在一张表; 把text,blob等大字段拆分出来放在附表中; 经常组合查询的 ...
- 【mysql的设计与优化专题(4)】表的垂直拆分和水平拆分
垂直拆分 垂直拆分是指数据表列的拆分,把一张列比较多的表拆分为多张表 通常我们按以下原则进行垂直拆分: 把不常用的字段单独放在一张表; 把text,blob等大字段拆分出来放在附表中; 经常组合查询的 ...
- MySQL 水平拆分与垂直拆分详解
前言:说到优化mysql,总会有这么个回答:水平拆分,垂直拆分,那么我们就来说说什么是水平拆分,垂直拆分. 一.垂直拆分 说明:一个数据库由很多表的构成,每个表对应着不同的业务,垂直切分是指按照业务将 ...
- MYSQL水平拆分与垂直拆分
目前很多互联网系统都存在单表数据量过大的问题,这就降低了查询速度,影响了客户体验.为了提高查询速度,我们可以优化sql语句,优化表结构和索引,不过对那些百万级千万级的数据库表,即便是优化过后,查询速度 ...
- [转]系统架构演变--集中式架构-垂直拆分-分布式服务-SOA(服务治理)-微服务
一.系统架构演变 1.1. 集中式架构 当网站流量很小时,只需一个应用,将所有功能都部署在一起,以减少部署节点和成本.此时,用于简化增删改查工作量的数据访问框架(ORM)是影响项目开发的关键. 存在的 ...
- MySQL垂直拆分和水平拆分的优缺点和共同点总结
数据的拆分(Sharding)根据其拆分分规则的类型,可以分为两种拆分模式.一种是按照不同的表(或者Schema)来切分到不同的数据库(主机)之上,这种切可以称之为数据的垂直(纵向)拆分:另外一种则是 ...
随机推荐
- English:Root "tele"
Xx_Introduction tele mean "far" mean"faar" cognate word have tele\culture\tel\pa ...
- 特殊权限SUID
特殊权限SUID SUID : 运行某程序时,相应进程的属主是程序文件自身的属主,而不是启动者: chmod u+s File chmod u-s File 如果 FileB本身原来就有执行权限,则S ...
- Excel上下标如何设置?
Excel表格怎么设置上下标?Excel上下标设置技巧 在21世纪的我们,平时的工作和学习中,经常会使用到一些专业的文档,比如方程式.数据的公式和科学计数等,其中均会涉及到许多的上下标符号输入以及使用 ...
- 十二、ITK例程-医疗图像配准的HelloWorld程序
一.说明 医疗图像配准是ITK的一个重要内容,而我们今天想要说的一个程序则相当于是其中的HelloWorld程序. 程序源码位置: InsightToolkit-\Examples\Registrat ...
- PC端视频播放器
视频播放器:Potplayer 它是一款纯净的.无广告.极速
- [专题总结]初探插头dp
彻彻底底写到自闭的一个专题. 就是大型分类讨论,压行+宏定义很有优势. 常用滚动数组+哈希表+位运算.当然还有轮廓线. Formula 1: 经过所有格子的哈密顿回路数. 每个非障碍点必须有且仅有2个 ...
- 高阶函数&&高阶组件(二)
高阶组件总共分为两大类 代理方式 操纵prop 访问ref(不推荐) 抽取状态 包装组件 继承方式 操纵生命周期 操纵prop 代理方式之 操纵prop 删除prop import React fro ...
- Ubuntu18.04 设置开机进入命令行模式
首先来了解下启动级别(Runlevel): 指 Unix 或 类 Unix 操作系统下不同的运行模式,运行级别通常分为 7 级: 运行级别 0:系统停机状态,系统默认运行级别不能设为0,否则不能正常启 ...
- swoole视频直播
$serv=new swoole_websocket_server("0.0.0.0",9501);$client=array();$serv->on("open& ...
- 母版页 treeview控件 SiteMapPath控件 treeview数据库绑定模式
母版页就是网站中一样的部分母版页的后缀名是.Master可以把母版页当成一个页面 想让哪里是别的内容就可以 通过如下: <asp:ContentPlaceHolder ID="C ...