首页 > 数据库 > mysql

MySQL分区表的基本入门教程

发布时间:2020-6-1 08:29

前言

在最近的项目中,我们需要保存大量的数据,而且这些数据是有有效期的,为了提供查询效率以及快速删除过期数据,我们选择了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文件


分区表

新增分区

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分区表基本入门教程的文章就介绍到这了,更多相关MySQL分区表内容请搜索猪先飞以前的文章或继续浏览下面的相关文章希望大家以后多多支持猪先飞!

标签:[!--infotagslink--]

您可能感兴趣的文章: