Mysql
 SQL >> Database >  >> RDS >> Mysql

Attivare un UPDATE MySQL solo quando la riga cambia davvero

Come soluzione alternativa, puoi confrontare il timestamp (vecchio vs nuovo), che non viene aggiornato quando nulla nella riga cambia. (Questa può essere la fonte della confusione: si chiama anch'esso 'on update' ma non si attiva quando non c'è alcuna modifica.) Le modifiche entro lo stesso secondo non eseguiranno quella parte del trigger, ma in alcuni casi è del tutto accettabile, ad esempio quando la tua applicazione rifiuta già le modifiche troppo rapide.

Ad esempio, invece di

IF NEW.a <> OLD.a or NEW.b <> OLD.b /* etc, all the way to NEW.z <> OLD.z */ THEN INSERT INTO bar (a, b) VALUES(NEW.a, NEW.b) ; END IF 

potresti scrivere

IF NEW.ts <> OLD.ts THEN INSERT INTO bar (a, b) VALUES(NEW.a, NEW.b) ; END IF 

Poi non devi modificare il trigger ogni volta che lo schema cambia, che è il problema sollevato nella domanda.

MODIFICA: Aggiunto esempio completo

create table foo (a INT, b INT, ts TIMESTAMP); create table bar (a INT, b INT); INSERT INTO foo (a,b) VALUES(1,1); INSERT INTO foo (a,b) VALUES(2,2); INSERT INTO foo (a,b) VALUES(3,3); DELIMITER /// CREATE TRIGGER ins_sum AFTER UPDATE ON foo FOR EACH ROW BEGIN IF NEW.ts <> OLD.ts THEN INSERT INTO bar (a, b) VALUES(NEW.a, NEW.b); END IF; END; /// DELIMITER ; select * from foo; +------+------+---------------------+ | a | b | ts | +------+------+---------------------+ | 1 | 1 | 2011-06-14 09:29:46 | | 2 | 2 | 2011-06-14 09:29:46 | | 3 | 3 | 2011-06-14 09:29:46 | +------+------+---------------------+ 3 rows in set (0.00 sec) -- UPDATE without change UPDATE foo SET b = 3 WHERE a = 3; Query OK, 0 rows affected (0.00 sec) Rows matched: 1 Changed: 0 Warnings: 0 -- the timestamo didnt change select * from foo WHERE a = 3; +------+------+---------------------+ | a | b | ts | +------+------+---------------------+ | 3 | 3 | 2011-06-14 09:29:46 | +------+------+---------------------+ 1 rows in set (0.00 sec) -- the trigger didn't run select * from bar; Empty set (0.00 sec) -- UPDATE with change UPDATE foo SET b = 4 WHERE a=3; Query OK, 1 row affected (0.00 sec) Rows matched: 1 Changed: 1 Warnings: 0 -- the timestamp changed select * from foo; +------+------+---------------------+ | a | b | ts | +------+------+---------------------+ | 1 | 1 | 2011-06-14 09:29:46 | | 2 | 2 | 2011-06-14 09:29:46 | | 3 | 4 | 2011-06-14 09:34:59 | +------+------+---------------------+ 3 rows in set (0.00 sec) -- and the trigger ran select * from bar; +------+------+---------------------+ | a | b | ts | +------+------+---------------------+ | 3 | 4 | 2011-06-14 09:34:59 | +------+------+---------------------+ 1 row in set (0.00 sec) 

Funziona grazie alla gestione dei timestamp di MySQL: il timestamp si aggiorna solo quando avviene una modifica effettiva.

La documentazione è qui:
https://dev.mysql.com/doc/refman/5.7/en/timestamp-initialization.html

desc foo; +-------+-----------+------+-----+-------------------+-----------------------------+ | Field | Type | Null | Key | Default | Extra | +-------+-----------+------+-----+-------------------+-----------------------------+ | a | int(11) | YES | | NULL | | | b | int(11) | YES | | NULL | | | ts | timestamp | NO | | CURRENT_TIMESTAMP | on update CURRENT_TIMESTAMP | +-------+-----------+------+-----+-------------------+-----------------------------+