当前位置:网站首页>MySQL sub-database sub-table
MySQL sub-database sub-table
2022-07-30 19:36:00 【prosperous】
1.The background of sub-library and sub-table generation
There are the following performance bottlenecks when using single database storage:
①IO瓶颈:热点数据太多,数据库缓存不足,产生大量磁盘IO,效率较低.请求数据太多,带宽不够,网络IO瓶颈.
②CPU瓶颈:排序,分组,连接查询,聚合统计等SQL会消耗大量的CPU资源,请求数太多,CPU出现瓶颈.
Sub-databases and sub-tables store data in a decentralized manner,使得单一数据库/表的数据量变小来缓解单一数据库的性能问题.
2.拆分策略:
水平拆分:水平分表,水平分库;
垂直拆分:垂直分表,垂直分库.
垂直分库:以表为依据,根据业务将不同表拆分到不同库中.特点:①每个库的表结构都不一样;②每个库的数据也不一样;③所有库的并集是全量数据.The following figure shows the vertical sub-library case.
垂直分表:以字段为依据,根据字段属性将不同字段拆分到不同表中.特点:①每个表的结构都不一样;②每个表的数据也不一样,一般通过一列(主键/外键)关联;③所有表的并集是全量数据.The figure below shows an example of vertical sub-tables,Both tables have primary keysid关联.
水平分库:以字段为依据,按照一定策略,将一个库的数据拆分到多个库中.特点:①每个库的表结构都一样;②每个库的数据都不一样;③所有库的并集是全量数据.The figure below shows the horizontal sub-library.
水平分表:以字段为依据,按照一定的策略,将一个表的数据拆分到多个表中.特点:①每个表的表结构都一样;②每个表的数据都不一样;③所有表的并集是全量数据.The figure below shows the horizontal sub-table.
3.The realization technology of sub-database and sub-table
shardingJDBC:基于AOP原理,Executed locally on the application pairSQL进行拦截,解析,改写,路由处理.需要自行编码配置实现,支持java语言,性能较高.
MyCat:数据库分库分表中间件,不用调整代码即可实现分库分表,支持多种语言,性能不及shardingJDBC.
4.MyCat
MyCat是一个数据库中间件,使用MyCat也很简单,Replace our previous connection to the database with a connectionMyCat即可.
mycat的核心概念:mycat中不存储数据,The data is stored in the node host,Which node host is stored in accordance with the sharding rules;mycatJust a logical structure,它是无感知的.
5.mycat分片配置
schema.xml涵盖了mycat的逻辑库,逻辑表,分片规则,分片节点及数据源的配置.There are mainly three sets of labels:schema标签,datanode标签,datahost标签
配置完schema.xml后,Also modify the directory at the same levelserver.xml文件,将schemasReplace with our configurationschema;
server.xml配置文件包含了mycat的系统配置信息,主要有两个重要标签:system,user
rule.xml中定义所有拆分表的规则,The sharding algorithm can be used flexibly during use,或对同一个分片算法使用不同的参数,它让分片过程可配置化,There are two main types of protection bureau labels:tableRule,function.
6.mycat启动
mycat启动后,占用端口8066.
7.mycat分片
垂直分库
mycat分片情况下,Involves cross-database queries(跨分片查询)会报错,因为mycatUnable to determine thisSQLWhich shard should be routed to.
解决方案:Set the involved tables as global tables(在schema.xml中table标签加上type=‘global’,dataNodefor all nodes),mycatDo it on any node in the global tableDML时,All nodes are synchronized
水平分表
The core of horizontal sharding lies in sharding rules,The sharding rules only need to be filled in for horizontal sharding.
8.Common sharding rules:
范围分片:根据指定的字段及其配置的范围与数据节点的对应情况,来决定该数据属于哪一个分片.rule=‘auto-sharding-long’;
取模分片:Performs a modulo operation based on the specified field and number of nodes,根据运算结果,来决定该数据属于哪一个分片.rule=‘mod-long’;
一致性hash分片:根据指定的字段,calculated fieldhash值,根据运算结果,来决定该数据属于哪一个分片.rule=‘sharding-by-murmur’;
枚举分片:通过在配置文件中配置可能的枚举值,指定数据分布到不同数据节点上,本规则适用于按照省份,性别,状态拆分数据等业务.rule=‘sharding-by-intfile-enumstatus’;
Data that exceeds the enumeration value should be stored in a node.
应用指定分片:运行阶段由应用自主决定路由到哪个分片,directly from the string(必须是数字)计算分片号.rule=‘sharding-by-substring’;
固定分片hash算法:This operation is similar to the modulo operation in decimal.例如:取id的二进制低10位与1111111111进行位&运算.rule=‘sharding-by-long-hash’;
位&运算:同为1则为1,有一个0则为0.例如:
1010101010&1111111111 = 1010101010
特点:①如果是求模,Consecutive values are assigned to different shards,但是此算法会将连续的值可能分配到相同的分片,降低事务处理的难度;②可以均匀分配,也可以非均匀分配;③分片字段必须为数字类型
字符串hash解析分片:Intercepts the string at the specified position in the string,进行hash算法,算出分片.rule=‘sharding-by-stringhash’;
按(天)日期分片:从开始时间开始,每10天(可以自行设置)为一个分片,After reaching the end time,会重复开始分片插入.rule=‘sharding-by-date’;
配置表的DataNode的分片,必须和分片规则数量一致,例如2022-01-01到2022-12-31,每10天一个分片,一共需要37个分片.因此,Be sure to choose the start date and end date carefully.
按自然月分片:Divide by month,每个自然月为一个分片.rule=‘sharding-by-month’;
配置表的DataNode的分片,必须和分片规则数量一致,例如2022-01-01到2022-12-31,一共需要12个分片.因此,Be sure to choose the start date and end date carefully.
9.mycat的监控与管理
9.1、mycat的原理
Each node only stores a part of the data,因此,聚合处理、Processing such as sorting and paging at each node is meaningless,mycatThe results of the query are merged and then processed.
9.2、mycat管理
mycat默认开通2个端口,可以在server.xml中进行修改.8066Data access ports and 9066数据库管理端口.
9.3、mycat图形化界面mycat-eye
mycat-eye是对mycat-server提供监控服务,功能不局限于对mycat-server使用,通过JDBC连接对mycat,mysql监控,监控远程服务器(目前仅限于linux系统)的cpu、内存、网络、磁盘.
mycat-eye运行过程中需要依赖zookeeper,因此需要先安装zookeeper.
先自我介绍一下,小编13年上师交大毕业,曾经在小公司待过,去过华为OPPO等大厂,18年进入阿里,直到现在.深知大多数初中级java工程师,想要升技能,往往是需要自己摸索成长或是报班学习,但对于培训机构动则近万元的学费,着实压力不小.自己不成体系的自学效率很低又漫长,而且容易碰到天花板技术停止不前.因此我收集了一份《java开发全套学习资料》送给大家,初衷也很简单,就是希望帮助到想自学又不知道该从何学起的朋友,同时减轻大家的负担.添加下方名片,即可获取全套学习资料哦
边栏推荐
- 来了!东方甄选为龙江农产品直播带货
- Scala学习:类和对象
- LeetCode 0952. Calculate Maximum Component Size by Common Factor: Mapping / Union Search
- 尊重客观事实
- Go system collection
- Win11如何更改默认下载路径?Win11更改默认下载路径的方法
- 卫星电话是直接与卫星通信还是通过地面站?
- Listen to the boot broadcast
- The Meta metaverse division lost 2.8 billion in the second quarter!Still want to keep betting?Metaverse development has yet to see a way out!
- 【刷题篇】计算质数
猜你喜欢
Alibaba Cloud Martial Arts Headline Event Sharing
【网站放大镜效果】两种方式实现
浅聊对比学习(Contrastive Learning)第一弹
MindSpore:【Resolve node failed】解析节点失败的问题
技术很牛逼,还需要“向上管理”吗?
部分分类网络性能对比
谷歌AlphaFold近日宣称预测出地球上几乎所有蛋白质结构
Zabbix 5.0 监控教程(一)
MindSpore:【MindSpore1.1】Mindspore安装后验证出现cudaSetDevice failed错误
The advanced version of the Niu Ke brushing series (team competition, sorting subsequences, inverting strings, deleting common characters, repairing pastures)
随机推荐
ResNet18-实现图像分类
启动前台Activity
Delay queue optimization (2)
MindSpore:【模型训练】【mindinsight】timeline的时间和实际用时相差很远
Niuke.com - Huawei Question Bank (100~108)
MindSpore:npu 多卡训练自定义数据集如何给不同npu传递不同数据
musicApp 的.eslintrc.js
实体中增加操作方法
Spark学习:编译Spark项目时遇到的报错
Trial writing C language sanbang
Swiper rotates pictures and plays background music
MySQl数据库————DQL数据查询语言
OMP: Error #15: Initializing libiomp5md.dll, but found libiomp5md.dll already initialized.解决方法
MindSpore:ImageFolderDataset数据读取问题
7.29模拟赛总结
The technology is very powerful, do you still need to "manage up"?
Entering the applet for the first time
卫星电话是直接与卫星通信还是通过地面站?
crontab中写go run不执行的问题
mysql慢查询优化