现在有一张machine_status_active表machine_status_tiny表两张表结构相同有一个创建时间字段写一个Mysql存储过程参数为创建时间实现将machine_status_active中时间范围内的数据插入machine_status_tiny 要求每次插入10W条
你可以使用以下的MySQL存储过程来实现将machine_status_active中时间范围内的数据插入machine_status_tiny,并且每次插入10W条数据:
DELIMITER //
CREATE PROCEDURE insert_data_to_tiny(IN start_time DATETIME, IN end_time DATETIME)
BEGIN
DECLARE total_rows INT;
DECLARE current_rows INT;
DECLARE batch_size INT DEFAULT 100000;
SELECT COUNT(*) INTO total_rows FROM machine_status_active WHERE created_time BETWEEN start_time AND end_time;
SET current_rows = 0;
WHILE current_rows < total_rows DO
INSERT INTO machine_status_tiny
SELECT *
FROM machine_status_active
WHERE created_time BETWEEN start_time AND end_time
LIMIT current_rows, batch_size;
SET current_rows = current_rows + batch_size;
END WHILE;
END //
DELIMITER ;
使用方法:
- 将上述存储过程复制到MySQL客户端中执行,创建存储过程。
- 调用存储过程,传入开始时间和结束时间参数,例如:
CALL insert_data_to_tiny('2022-01-01 00:00:00', '2022-01-02 00:00:00');
这将会将machine_status_active表中2022年1月1日的数据插入到machine_status_tiny表中,并且每次插入10W条数据,直到所有符合条件的数据都被插入到machine_status_tiny表中
原文地址: https://www.cveoy.top/t/topic/iDrI 著作权归作者所有。请勿转载和采集!