Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- DELIMITER ;;
- CREATE PROCEDURE `FixTable`(IN `TableName` VARCHAR (400), IN `FixLevel` INT)
- BEGIN
- declare done int default 0;
- DECLARE CONTINUE HANDLER FOR SQLSTATE '02000' SET done = 1;
- CASE FixLevel
- WHEN 1 THEN SET @SQLText = CONCAT('REPAIR TABLE `',TableName, '` QUICK');
- WHEN 2 THEN SET @SQLText = CONCAT('REPAIR TABLE `',TableName, '` EXTENDED');
- WHEN 3 THEN SET @SQLText = CONCAT('REPAIR TABLE `',TableName, '` USE_FRM');
- WHEN 4 THEN SET @SQLText = CONCAT('OPTIMIZE LOCAL TABLE `',TableName, '` ');
- END CASE;
- PREPARE RES from @SQLText;
- EXECUTE RES;
- END ;;
- DELIMITER ;
- DELIMITER ;;
- CREATE PROCEDURE `fixAllTables`(IN `FixLevel` INT)
- begin
- DECLARE done INT DEFAULT 0;
- declare TableName VARCHAR(30);
- DECLARE tablestatusCur CURSOR FOR
- SELECT TABLE_NAME FROM information_schema.`TABLES` T
- where TABLE_SCHEMA=SCHEMA() and Table_comment not like '%VIEW%' and Table_ROWS is null;
- DECLARE CONTINUE HANDLER FOR NOT FOUND SET done = 1;
- open tablestatusCur;
- repeat
- FETCH tablestatusCur INTO TableName;
- if not done then
- call FixTable(TableName,FixLevel);
- end if;
- until done end repeat;
- end ;;
- DELIMITER ;
- DELIMITER ;;
- CREATE EVENT `fixTablesHourly`
- ON SCHEDULE EVERY 1 HOUR STARTS CURRENT_TIMESTAMP
- ON COMPLETION NOT PRESERVE
- ENABLE
- COMMENT 'Check for damaged tables and fix them.'
- DO BEGIN
- DECLARE CONTINUE HANDLER FOR SQLSTATE '02000' SET @LastEventErrorTime=CURRENT_TIMESTAMP;
- call fixAllTables(1);
- call fixAllTables(2);
- call fixAllTables(3);
- END ;;
- DELIMITER ;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement