如何在后台运行mysql存储过程而不创建加载网站以等待过程结果完成?
DELIMITER $$
USE `simbakda_sensus`$$
DROP PROCEDURE IF EXISTS `info`$$
CREATE DEFINER=`potutu`@`%` PROCEDURE `info`(unit VARCHAR(20))
BEGIN
DELETE FROM info WHERE kd_unit=unit;
INSERT INTO info
SELECT unit,'a' AS kib ,SUM(a) AS a_sen,SUM(b) AS a_tot,(SUM(b)-SUM(a)) AS a_non FROM (
SELECT unit,COUNT(*) AS a,'0' AS b FROM trkib_a WHERE kd_unit=unit AND STATUS='1'
UNION ALL
SELECT unit,'0' AS a, COUNT(*) AS b FROM trkib_a WHERE kd_unit=unit
)a
UNION ALL
SELECT unit,'b' AS kib ,SUM(a) AS a_sen,SUM(b) AS a_tot,(SUM(b)-SUM(a)) AS a_non FROM (
SELECT unit,COUNT(*) AS a,'0' AS b FROM trkib_b WHERE kd_unit=unit AND STATUS='1'
UNION ALL
SELECT unit,'0' AS a, COUNT(*) AS b FROM trkib_b WHERE kd_unit=unit
)b
UNION ALL
SELECT unit,'c' AS kib ,SUM(a) AS a_sen,SUM(b) AS a_tot,(SUM(b)-SUM(a)) AS a_non FROM (
SELECT unit,COUNT(*) AS a,'0' AS b FROM trkib_c WHERE kd_unit=unit AND STATUS='1'
UNION ALL
SELECT unit,'0' AS a, COUNT(*) AS b FROM trkib_c WHERE kd_unit=unit
)c
UNION ALL
SELECT unit,'d' AS kib ,SUM(a) AS a_sen,SUM(b) AS a_tot,(SUM(b)-SUM(a)) AS a_non FROM (
SELECT unit,COUNT(*) AS a,'0' AS b FROM trkib_d WHERE kd_unit=unit AND STATUS='1'
UNION ALL
SELECT unit,'0' AS a, COUNT(*) AS b FROM trkib_d WHERE kd_unit=unit
)d
UNION ALL
SELECT unit,'e' AS kib ,SUM(a) AS a_sen,SUM(b) AS a_tot,(SUM(b)-SUM(a)) AS a_non FROM (
SELECT unit,COUNT(*) AS a,'0' AS b FROM trkib_e WHERE kd_unit=unit AND STATUS='1'
UNION ALL
SELECT unit,'0' AS a, COUNT(*) AS b FROM trkib_e WHERE kd_unit=unit
)e
UNION ALL
SELECT unit,'f' AS kib ,SUM(a) AS a_sen,SUM(b) AS a_tot,(SUM(b)-SUM(a)) AS a_non FROM (
SELECT unit,COUNT(*) AS a,'0' AS b FROM trkib_f WHERE kd_unit=unit AND STATUS='1'
UNION ALL
SELECT unit,'0' AS a, COUNT(*) AS b FROM trkib_f WHERE kd_unit=unit
)f;
END$$
DELIMITER ;
答案 0 :(得分:1)
您有一些不错的选择。
如果您需要定期运行(例如每小时或每个星期日的03:00,等等),请使用MySQL Event。 (请注意,每月5美元的共享托管服务中的某些服务不允许在其MySQL服务器上使用事件。)
启用事件计划程序:
SET GLOBAL event_scheduler = ON;
然后创建事件对象。它会存在于您的MySQL数据库中,并按照您指定的时间表运行。
CREATE EVENT myevent_01
ON SCHEDULE EVERY 1 HOUR
STARTS CURRENT_TIMESTAMP
DO CALL simbakda_sensus.info;
另一种选择是一次性活动。同样的交易,但它运行一次就消失了。每次要运行过程时,都必须重新创建事件。这是过程的纯“后台”执行。
CREATE EVENT run_info_now
ON SCHEDULE AT CURRENT_TIMESTAMP
DO CALL simbakda_sensus.info;
第三种方式:
创建一个小的php页面来运行您的过程。然后将iframe放入您的主网页,并从中调用小页面。您可以使小页面显示“信息已完成!”或sp结束运行时显示的其他消息。
第四种方式。
某些linux / FreeBSD / Unix服务器允许用户设置称为cron作业的定期计划事件。大话题:在这里阅读。 How to create cron job using PHP?
专业提示:创建日志表。使每个数据维护过程在运行时在表中插入一行,以显示当前时间和有关该过程的其他信息。我向您保证,如果您的系统被大量使用,您将不时查看此日志以确保一切正常运行。