2024-01-15 11:31:03 +00:00
|
|
|
DELIMITER $$
|
|
|
|
CREATE OR REPLACE DEFINER=`root`@`localhost` PROCEDURE `util`.`slowLog_prune`()
|
|
|
|
BEGIN
|
|
|
|
/**
|
|
|
|
* Prunes MySQL slow query log table deleting all records older than one week.
|
|
|
|
*/
|
|
|
|
DECLARE vSlowQueryLog INT DEFAULT @@slow_query_log;
|
|
|
|
DECLARE vSqlLogBin INT DEFAULT @@SESSION.sql_log_bin;
|
2024-04-12 07:21:34 +00:00
|
|
|
DECLARE vLogExists BOOL;
|
2024-01-15 11:31:03 +00:00
|
|
|
|
|
|
|
SET sql_log_bin = OFF;
|
|
|
|
SET GLOBAL slow_query_log = OFF;
|
|
|
|
|
2024-04-12 07:24:48 +00:00
|
|
|
SELECT COUNT(*) > 0 INTO vLogExists
|
2024-04-12 07:21:34 +00:00
|
|
|
FROM information_schema.TABLES
|
|
|
|
WHERE TABLE_SCHEMA = 'mysql' AND TABLE_NAME = 'slow_log';
|
|
|
|
|
|
|
|
IF vLogExists THEN
|
2024-04-12 07:24:48 +00:00
|
|
|
DROP TEMPORARY TABLE IF EXISTS mysql.slow_log_temp;
|
|
|
|
RENAME TABLE mysql.slow_log TO mysql.slow_log_temp;
|
2024-04-12 07:21:34 +00:00
|
|
|
END IF;
|
2024-01-15 11:31:03 +00:00
|
|
|
|
2024-04-12 07:24:48 +00:00
|
|
|
DELETE FROM mysql.slow_log_temp
|
2024-01-15 11:31:03 +00:00
|
|
|
WHERE start_time < TIMESTAMPADD(WEEK, -1, util.VN_NOW());
|
|
|
|
|
2024-04-12 07:24:48 +00:00
|
|
|
RENAME TABLE mysql.slow_log_temp TO mysql.slow_log;
|
2024-01-15 11:31:03 +00:00
|
|
|
|
|
|
|
SET GLOBAL slow_query_log = vSlowQueryLog;
|
|
|
|
SET sql_log_bin = vSqlLogBin;
|
|
|
|
END$$
|
|
|
|
DELIMITER ;
|