Home > Software engineering >  MySQL trigger vs application insert for history
MySQL trigger vs application insert for history

Time:09-26

I have a main table in mysql and need a history table for tracking the changes in the table.

I have 2 approaches.

  1. trigger --> create a trigger for the main table which inserts into history table for any change in the main table
  2. insert into the history table while inserting or updating in the main table from application

I am checking which is the best approach with performance.

CodePudding user response:

Assuming your trigger performs exactly the same operation as the separate logging query (e.g. both insert a row to your history table whenever you modify your table), there is no significant performance difference between your two options, as both do the same amount of work.

The decision is usually design driven - or the preference of whoever makes the guidelines you have to follow.

Some advantages of using a trigger for your history log:

  • You cannot forget to log, e.g. by coding mistakes in your app, and don't have to take care of it in every quick and dirty maintenance script. MySQL does it for you.
  • You have direct access to all column values in the trigger including their previous values, and specifically the primary key (new.id). This makes logging trivial.
  • If you e.g. do batch modifications, it might be complicated to write an individual logging query. delete from tablename where xyz? You probably will do a insert into historytable ... select ... where xyz first, and if xyz is a slow condition that ends up not deleting anything, you may just double your execution time this way. So much for performance. update tablename set a = rand() where a > 0.5? Good luck writing a proper separate logging query for this.

Some advantages not using a trigger to log:

  • you have control over when and what you log, e.g. if you want to log only specific changes done by end users in your application, but not those by batch scripts or automatic processes, it might be easier (and faster) to just log explicitly what you want to log.
  • you may want to log additional information not available to the trigger (and that you don't want to store in the main table), e.g the windows login or the last button the user pressed to access the function that modified this data.
  • it might be more convenient to write a general logging function in a programming language, where you can use meta data to e.g. dynamically generate the logging query or compare old and new values in a loop over all columns, than to maintain 3 triggers for every table, where you usually have to list every column explicitly.
  • since you are especially interested in performance: although it's probably more a theoretical than a practical advantage, if you do a lot of batch modifications, it might be faster to write the log in batches too (e.g. inserting 1000 history rows at once will be faster than inserting 1000 rows individually using a trigger). But you will have to properly design your logging query, and the query itself cannot be slow.
  • Related