Mysql分区 按日期与按日进行分区

一,按天数进行分区,需要提到的是(每种分区方式,皆有一些限制,比如类型,比如索引问题会导致其它的SQL语句应该走的路径,导致出现偏差,或者主动进行忽略,按照索引策略进行查询优化!)
create table test_par(
id int not null,
date01 date default ‘0000-00-00’ comment ‘日期’
PRIMARY KEY(id,date01) comment ‘必须将分区字段跟主键放在一起’
)ENGINE=Innodb Default CHARSET = utf8
PARTITION by RANGE(TO_DAYS(date01))
(
PARTITION po values less than (to_days(‘2020-02-28’)),
PARTITION p1 values less than(to_days(‘2020-02-29’)),
PARTITION p2 values less than(maxvalue) engine = innodb)

二,按照日期进行分区
1,日期组合分区
写法如下
PARTITION by RANGE COLUMS(date,date_timestamp)
(
PARTITION po values less than (‘2019-01-01’,‘1546272000’),
PARTITION p1 values less than(to_days(‘2019-02-01’,‘154898950400’),
PARTITION p2 values less than(maxvalue,MAXVALUE) engine = innodb)
注意:第一个是日期,第二个是转换为timestamp的日期时间

  2,日期分区
                      PARTITION by RANGE COLUMS(date)
        (
        PARTITION po values less than ('2019-01-01'),
        PARTITION p1 values less than('2019-02-01'),
        PARTITION p2 values less than(maxvalue) engine = innodb)
  3,根据月份并且将月份划分为4个分区
  PARTITION BY RANGE(yearweek(date))
  SUBPARTITION BY HASH (yearweek(date))
  SUBPARTITIONS 4(
        PARTITION po values less than (yearweek('2019-01-01')),
        PARTITION p1 values less than(yearweek('2019-02-01')),
        PARTITION p2 values less than(maxvalue) engine = innodb)
  )

你可能感兴趣的:(Mysql记录篇)