Data change history with audit tables: Grouping changes

前端 未结 2 2040
有刺的猬
有刺的猬 2021-02-06 08:01

Lets say I want to store users and groups in a MySQL database. They have a relation n:m. To keep track of all changes each table has an audit table user_journal, group_journal a

2条回答
  •  小蘑菇
    小蘑菇 (楼主)
    2021-02-06 08:41

    Assuming you're rate of adding a batch of users to a group is less than once a second....

    I would suggest simply adding a column of type timestamp named something like added_timestamp to the user_group and user_group_journal. DO NOT MAKE THIS AN AUTO UPDATE TIMESTAMP OR DEFAULT IT TO CURRENT_TIMESTAMP, instead, in your code when you insert by batch into the user_group, calculate the current date and time, then manually set this for all the new user_group record.

    You may need to tweak your setup to add the field to be copied the rest of the new user_group record into the user_group_journal table.

    Then when you could create a query/view that groups on a group_id and the new added_timestamp column.

    If more fidelity is needed then 1 second you could use a string column and populate it with a string representation of a more granular time (which you'd need to generate however the libraries your language of use allows).

提交回复
热议问题