Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MySQL trigger/procedure execution delay

Is there a decent way to delay execution of mysql trigger?

WHILE @condition = 0
  sleep for awhile

insert into some_table values(NEW.value1, NEW.value2);
like image 680
Ragnar Avatar asked Jun 04 '09 11:06

Ragnar


2 Answers

Since MySQL 5.0.12, you can do this:

SELECT SLEEP(<seconds>);

The seconds parameter can be in a fraction of a second like .5.

like image 149
Jonathan Campbell Avatar answered Oct 10 '22 18:10

Jonathan Campbell


DO SLEEP(<seconds>);

is better. There is no reason to just run SELECT statements inside triggers without needing the result set. If you really want to do this you need to do it like here:

SET @nothing = (SELECT SLEEP(<seconds>));

But I recommend to use DO. And don't forget that a trigger is just a single statement per default. If you have more then 1 statement in your trigger you need to use BEGIN/END:

BEGIN
    DO SLEEP(<seconds>);
    UPDATE ...;
END
like image 23
fnkr Avatar answered Oct 10 '22 18:10

fnkr