mysql分区技术

// 为什么不用分表?而使用分区技术

导致程序的工作量倍数增长,兼容性要求更高。所有mysql5.1之后,就有了分区技术。 => 解决水平分表问题

 

// 四种分区技术:rang分区、list分区、hash分区、key分区(不常用)

==> 主要使用rang和list分区技术,这两种可以按照时间段,id等进行分区,可以增加删除分区等。

==> hsah分区能够把数据进行均匀的分散到多个分区里。

 

// 分区例子:

// 创建表
create table webservicelog(
    `id` int(11) unsigned not null auto_increment,
    `fromto` tinyint(1) not null default '0',
    `biztype` tinyint(2) not null default '0',
    `bizcode` varchar(32) not null default '',
    `result` tinyint(1) unsigned not null default '0',
    `errmsg` varchar(256) not null default '',
    `desc` varchar(100) not null default '',
    `oprtime` datetime,
    key `id` (`id`),
    key `biz`(`biztype`,`bizcode`),
    KEY `operatetime` (`oprtime`)
)
ENGINE=InnoDB AUTO_INCREMENT=1  DEFAULT CHARSET=utf8

// 分区的设置,以oprtime作为分区的依据
partition by range(to_days(`oprtime`))(
    partition p201610 values less than (to_days('2016-11-01')),
    partition p201611 values less than (to_days('2016-12-01')),
    partition p201612 values less than (to_days('2017-01-01')),
    partition p201701 values less than maxvalue
);

————以上表示,小于2016年11月01号的,存储在p201610这个分区;2016-12-01前的,存储在p201611这个分区,以此类推。

 

基本上每一个表都要有一个主键,这里不能以id作为主键,但是可以是自增长的类型,如果以id为主键,则分区依据中一定要包含id字段才行,

这里只是以range(oprtime)作为分区的依据,所以不用将id设为主键,但是这条日志信息需要看详情时,需要根据id来查找(这样效率高一些),就需要将id作为一个索引了。

 

// 例子2:

create table t4(id int)engine=innodb
partition by RANGE(id)(
    partition p0 values less than(10000),
    partition p1 values less than(20000),
    PARTITION p2 VALUES less than MAXVALUE
);

————以上表示:用range的方式进行分区,分区依据为id,当id < 10000的时候,存在p0区域,当id < 20000的时候,存在p1区域...

 

 

// 查看分区是否生效

explain partitions select * from webservicelog where oprtime< date '2016-10-21';

 

// 删除并且重建分区:

alter table webservicelog drop partition p201701;
alter table webservicelog add partition(partition p201701 values less than (to_days('2017-02-01')));
alter table webservicelog add partition(partition p201702 values less than (to_days('2017-03-01')));

 

 

 

 

 

 

 

 

——————占位符

转载于:https://www.cnblogs.com/windyet/articles/9647874.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值