• 欢迎访问搞代码网站,推荐使用最新版火狐浏览器和Chrome浏览器访问本网站!
  • 如果您觉得本站非常有看点,那么赶紧使用Ctrl+D 收藏搞代码吧

MySQL 垂直切分(读书笔记整理)

mysql 搞代码 4年前 (2022-01-09) 32次浏览 已收录 0个评论

相对于水平拆分来说,垂直拆分比较容易实现一些,垂直拆分的意思是把数据库中不同的业务数据拆分到不同的数据库中。垂直拆分能很

1,垂直拆分

相对于水平拆分来说,垂直拆分比较容易实现一些,垂直拆分的意思是把数据库中不同的业务数据拆分到不同的数据库中。垂直拆分能很好的起到分散数据库压力的作用。业务模块不明晰,耦合(表关联)度比较高的系统不适合使用这种拆分方式。

有得用户查询积分快,有的用户查询自己的订单很快,但是查询自己的用户信息很慢,为什么?

2,垂直切分的优点

◆ 数据库的拆分简单明了,拆分规则明确;
◆ 应用程序模块清晰明确,整合容易;
◆ 数据维护方便易行,,容易定位;

3,垂直切分的缺点

◆ 部分表关联无法在数据库级别完成,需要在程序中完成;
◆ 对于访问极其频繁且数据量超大的表仍然存在性能平静,不一定能满足要求;
◆ 事务处理相对更为复杂;
◆ 切分达到一定程度之后,扩展性会遇到限制;
◆ 过读切分可能会带来系统过渡复杂而难以维护。

针对于垂直切分可能遇到数据切分及事务问题,在数据库层面实在是很难找到一个较好的处理方案。实际应用案例中,数据库的垂直切分大多是与应用系统的模块相对应,同一个模块的数据源存放于同一个数据库中,可以解决模块内部的数据关联问题。而模块与模块之间,则通过应用程序以服务接口方式来相互提供所需要的数据。虽然这样做在数据库的总体操作次数方面确实会有所增加,但是在系统整体扩展性以及架构模块化方面,都是有益的。

可能在某些操作的单次响应时间会稍有增加,但是系统的整体性能很可能反而会有一定的提升。而扩展瓶颈问题。

4,拆分规则

根据模块拆分,比如用户模块、订单模块、日志模块,系统参数模块
用户模块的表:uc_user表;uc_user_info表;uc_action表;uc_profile表
订单模块表:order_action表;order_list表;shop表;order表;
日志模块:order_log表;uc_log表;plocc_log表;
系统参数模块:plocc_parameter表;

模块模块之间都一看都是有联系的,这些都是用到用户的,那么都和用户模块有关联

如下图所示:

5,垂直拆分演示5.1准备mysql环境

创建多实例参考:

建库sql语句:

user_db;CREATE TABLE user_db.`uc_user` (`user_id` bigint(20) NOT NULL,,`created_time` datetime DEFAULT NULL,PRIMARY KEY (`user_id`)) ENGINE=InnoDB ,;— 查询mysql order_db;CREATE TABLE order_db.`order` (`order_id` bigint(20) NOT NULL,,`created_time` datetime DEFAULT NULL,`user_id` bigint(20) not null,PRIMARY KEY (`order_id`)) ENGINE=InnoDB ,;— 查询mysql log_db;CREATE TABLE log_db.`order_log` (`orlog_id` bigint(20) NOT NULL,,`created_time` datetime DEFAULT NULL,`user_id` bigint(20) not null,,PRIMARY KEY (`orlog_id`)) ENGINE=InnoDB ,;— 查询 mysql plocc_db;CREATE TABLE plocc_db.`plocc_parameter` (`plocc_id` bigint(20) NOT NULL,,`created_time` datetime DEFAULT NULL,`status` bigint(20) not null,`creator` bigint not null,PRIMARY KEY (`plocc_id`)) ENGINE=InnoDB ,;5.2 演示java代码package mysql;import java.math.BigInteger;import java.sql.Connection;import java.sql.DriverManager;import java.sql.PreparedStatement;import java.sql.ResultSet;import java.sql.SQLException;import java.sql.Statement;import java.text.SimpleDateFormat;import java.util.Calendar;import java.util.Date;{user_id;(String[] args) {MySQLTest2 mt=new MySQLTest2();/* 录入用户数据BigInteger user_id0 = new BigInteger(“10001”);Connection conn=mt.getConn(“user_db”);mt.insertUser(conn, bi, “tim–“+user_id0.longValue());*//* 录入日志数据Connection conn2=mt.getConn(“log_db”);BigInteger user_id = new BigInteger(“10001”);BigInteger order_id = new BigInteger(“20150531001”);BigInteger orlog_id = new BigInteger(“20150531001”);mt.insertLog(conn2, user_id,order_id , orlog_id, “create a order for tim”);*///录入订单数据Connection conn3=mt.getConn(“order_db”);BigInteger user_id2 = new BigInteger(“10001”);BigInteger order_id2 = new BigInteger(“20150531001”);BigInteger shop_id2 = new BigInteger(“20150531001”);mt.insertOrder(conn3,order_id2 , shop_id2, user_id2);}Connection getConn(String type ) {String port=“3307”;if (type==“user_db” ){port=“3307”;}else if(type==“order_db”){port=“3308”;}else if(type==“log_db”){port=“3309”;}else if(type==“plocc_db”) {port=“3310”;}else{port=“3311”;}Connection conn = null;try {Class.forName(“com.mysql.jdbc.Driver”);} catch (ClassNotFoundException e) {// TODO Auto-generated catch blocke.printStackTrace();}String url = “jdbc:mysql://192.168.52.130:”+port+http://www.linuxidc.com/”+type;try {conn = DriverManager.getConnection(url, “tim”, “timgood2013”);} catch (SQLException e) {// TODO Auto-generated catch blocke.printStackTrace();}System.out.println(“the current db is :”+url);return conn;}// 获取日期字符串public String getTimeByCalendar(){/*Calendar cal = Calendar.getInstance();int year = cal.get(Calendar.YEAR);//获取年份int month=cal.get(Calendar.MONTH);//获取月份int day=cal.get(Calendar.DATE);//获取日int hour=cal.get(Calendar.HOUR);//小时int minute=cal.get(Calendar.MINUTE);//分int second=cal.get(Calendar.SECOND);//秒String strdate=year+”-“+month+”-“+day+” “+hour+”:”+minute+”:”+second;*/SimpleDateFormat df = new SimpleDateFormat(“yyyy-MM-dd HH:mm:ss”);//设置日期格式System.out.println(df.format(new Date()));// new Date()为获取当前系统时间return df.format(new Date());}(Connection cnn,BigInteger user_id,String name){String sql=“insert into user_db.uc_user(user_id,uc_name,created_time)values(?,?,?)”;int i=0;long uid = user_id.longValue();Connection conn=cnn;try{PreparedStatement preStmt =conn.prepareStatement(sql);preStmt.setLong(1, uid);preStmt.setString(2,name);preStmt.setString(3,getTimeByCalendar());i=preStmt.executeUpdate();}catch (SQLException e){e.printStackTrace();}return i;//返回影响的行数,1为执行成功 }(Connection cnn,BigInteger user_id,BigInteger order_id,BigInteger orlog_id,String action){String sql=“insert into log_db.order_log(orlog_id,order_id,created_time,user_id,action)values(?,?,?,?,?)”;int i=0;Connection conn=cnn;try{PreparedStatement preStmt =conn.prepareStatement(sql);preStmt.setLong(1, user_id.longValue());preStmt.setLong(2, order_id.longValue());preStmt.setString(3,getTimeByCalendar());preStmt.setLong(4, orlog_id.longValue());preStmt.setString(5,action);i=preStmt.executeUpdate();}catch (SQLException e){e.printStackTrace();}return i;//返回影响的行数,1为执行成功 }(Connection cnn,B

本文来源gaodai.ma#com搞##代!^码@网3

igInteger order_id,BigInteger shop_id,BigInteger user_id){String sql=“insert into order_db.order(order_id,shop_id,created_time,user_id)values(?,?,?,?)”;int i=0;Connection conn=cnn;try{PreparedStatement preStmt =conn.prepareStatement(sql);preStmt.setLong(1, order_id.longValue());preStmt.setLong(2, shop_id.longValue());preStmt.setString(3,getTimeByCalendar());preStmt.setLong(4, user_id.longValue());i=preStmt.executeUpdate();}catch (SQLException e){e.printStackTrace();}return i;//返回影响的行数,1为执行成功 }(){int i=0;return i;}}

更多详情见请继续阅读下一页的精彩内容:


搞代码网(gaodaima.com)提供的所有资源部分来自互联网,如果有侵犯您的版权或其他权益,请说明详细缘由并提供版权或权益证明然后发送到邮箱[email protected],我们会在看到邮件的第一时间内为您处理,或直接联系QQ:872152909。本网站采用BY-NC-SA协议进行授权
转载请注明原文链接:MySQL 垂直切分(读书笔记整理)

喜欢 (0)
[搞代码]
分享 (0)
发表我的评论
取消评论

表情 贴图 加粗 删除线 居中 斜体 签到

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址