您好,欢迎访问一九零五行业门户网

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

相对于水平拆分来说,垂直拆分比较容易实现一些,垂直拆分的意思是把数据库中不同的业务数据拆分到不同的数据库中。垂直拆分能很
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,biginteger 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;}} 更多详情见请继续阅读下一页的精彩内容:
其它类似信息

推荐信息