0、整理表空间碎片
optimize table tablename
1、表分区按年分区,季度子分区
alter table key_part partition by range(year(create_time)) subpartition by hash(quarter(create_time)) subpartitions 4 ( partition p0 values less than (2015), partition p2015 values less than (2016), partition p2016 values less than (2017), partition p2017 values less than (2018), partition p2018 values less than (2019), partition p1 values less than maxvalue );
总共产生24个分区,1年4个季度,6年24个季度。
2、按照天分区月表
create_time支持如下日期格式:
%Y-%c-%d
%Y-%c-%d %h:%i:%s
alter table aa partition by range (to_days(create_time)) ( partition p01 values less than (to_days(‘2018-04-01‘)) engine = innodb, partition p02 values less than (to_days(‘2018-04-02‘)) engine = innodb, partition p03 values less than (to_days(‘2018-04-03‘)) engine = innodb, partition p04 values less than (to_days(‘2018-04-04‘)) engine = innodb, partition p05 values less than (to_days(‘2018-04-05‘)) engine = innodb, partition p06 values less than (to_days(‘2018-04-06‘)) engine = innodb, partition p07 values less than (to_days(‘2018-04-07‘)) engine = innodb, partition p08 values less than (to_days(‘2018-04-08‘)) engine = innodb, partition p09 values less than (to_days(‘2018-04-09‘)) engine = innodb, partition p10 values less than (to_days(‘2018-04-10‘)) engine = innodb, partition p11 values less than (to_days(‘2018-04-11‘)) engine = innodb, partition p12 values less than (to_days(‘2018-04-12‘)) engine = innodb, partition p13 values less than (to_days(‘2018-04-13‘)) engine = innodb, partition p14 values less than (to_days(‘2018-04-14‘)) engine = innodb, partition p15 values less than (to_days(‘2018-04-15‘)) engine = innodb, partition p16 values less than (to_days(‘2018-04-16‘)) engine = innodb, partition p17 values less than (to_days(‘2018-04-17‘)) engine = innodb, partition p18 values less than (to_days(‘2018-04-18‘)) engine = innodb, partition p19 values less than (to_days(‘2018-04-19‘)) engine = innodb, partition p20 values less than (to_days(‘2018-04-20‘)) engine = innodb, partition p21 values less than (to_days(‘2018-04-21‘)) engine = innodb, partition p22 values less than (to_days(‘2018-04-22‘)) engine = innodb, partition p23 values less than (to_days(‘2018-04-23‘)) engine = innodb, partition p24 values less than (to_days(‘2018-04-24‘)) engine = innodb, partition p25 values less than (to_days(‘2018-04-25‘)) engine = innodb, partition p26 values less than (to_days(‘2018-04-26‘)) engine = innodb, partition p27 values less than (to_days(‘2018-04-27‘)) engine = innodb, partition p28 values less than (to_days(‘2018-04-28‘)) engine = innodb, partition p29 values less than (to_days(‘2018-04-29‘)) engine = innodb, partition p30 values less than (to_days(‘2018-04-30‘)) engine = innodb, partition p31 values less than MAXVALUE engine = innodb );
explain partitions select * from key_part where create_time>=‘2018-04-12‘ and create_time<=‘2018-04-15‘
可以看到只遍历了4个分区表,只扫描了4行,而不是扫描所有的行。
explain partitions select * from key_part where create_time>=‘2018-04-27‘
可以看到只遍历了5个分区表,只扫描了5行,而不是扫描所有的行。
原文地址:https://www.cnblogs.com/linjiqin/p/9127012.html
时间: 2024-11-11 11:37:23