一、为什么要水平分表?
简而言之,当单表数据量过大时,无法对其进行有效的维护,以及查询速度严重变慢时,我们就需要对其时行水平分表.

二、什么时候需要水平分表?
在数据库结构的设计中,需要充分考虑后期数据的增长量和增长速度,如果后期的数据增长量过快,以及后期数据量巨大,就需要使用水平分表。

三、怎样实现水平分表?
其实水平分表的方法,很多,但个人觉得结合程序的增删改查,本篇介绍的方法MRG_MySIAM存储引擎(MERGE存储引擎)个人觉得还是比较简单方便的,虽然性能方面与其它分表技术相比可能不是第一,但就使用程序对其的操控性来说,个人觉得还是很不错的。

四、Merge表的优点:
A: 分离静态的和动态的数据
B:利用结构接近的的数据来优化查询
C: 查询时可以访问更少的数据
D:更容易维护大数据集
E: 可以通过修改.mrg文件来修改Merge表,当然也可以用alter进行修改,修改后要通过FLUSH TABLES刷新表缓存,此法可以动态增加减少子表

五、分表步骤:

1.首先创建一张MERGE存储类型的主表,

drop table if exists weather_temp;
create table weather_temp like weather_data;

2.给weather_date字段设置索引

alter table weather_temp add INDEX weather_date(weather_date);

3.给weather_temp设置制定引擎engine=myisam

alter table weather_temp ENGINE=MyISAM;

4.然后再批量创建8张MyISAM存储类型的数据表。

drop table if exists weather_temp_1;
create table weather_temp_1 like weather_temp;
drop table if exists weather_temp_2;
create table weather_temp_2 like weather_temp;
drop table if exists weather_temp_3;
create table weather_temp_4 like weather_temp;
drop table if exists weather_temp_4;
create table weather_temp_4 like weather_temp;
drop table if exists weather_temp_5;
create table weather_temp_5 like weather_temp;
drop table if exists weather_temp_6;
create table weather_temp_6 like weather_temp;
drop table if exists weather_temp_7;
create table weather_temp_7 like weather_temp;
drop table if exists weather_temp_8;
create table weather_temp_8 like weather_temp;

5.修改weather_temp设置联合查询
alter table weather_temp ENGINE=MERGE UNION=(weather_temp_1,weather_temp_2,weather_temp_3,weather_temp_4,weather_temp_5,weather_temp_6,weather_temp_7,weather_temp_8) INSERT_METHOD=LAST;

注意:总表只是一个外壳,存取数据发生在一个一个的分表里面。

6.问题分析 (插入)
主表插入:
主表插入id自动分配不会重复 通过union来增加或删除分表来满足部分业务的需求,大多数按照时间来做分表。
分表插入:
插入分表后,查询主表会出现重复id。

id不重复:第三方redis维护 数据库建表维护id。
当id不被引用,可以直接插入分表不单独维护id。

7.更新和删除
建议以分表为主 更新或者删除分表的效率高时间短。

8.删除表问题
不能直接删除一个分表,这样会破坏merge表。正确的方法是:

alter table weather_temp ENGINE=MRG_MyISAM UNION=(weather_temp_2) INSERT_METHOD=LAST;

drop table weather_temp_2

最新文章

  1. Sublime Text3使用总结
  2. 利用WCF双工模式实现即时通讯
  3. Libliner 中的-s 参数选择:primal 和dual
  4. JavaScript - 基本概念
  5. 使用MapReduce实现一些经典的案例
  6. POJ 1679 The Unique MST --Kruskal应用
  7. javascript常用排序算法总结
  8. SQL Server事务的隔离级别
  9. Sublime Text 快捷键
  10. Java MongoDB 资料集合
  11. 数据一致性(consistency)、服务可用性(availability)、分区容错性(partition-tolerance)
  12. C语言学习第四章
  13. 数据库【mongodb篇】基本命令学习笔记
  14. spark读写hbase性能对比
  15. 【POJ1179】Polygon 区间DP
  16. [Model] ResNet
  17. 互联网自治域间IP源地址验证技术综述
  18. LeetCode题解之To Lower Case
  19. Ext ComboBox 动态查询
  20. 理解数据库中的undo日志、redo日志、检查点

热门文章

  1. PCL学习(一)从PLY文件读入点云数据
  2. [转帖]微软宣布即将开始大规模推送Windows 10 V1903重大版本更新
  3. FastJson反序列化获取不到值
  4. 安全篇-AES/RSA加密机制
  5. mac OS 安装qt环境
  6. Python操作Redis(转)
  7. 解决django的后台管理界面添加中文内容乱码问题
  8. (十四)Hibernate中的多表操作(4):单向一对一
  9. LinearSearch Java
  10. vue 集成 NEditor 富文本