首页 > 数据库 >mysql表分区存储过程

mysql表分区存储过程

时间:2023-02-21 11:02:23浏览次数:38  
标签:存储 SET 分区 partition TABLENAME DAY INTERVAL mysql DECLARE

本文为博主原创,未经允许不得转载:

  由于数据库一张表数据量有几千万条,而且在不断增长,看见公司前辈写了一个创建表分区的存储过程,感觉

甚是牛逼,在此供自己保留学习。

/*PROCEDURE create_partition_by_month*/
DROP PROCEDURE IF EXISTS `create_partition_by_month`;
DELIMITER $$
CREATE PROCEDURE `create_partition_by_month`(IN_TABLENAME VARCHAR(64))
BEGIN
DECLARE ROWS_CNT INT UNSIGNED;
DECLARE EXPIRE_ROWS_CNT INT UNSIGNED;
DECLARE BEGINTIME TIMESTAMP;
DECLARE ENDTIME TIMESTAMP;
DECLARE PARTITIONNAME VARCHAR(16);
DECLARE PARTITIONRANGE INT UNSIGNED;
DECLARE EXPIRETIME TIMESTAMP;
DECLARE EXPIREPARTITION VARCHAR(16);

SET BEGINTIME = DATE(NOW() - INTERVAL DAY(NOW()) DAY + INTERVAL 1 DAY + INTERVAL 1 MONTH);
SET PARTITIONNAME = DATE_FORMAT( BEGINTIME, 'p%Y%m' );
SET ENDTIME = DATE(NOW() - INTERVAL DAY(NOW()) DAY + INTERVAL 1 DAY + INTERVAL 2 MONTH);
SET PARTITIONRANGE = extract(YEAR_MONTH FROM ENDTIME);
SET EXPIRETIME = DATE(NOW() - INTERVAL 3 YEAR);
SET EXPIREPARTITION = DATE_FORMAT( EXPIRETIME, 'p%Y%m' );

SELECT COUNT(*) INTO ROWS_CNT FROM information_schema.partitions WHERE table_name = IN_TABLENAME AND partition_name = PARTITIONNAME;
IF ROWS_CNT = 0 THEN
SET @SQL = CONCAT( 'ALTER TABLE `', IN_TABLENAME, '`',
' ADD PARTITION (PARTITION ', PARTITIONNAME, ' VALUES LESS THAN (', PARTITIONRANGE ,') ENGINE = InnoDB);' );
PREPARE STMT FROM @SQL;
EXECUTE STMT;
DEALLOCATE PREPARE STMT;
ELSE
SELECT CONCAT("partition `", PARTITIONNAME, "` for table `", IN_TABLENAME, "` already exists") AS result;
END IF;

SELECT COUNT(*) INTO EXPIRE_ROWS_CNT FROM information_schema.partitions WHERE table_name = IN_TABLENAME AND partition_name = EXPIREPARTITION;
IF EXPIRE_ROWS_CNT = 0 THEN
SELECT CONCAT("partition `", EXPIREPARTITION, "` for table `", IN_TABLENAME, "` not exists") AS result;
ELSE
SET @SQL = CONCAT( 'ALTER TABLE `', IN_TABLENAME, '`', ' DROP PARTITION ', PARTITIONNAME, ';' );
PREPARE STMT FROM @SQL;
EXECUTE STMT;
DEALLOCATE PREPARE STMT;
END IF;

END$$
DELIMITER ;
/*EVENT e_alarm_partition*/
DROP EVENT IF EXISTS `e_alarm_partition`;
DELIMITER $$
CREATE EVENT IF NOT EXISTS `e_alarm_partition`
ON SCHEDULE EVERY 1 DAY #执行周期,还有天、月等等
STARTS '2017-12-23 04:00:00'
ON COMPLETION PRESERVE
ENABLE
COMMENT 'Creating partitions'
DO BEGIN
CALL create_partition_by_month('tbl_alarminfo');
CALL create_partition_by_month('tbl_user_log');
END$$
DELIMITER ;

SET GLOBAL group_concat_max_len=102400;
SET SESSION group_concat_max_len=102400;
tbl_alarminfo为告警信息表,tbl_user_log为日志操作记录表



标签:存储,SET,分区,partition,TABLENAME,DAY,INTERVAL,mysql,DECLARE
From: https://blog.51cto.com/u_15535797/6076091

相关文章

  • Python--字典底层存储、补充:类型对象、函数传参
    补充#python字典底层存储https://www.cnblogs.com/xiaoyuanqujing/articles/12008689.html#Python中数据类型都是对象都是地址引用,不存在值类型,都是......
  • mysql8.0的可用方案
    1.在/use/local下创建mysql文件夹mkdirmysql2.切换到mysql文件夹下cdmysql3.下载mysqlwgethttps://dev.mysql.com/get/Downloads/MySQL-8.0/mysql-8.0.20-linux-......
  • division by zero引发的存储过程假运行
    在存储过程中有用到除法去得到数据的,并且除数字段一定有0值的存在才导致的错误。导致的现象就是存储过程运行看似没有问题,成功执行,但是实际看表数据量却是没有数据的,(还有......
  • 07-存储服务测试(Ceph Pacific)
    1.块存储服务测试1.1查看镜像列表rbdlist1.2映射镜像格式:rbddevicemap{pool-name}/{image-name}--id{user-name}示例:rbddevicemaprbd/myimage--idadmin......
  • Linux中做逻辑分区,分区扩容操作
    1.lvm删除先用umount卸载逻辑卷,再用lvremove删逻辑卷,vgremove删卷组,最后用pvremove删物理卷。2.lvm创建  http://www.linuxidc.com/Linux/2010-08/28228.htm1.新......
  • 基于 springboot + mybatis-plus + MySQL 重构过去一个项目的踩坑总结(持续更新)
    ①使用mybatis-plus时,如果根据某个条件查询没有查到数据,那么返回的实体类是null,如果使用get方法就会导致空指针异常,这点要规避②如果Map初始化在循环体外,那么在循......
  • Mysql自增ID用完了会咋样?
    这个问题其实可以分为有主键&无主键两种情况回答。国际惯例,先上张脑图:02有主键如果你的表有主键,并且把主键设置为自增。在MySQL中,一般会把主键设置成int型。而MySQ......
  • mysql触发器对于null值的处理
    mysql的触发器是不会对null值进行直接监控的,需要手动对于null值做处理这样处理null值就可以NEW.prom_priceisnotnullandOLD.prom_priceisnull......
  • Mysql的版本号实现可重复读
    mysql基于乐观锁原理实现的mvcc(Multi-VersionConcurrencyControl,多版本并发控制)mysql默认隔离级别就是可重复读,这个隔离级别解决了不可重复读和脏读,所谓不可重复读就是......
  • 最新JAVA实现导出mysql表结构到Word
    JAVA实现导出mysql表结构到Word1.引入jar包<dependency><groupId>org.apache.poi</groupId><artifactId>poi-ooxml</artifactId>......