详解MySQL分区表

发布时间:2020-07-20 15:00:13 作者:小猪
来源:亿速云 阅读:153

小编这次要给大家分享的是详解MySQL分区表,文章内容丰富,感兴趣的小伙伴可以来了解一下,希望大家阅读完这篇文章之后能够有所收获。

前言

在最近的项目中,我们需要保存大量的数据,而且这些数据是有有效期的,为了提供查询效率以及快速删除过期数据,我们选择了MySQL的分区机制。把数据按照时间进行分区。

分区类型

分区命令

创建分区

CREATE TABLE `access_log` (
 `id` int(11) NOT NULL AUTO_INCREMENT,
 `access_time` datetime NOT NULL,
 PRIMARY KEY (`id`,`access_time`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8
/*!50100 PARTITION BY RANGE (to_days(access_time))
(PARTITION p1 VALUES LESS THAN (to_days(20190101)) ENGINE = InnoDB,
 PARTITION p2 VALUES LESS THAN (to_days(20190102)) ENGINE = InnoDB,
 PARTITION p3 VALUES LESS THAN (to_days(20190103)) ENGINE = InnoDB) */;

创建后可以看到,每个分区都会对应1个ibd文件

详解MySQL分区表
分区表

新增分区

alter table access_log add partition(
 partition p4 values less than (to_days('20190105'))
);

删除分区

alter table access_log drop partition p1;

拆分分区

alter table access_log reorganize partition p4 into(
 -> partition s0 values less than(to_days('20190104')),
 -> partition s1 values less than(to_days('20190105'))
 -> );

合并分区

alter table access_log reorganize partition s0,s1 into ( 
 partition p4 values less than (to_days('20190105')) 
);

注意事项

常见问题

alter table access_log partition by range(to_days(access_time))(
partition p1 values less than (to_days('20191202')), 
partition p2 values less than (to_days('20191203')), 
 partition po values less than (maxvalue)
)

看完这篇关于详解MySQL分区表的文章,如果觉得文章内容写得不错的话,可以把它分享出去给更多人看到。

推荐阅读:
  1. MySQL——RDS下的分区表实践
  2. MySQL中分区表是什么意思

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

mysql

上一篇:cat与vim有什么主要区别

下一篇:postgresql数据库不启动命令有哪些

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》