我正在尝试简化某些任务,我相信存储过程实际上是一种更快的方法,而不是生成一些mysqlclient进程的bash脚本
我尝试了一些python脚本和一些其他bash脚本,它们都可以正常工作,但是我很好奇带有一些事件触发器的存储过程是否更好
注意:这将应用于约700个与模式相同的数据库,并且可以同时运行,也可以通过脚本顺序运行(一些建议也可以。如果它更适合尝试使用python id进行某种多线程处理,例如知道
USE `<db>`;
DROP procedure IF EXISTS `archive_table`;
DELIMITER //
USE `basegps3`//
CREATE PROCEDURE `archive_table`(IN trunc_date DATETIME)
BEGIN
DECLARE max_loc_id INT DEFAULT 1;
SELECT IFNULL(MAX(location_id),1) INTO max_loc_id FROM location;
INSERT IGNORE INTO arc_location SELECT * FROM location WHERE update_time < trunc_date AND location_id != max_loc_id;
DELETE FROM location WHERE update_time < trunc_date AND location_id != max_loc_id;
INSERT IGNORE INTO location SELECT l.* FROM arc_location l INNER JOIN vehicle_info USING(location_id);
DELETE FROM arc_location WHERE location_id IN (SELECT location_id FROM vehicle_info);
INSERT IGNORE INTO arc_message SELECT * FROM message WHERE create_time < trunc_date;
DELETE FROM message WHERE create_time < trunc_date;
INSERT IGNORE INTO arc_qt_answer SELECT * FROM qt_answer WHERE date_time < trunc_date;
INSERT IGNORE INTO arc_phone_error SELECT * FROM phone_error WHERE timestamp < trunc_date;
DELETE FROM phone_error WHERE timestamp < trunc_date;
INSERT IGNORE INTO arc_status_change SELECT * FROM status_change WHERE timestamp < trunc_date AND most_recent = 0;
DELETE FROM status_change WHERE timestamp < trunc_date and most_recent = 0;
INSERT IGNORE INTO arc_area_points SELECT * FROM area_points WHERE area_id IN (SELECT area_id FROM area WHERE expire_time IS NOT NULL AND expire_time < trunc_date AND area_type_id = 1);
DELETE FROM area_points WHERE area_id IN (SELECT area_id FROM area WHERE expire_time IS NOT NULL AND expire_time < trunc_date AND area_type_id = 1);
INSERT IGNORE INTO arc_hotspot SELECT * FROM hotspot WHERE area_id IN (SELECT area_id FROM area WHERE expire_time IS NOT NULL AND expire_time < trunc_date AND area_type_id = 1);
DELETE FROM hotspot WHERE area_id IN (SELECT area_id FROM area WHERE expire_time IS NOT NULL AND expire_time < trunc_date AND area_type_id = 1);
INSERT IGNORE INTO arc_area SELECT * FROM area WHERE expire_time IS NOT NULL AND expire_time < trunc_date AND area_type_id = 1;
DELETE FROM area WHERE expire_time IS NOT NULL AND expire_time < trunc_date AND area_type_id = 1;
INSERT IGNORE INTO arc_area_time SELECT * FROM area_time WHERE exit_timestamp IS NOT NULL AND exit_timestamp < trunc_date;
DELETE FROM area_time WHERE exit_timestamp IS NOT NULL AND exit_timestamp < trunc_date;
INSERT IGNORE INTO arc_ed_sample_data SELECT * FROM ed_sample_data WHERE timestamp < trunc_date;
DELETE FROM ed_sample_data WHERE timestamp < trunc_date;
END//
DELIMITER ;```