在数据库中调用存储过程进行分区,包括分区初始化和分区自动递增

CREATE DEFIdata_value_table_partition_initNER=`root`@`localhost` PROCEDURE `table_partition_init`()
BEGIN
      set @number=(SELECT count(*) partition_name FROM information_schema.partitions where table_schema='数据库名' and   table_name = '表名' and partition_name LIKE '%p%'); 

    if (@number=0) then

         SET @today_partition_name = (SELECT concat('p',DATE_FORMAT(DATE_ADD(curdate()-day(curdate())+1,interval 1 month),'%Y%m%d')));
     SET @today_no = (select DATE_ADD(curdate()-day(curdate())+1,interval 1 month) );
     
     SET @test_init_partition_sql=CONCAT('ALTER TABLE 表名 partition by range columns(date_time)( partition ',@today_partition_name,' values less than(\'',@today_no,'\'));');
         PREPARE stmt FROM @test_init_partition_sql;
         EXECUTE stmt;
        end if;
END

CREATE DEFINER=`root`@`localhost` PROCEDURE `table_create_partition`()
L_END:
begin     
    declare max_partition_description varchar(255) default '';
    declare p_name varchar(255) default 0;       
    declare p_description varchar(255) default 0;   
    declare isexist_partition varchar(255) default 0; 
        declare i int default 1;
        declare partition_sum int default 1;
        declare scada_db varchar(50) default 'scada_db';
        declare data_value_table varchar(50)  default 表名';
        declare partSQL varchar(255);
        declare sql1 MediumText;
    
     
        
    select partition_name into isexist_partition from information_schema.partitions where table_schema = 数据库名 and table_name = 表名 limit 1;
    
    if isexist_partition <=> "" then
       select "partition table not is exist" as "ERROR";
       leave L_END;
    end if;
 
    
    select partition_description into max_partition_description  from information_schema.partitions where table_schema = 数据库名 and table_name = 表名 order by partition_description desc limit 1;
   
    
    if max_partition_description <=> "" then
       select "partition table is error" as "ERROR";
       leave L_END;
    end if;
    
    
        
        set max_partition_description = REPLACE(max_partition_description, '\'', '');
    
    
      
      
label:BEGIN
 
              set @data_value_table_number=(SELECT  COUNT(*) AS CNT FROM information_schema.PARTITIONS WHERE     TABLE_NAME = '表名'  AND       TABLE_SCHEMA = '数据库'); 
    
        if (@data_value_table_number>=360) then
      
       leave label;
    end if;
        


        
        set partSQL = CONCAT('ALTER TABLE ', 表名 ,' ADD PARTITION ( ');
        set sql1 = '';
    
        while (i < 360) do
             set p_description = DATE_ADD(max_partition_description, interval i*1 month); 
             
             set p_name = REPLACE(p_description, ' ', '');
                        set p_name = REPLACE(p_name, '-', '');
             set p_name = REPLACE(p_name, ':', '');
             
             if i > 1 then
                    set sql1 = CONCAT(sql1,',');
             end if;
             
             set sql1 = CONCAT(sql1, 'PARTITION p', p_name ,' VALUES LESS THAN (\'', p_description ,'\')');  
             
             set i = (i + 1) ;
     end while ;

        set @sql = concat(partSQL, sql1, ');');
        PREPARE stmt from @sql;
        EXECUTE stmt;
        DEALLOCATE PREPARE stmt;

END label;
                         
end

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值