zoukankan      html  css  js  c++  java
  • SQL分表

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

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

    三、怎样实现水平分表?
    其实水平分表的方法,很多,但个人觉得结合程序的增删改查,本篇介绍的方法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

  • 相关阅读:
    linux系统中只删除所有文件或只删除所有目录
    redhat7使用nmtui配置网络
    xshell,winscp,xftp无法用root用户登录
    windows无法访问指定设备路径或文件,你可能没有权限
    挂起/释放执行sap Job
    ALE,RFC和CPIC之间的关系
    CALL RFC synchronize and asychronize Examples
    关于BAPI
    ABAP调用SAP的单位转换函数进行单位转换
    BASIS--Client 锁定和解锁
  • 原文地址:https://www.cnblogs.com/hz04022016/p/7381193.html
Copyright © 2011-2022 走看看