I have a database that it is always changing… updates, inserts and deletes.
For statistics reason, we need sometime to “go back in time” (-: and query the database like we are in the past.
For example:
I have a table named user, I have updated yesterday one of the users like the following:
update user set status = 1 where user_id = 2656
The old status was 0, so if i can “go back in time” to yesterday and query it ill get status 0, and if ill query it now, ill get status 1.
I know that one way to do it is to trigger update,insert and delete and log it to different tables, but it is not so clean, and will make the development complicated.
I hope you got me right, not so simple to explain for none English speaker…
Edit-1
We have few algorithms in the application and we fix them and upgrade them always, we have to know how the database was in the past so we can work on the algorithms, we have queries/interface that can be changed to support that (I am aware that this is not a one week project, we have the resources for that)
Thanks
Option 1 Enable the binary log.
This will safe all updates, inserts and deletes.
There are query tools for the binlog.
See:
http://dev.mysql.com/doc/refman/5.0/en/binary-log.html
http://www.mydigitallife.info/how-to-read-mysql-binary-log-files-binlog-with-mysqlbinlog/
Option 2 Create a trigger that saves the changes
The trigger save a data into a parallell database that has the same layout as the original database, except all tables have two extra fields. A unqiue id called
log_idand a timestamp. Whenever a field changes you have the trigger log in in the log.You need at least an after_update and after_delete trigger and I would recommend a after_insert trigger as well. If you feel like it, you can add a third field to every tables called
operation, which is anENUM('insert','delete','update').Of course if you create an extra timestamp type field in
table1_logyou don’t need to get it explicitly tonow()the DB will do that.See: http://dev.mysql.com/doc/refman/5.0/en/triggers.html