I'm trying to create a trigger
that runs the OPTIMIZE command of MySQL.
In short, I have a table A and I created a table B that will write a datetime
where this value will be updated every time the table A receives the OPTIMIZE command.
Trigger would go
I have tried the following code, but MySQL has an error and I do not find the solution.
DELIMITER $$
CREATE TRIGGER optimizechattable AFTER UPDATE ON lz_chat_archive FOR EACH ROW
BEGIN
DECLARE @qtd int = 0;
DECLARE @MAXDATE DATE;
DECLARE @DATAATUAL DATE;
SET @qtd = ( SELECT count(c.time) as qtd FROM lz_chats_table_optimize as c );
SET @MAXDATE = ( SELECT MAX(c.time) FROM lz_chats_table_optimize as c );
SET @DATAATUAL = (SELECT GETDATE());
IF(@quantidade <= 0)
optimize table lz_chat_archive;
/* break execution */
END IF;
/* adaptar para , se diferença for maior que x horas, executar if*/
IF (@MAXDATE > @DATAATUAL)
UPDATE lz_chats_table_optimize as c SET c.time = @DATAATUAL where id = 1
optimize table lz_chat_archive;
END IF;
END $$
DELIMITER;