Search code examples
javamysqldatabasetransactional

What is different in ram process of trigger and manual insert in mySQL


Which way is better for saving log of data access in table for transactional database ? Using trigger or using manual insert in table? Manual means writing sqlQuery for inserting log of program in table.


Solution

  • Auditing of this kind is mostly done via triggers. The main reasons are:

    • Developers will not forget calling it, as it would happen if there is a separate insert need to be fired
    • A simple bug would not cause the second insert to fail and leave the previous operation non-audited
    • The auditing cannot be intentionally left out, it is really controlled by the owner of the DB
    • The extra network round-trip + query parsing required by the second insert is not a small matter. For basic operations the actual time-cost of these are significant.

    On the other hand the only downside of this solution is the extra logic that is now on DB side. By default developers tend to leave as little logic live in the DB as possible (which is normally a good idea), but in this case I think it is not a valid argument. This is not business logic, it is an organic part of your DB. The data about "who accessed and what data" is still data, and belongs to the database.