Eventi

Stai visualizzando una vecchia versione di questo article. Visualizza la versione più recente.

Gli Eventi sono oggetti dei database con nome, contenenti istruzioni SQL che devono essere eseguite ad un momento programmato, una sola volta o a intervalli regolari.

Funzionano in maniera molto simile al Task Scheduler di Windows o ai Cron Job di Unix.

Per creare, modificare o eliminare gli eventi occorre il privilegio EVENT.

Creare gli Eventi

Gli eventi si creano con l'istruzione CREATE EVENT.

Esempio

MariaDB [test]> CREATE EVENT test_event ON SCHEDULE EVERY 1 MINUTE DO UPDATE test.t1 SET a = a + 1;

Eseguire gli Eventi

Gli Eventi vengono eseguiti solo se l'Event Scheduler è attivo. Esso dipende dalla variabile di sistema event_scheduler, che deve essere impostata a On se si desidera eseguire gli eventi.

E' possibile controllare se l'Event Scheduler è attivo in questo modo:

MariaDB [test]> SHOW PROCESSLIST;
+----+-----------------+-----------+------+---------+------+-----------------------------+------------------+----------+
| Id | User            | Host      | db   | Command | Time | State                       | Info             | Progress |
+----+-----------------+-----------+------+---------+------+-----------------------------+------------------+----------+
| 40 | root            | localhost | test | Sleep   | 4687 |                             | NULL             |    0.000 |
| 41 | root            | localhost | test | Query   |    0 | init                        | SHOW PROCESSLIST |    0.000 |
| 42 | event_scheduler | localhost | NULL | Daemon  |   30 | Waiting for next activation | NULL             |    0.000 |
+----+-----------------+-----------+------+---------+------+-----------------------------+------------------+----------+

Se l'Event Scheduler non è in funzione e event_scheduler è impostata a OFF, si può usare:

SET GLOBAL event_scheduler = ON;

per attivarla. Se event_scheduler è impostata a Disabled, non è possibile modificarne il valore a runtime. Per modificare il valore della variabile event_scheduler occorre il privilegio SUPER.

Viewing current events

A list of current events can be obtained with the SHOW EVENTS statement. This only shows the event name and interval - the full event details, including the SQL, can be seen with SHOW CREATE EVENT.

Example

MariaDB [test]> SHOW EVENTS\G;
*************************** 1. row ***************************
                  Db: test
                Name: test_event
             Definer: root@localhost
           Time zone: SYSTEM
                Type: RECURRING
          Execute at: NULL
      Interval value: 1
      Interval field: MINUTE
              Starts: 2013-05-20 13:46:56
                Ends: NULL
              Status: ENABLED
          Originator: 1
character_set_client: utf8
collation_connection: utf8_general_ci
  Database Collation: latin1_swedish_ci
MariaDB [test]> SHOW CREATE EVENT test_event\G
*************************** 1. row ***************************
               Event: test_event
            sql_mode: 
           time_zone: SYSTEM
        Create Event: CREATE DEFINER=`root`@`localhost` EVENT `test_event` ON SCHEDULE EVERY 1 MINUTE STARTS '2013-05-20 13:46:56' ON COMPLETION NOT PRESERVE ENABLE DO UPDATE test.t1 SET a = a + 1
character_set_client: utf8
collation_connection: utf8_general_ci
  Database Collation: latin1_swedish_ci

Altering events

An event can be changed with the ALTER EVENT statement.

Example

ALTER EVENT test_event ON SCHEDULE EVERY '2:3' DAY_HOUR;

Dropping events

Events are dropped with the DROP EVENT statement. Events are also also automatically dropped once they have run for the final time according to their schedule.

Example

MariaDB [test]> DROP EVENT test_event;
Query OK, 0 rows affected (0.00 sec)

Commenti

Sto caricando i commenti......
Content reproduced on this site is the property of its respective owners, and this content is not reviewed in advance by MariaDB. The views, information and opinions expressed by this content do not necessarily represent those of MariaDB or any other party.