Silentdoer

导航

分区表创建和查询

create table sign_in_log(unid BIGINT(20) NOT NULL,
                         create_time datetime not null)engine = innodb default charset=utf8 partition by range columns (create_time)(
                             PARTITION P_20220623
                             VALUES LESS THAN ('2022-06-24 00:00:00') ENGINE=INNODB,
                             PARTITION P_20220624
                             VALUES LESS THAN ('2022-06-25 00:00:00') ENGINE = INNODB
                             );

注意,分区表其实是需要每天进行创建的,可以写个定时任务,但是可以一开始先创建几天的,然后后面每天定时任务创建一天的,这里是根据create_time字段来创建分区;

上面的sql就是创建分区表模板的同时创建了表分区:P_20220623和P_20220624;

后续要继续追加创建表分区则可以直接:alter table sign_in_log add partition (partition P_20220625 values less than ('2022-06-26 00:00:00'));

 

可以通过来查询最近5天的分区以及每个分区当前的数据量:

select PARTITION_NAME AS '分区名', TABLE_ROWS AS '表该分区行数' from information_schema.partitions where table_schema = "db_test" and table_name = "sigin_in_log" order by PARTITION_NAME desc limit 5

 

posted on 2022-06-23 14:24  Silentdoer  阅读(245)  评论(0编辑  收藏  举报