现在有一张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 days INT)
BEGIN
DECLARE total_count INT;
DECLARE current_count INT;
DECLARE start_date DATE;
DECLARE end_date DATE;
SET total_count = (SELECT COUNT(*) FROM machine_status_active WHERE created_at >= CURDATE() - INTERVAL days DAY);
SET current_count = 0;
SET start_date = CURDATE() - INTERVAL days DAY;
SET end_date = CURDATE();
WHILE current_count < total_count DO
INSERT INTO machine_status_tiny
SELECT *
FROM machine_status_active
WHERE created_at >= start_date AND created_at < end_date
LIMIT 100000;
SET current_count = current_count + ROW_COUNT();
END WHILE;
END //
DELIMITER ;
使用方法:
- 将上述存储过程代码复制到MySQL客户端中,执行代码创建存储过程。
- 调用存储过程,并传入天数参数,例如:
CALL insert_data_to_tiny(7);,这将提取过去7天内的数据并插入到machine_status_tiny表中,每次插入10W条数据,直到所有符合条件的数据都被插入为止。
请注意,以上存储过程仅作为示例提供,具体根据实际需求进行调整
原文地址: https://www.cveoy.top/t/topic/iDrJ 著作权归作者所有。请勿转载和采集!