需求:将老的按”月”存储的数据按照”年月”存储到新的表中(最近12个月)
image.png

将按”月”存储的模拟量导入到新的按照”年月”分表的新数据库中

  1. -- 将按月存储的模拟量导入到新的按照"年月"分表的新数据库中
  2. DROP PROCEDURE IF EXISTS insertAnalogValue2NewTable;
  3. DELIMITER;;
  4. CREATE PROCEDURE insertAnalogValue2NewTable()
  5. BEGIN
  6. DECLARE i INT;
  7. SET i = 0;
  8. WHILE i < 12 DO
  9. -- 存入的表
  10. SET @table_name1 = CONCAT( 'a_analog_value_', date_format( date_sub( curdate()- 1, INTERVAL i MONTH ), '%Y_%m' ));
  11. -- 查询的表
  12. SET @table_name2 = CONCAT( 'a_analog_value_', date_format( date_sub( curdate()- 1, INTERVAL i MONTH ), '%m' ));
  13. SET @sql_where = CONCAT( " FORCE INDEX (createtime) where createtime like '", date_format( date_sub( curdate()- 1, INTERVAL i MONTH ), '%Y-%m' ), "%'");
  14. SET @insert_sql = CONCAT( "INSERT INTO ", @table_name1, " ( `id`, `deviceid`, `value`, `createtime`, `channelno`, `partunitloopcode`, `analog_value_type_id`, `analog_status_id`, `companyid`, `systemaddress` ) select `id`, `deviceid`, `value`, `createtime`, `channelno`, `partunitloopcode`, `analog_value_type_id`, `analog_status_id`, `companyid`, `systemaddress` from ", @table_name2, @sql_where );
  15. PREPARE insert_sql FROM @insert_sql;
  16. EXECUTE insert_sql;
  17. SET i = i + 1;
  18. END WHILE;
  19. END;
  20. ;;
  21. DELIMITER;
  22. CALL insertAnalogValue2NewTable();

创建最近12个月的表

  1. DROP PROCEDURE IF EXISTS createAnalogTable;
  2. DELIMITER ;;
  3. CREATE PROCEDURE createAnalogTable()
  4. BEGIN
  5. declare i int;
  6. set i=0;
  7. while i<12 do
  8. set @table_name = CONCAT('a_analog_value_',date_format(date_sub(curdate()-1, interval i month),'%Y_%m'));
  9. SET @sql_begin='CREATE TABLE ';
  10. SET @sql_end="(`id` int(11) NOT NULL AUTO_INCREMENT,
  11. `deviceid` int(11) NOT NULL,
  12. `value` varchar(20) DEFAULT NULL,
  13. `createtime` datetime DEFAULT NULL,
  14. `systemtime` datetime DEFAULT NULL,
  15. `channelno` varchar(5) DEFAULT NULL,
  16. `partunitloopcode` varchar(10) DEFAULT NULL,
  17. `analog_value_type_id` int(11) DEFAULT NULL,
  18. `analog_status_id` int(11) DEFAULT NULL,
  19. `companyid` int(11) DEFAULT NULL,
  20. `systemaddress` varchar(2) DEFAULT NULL,
  21. PRIMARY KEY (`id`) USING BTREE,
  22. KEY `group_index` (`companyid`,`deviceid`,`partunitloopcode`,`channelno`,`systemaddress`) USING BTREE
  23. ) ENGINE=InnoDB AUTO_INCREMENT=140747 DEFAULT CHARSET=utf8 ROW_FORMAT=COMPACT;";
  24. set @create_sql=CONCAT(@sql_begin,@table_name,@sql_end);
  25. PREPARE create_table from @create_sql;
  26. EXECUTE create_table;
  27. insert into @table_name (`deviceid`, `value`, `createtime`, `channelno`, `partunitloopcode`, `analog_value_type_id`, `analog_status_id`, `companyid`, `systemaddress`)
  28. SELECT `deviceid`, `value`, `createtime`, `channelno`, `partunitloopcode`, `analog_value_type_id`, `analog_status_id`, `companyid`, `systemaddress`
  29. from a_analog_value force index (createtime) where createtime like concat(date_format(date_sub(curdate()-1, interval i month),'%Y_%m'),"%")
  30. set i=i+1;
  31. end while;
  32. END;
  33. ;;
  34. DELIMITER ;
  35. CALL createAnalogTable();