MySQL - Fastest way to check if data in InnoDB table has changed

前端 未结 2 1716
不知归路
不知归路 2021-02-10 11:11

My application is very database intensive. Currently, I\'m running MySQL 5.5.19 and using MyISAM, but I\'m in the process of migrating to InnoDB. The only problem left is checks

2条回答
  •  小蘑菇
    小蘑菇 (楼主)
    2021-02-10 11:42

    The simplest way is to add a nullable column with type TIMESTAMP, with the trigger: ON UPDATE CURRENT_TIMESTAMP.

    Therefore, the inserts will not change because the column accepts nulls, and you can select only new and changed columns by saying:

    SELECT * FROM `table` WHERE `mdate` > '2011-12-21 12:31:22'
    

    Every time you update a row this column will change automatically.

    Here are some more informations: http://dev.mysql.com/doc/refman/5.0/en/timestamp.html

    To see deleted rows simply create a trigger which is going to log every deletion to another table:

    DELIMITER $$
    CREATE TRIGGER MyTable_Trigger
    AFTER DELETE ON MyTable
    FOR EACH ROW
    BEGIN
        INSERT INTO MyTable_Deleted VALUES(OLD.id, NOW());
    END$$
    

提交回复
热议问题