MySql表分区(根据时间datetime)

发布时间:2020-06-28 19:55:31 作者:pannijingling
来源:网络 阅读:2389

timestamp 类型分区请移步=>MySql表分区(根据时间timestamp)
环境:
  MySql8.0.18(5.6和5.7的未验证)
  分区条件的字段类型是datetime
完整的sql操作表分区的语句如下:

-- 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;

我们使用第5步的sql可以查看分区信息
  可以看到2020-01-01之前的有2条信息,2020-01-01至2020-02-01之前的有1条信息,以此类推,最后2020-05-01之后的一共有4条信息

MySql表分区(根据时间datetime)

也可使用Navicat for MySQL工具操作分区:Navicat for MySQL进行表分区操作(图解)
timestamp 类型分区请移步=>MySql表分区(根据时间timestamp)

推荐阅读:
  1. Navicat for MySQL进行表分区操作(图解)
  2. mysql 每日新增表分区

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

mysql dat tim

上一篇:c++单例模式 ---超详细

下一篇:关闭selinux

相关阅读

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

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