MySql partition table (based on time datetime)

Partition venue type timestamp => MySql partition table (depending on the time timestamp)
Environment:
  MySql8.0.18 (unverified 5.6 and 5.7)
  field type partition condition is datetime
complete operating table partition sql statement as follows:

-- 1.删除表
drop table t_test;

-- ===================================================================================
-- 2.创建一个表并对其分区,被创建分区的字段必须为主键,或者创建分区时表中没有主键
-- 2.1 方式一:表和分区一起创建
create table t_test (
        id int,
        dates timestamp
)partition by range (unix_timestamp(dates)) (
        -- 小于2018-01-01的
        partition p1 values less than (unix_timestamp('2018-01-01')),
        partition p2 values less than (unix_timestamp('2018-02-01')),
        partition p3 values less than (unix_timestamp('2018-03-01')),
        -- 大于2018-03-01的
        partition p4 values less than maxvalue
);

-- ===================================================================================
-- 2.2 方式二:表和分区分开创建
-- 2.2.1 建表
create table `t_test`  (
  `id` int(11) not null,
  `dates` datetime(0) not null on update current_timestamp(0),
  primary key (`id`, `dates`)
);

-- 3. 修改分区信息
alter table t_test partition by range (to_days(dates)) (
    -- 小于2020-01-01的
    partition p1 values less than (to_days('2020-01-01')),
    partition p2 values less than (to_days('2020-02-01')),
    partition p3 values less than (to_days('2020-03-01')),
    partition p4 values less than (to_days('2020-04-01')),
    -- 大于2020-04-01的
    partition p5 values less than maxvalue
);

-- ===================================================================================
-- 4. 删除并添加新的分区(注意:如果原先最后一个分区是partition pnow values less than maxvalue; 那么应该先删除该分区,然后在执行新增分区语句,然后再新增回该分区)
-- 4.1 删除一个分区(注意:删除一个分区时,该分区内的所有数据也都会被删除;)
alter table t_test drop partition p5;
-- 4.2 新增一个分区
alter table t_test add partition (partition p6 values less than (to_days('2020-05-01')));
-- 4.3 新增一个分区(不满足其余分区条件的都存放在这个分区)
alter table t_test add partition (partition p7 values less than maxvalue);

-- ===================================================================================
-- 5.查询这个表有多少分区
-- 5.1查询每一个分区对应的数量
select
    partition_name part, partition_expression expr, partition_description descr, 
    from_days(partition_description) expirydate, table_rows 
from
    information_schema.`partitions`
where
    table_name='t_test'; 

-- 6.创建测试数据
-- 小于2020-01-01 2条
insert into `t_test` values ('1', '2018-01-02 15:00:00');
insert into `t_test` values ('2', '2019-12-02 15:00:00');
-- 2020-01-01至2020-02-01 1条
insert into `t_test` values ('3', '2020-01-02 16:00:00');
-- 2020-02-01至2020-03-01 2条
insert into `t_test` values ('4', '2020-02-03 15:00:00');
insert into `t_test` values ('5', '2020-02-03 15:00:00');
-- 2020-03-01至2020-04-01 1条
insert into `t_test` values ('6', '2020-03-03 15:00:00');
-- 2020-04-01至2020-05-01 1条
insert into `t_test` values ('7', '2020-04-03 15:00:00');
-- 大于2020-05-01 4条
insert into `t_test` values ('8', '2020-05-03 15:00:00');
insert into `t_test` values ('8', '2020-06-03 15:00:00');
insert into `t_test` values ('8', '2020-06-06 15:00:00');
insert into `t_test` values ('8', '2021-01-01 15:00:00');

-- 6.查询数据
select * from t_test;

We use the first 5 steps sql can view the partition information
  can see there are two information before 2020-01-01, there is an information before 2020-01-01 to 2020-02-01, and so on, and finally 2020 a total of four information after -05-01

MySql partition table (based on time datetime)

You may also be used Navicat for MySQL tool operations Subdivision: Navicat for MySQL partition a table operation (illustrated)
timestamp venue partition type => MySql partition table (based on time timestamp)

Guess you like

Origin blog.51cto.com/1197822/2456558