SQL分表

1、为何要水平分表?
简而言之,当单表数据量过大时,没法对其进行有效的维护,以及查询速度严重变慢时,咱们就须要对其时行水平分表.redis

2、何时须要水平分表?
在数据库结构的设计中,须要充分考虑后期数据的增加量和增加速度,若是后期的数据增加量过快,以及后期数据量巨大,就须要使用水平分表。数据库

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

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

5、分表步骤:大数据

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

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

 

2.给weather_date字段设置索引设计

alter table weather_temp add INDEX weather_date(weather_date);索引

 

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

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

相关文章
相关标签/搜索