patternsqlMinor
Is it normal to use many Triggers?
Viewed 0 times
usetriggersnormalmany
Problem
I have many so-called summary columns throughout my database to count number of rows in other tables. For example, counting the number of comments by a user (a column in the user table). Currently, I update this column by an extra query:
Now I want to introduce
Benchmarks show that using
Am I on the right track?
UPDATE user_table SET comments = comments + 1 WHERE user_id='x'Now I want to introduce
Triggers to my database instead of running the second query. Two Triggers for each column: one DELETE and one INSERT. Then, I will have 30 - 40 Triggers!!!Benchmarks show that using
Triggers is about 50% faster than executing a second query. Apparently, it is reasonable to use the modern features of mysql. However, the reason that I am asking this question is that many programmers are strongly against Triggers, as it makes coding vague. Moreover, adding lots of Triggers may make Information_Schema messy.Am I on the right track?
Solution
The major problems with triggers are:
a) They are difficult to debug
b) Trigger cascades can be fatal to your database - take for example if you have an after update trigger in one table which updates another table, which also has an after update trigger, then you will run into lots of issues
A few questions:
a) How often are the "computed" summaries used within your application?
b) How often are the "computed" summaries required to be updated?
c) Would you consider creating a "reporting" table which is refreshed on a regular basis with the major "computations" that you need?
a) They are difficult to debug
b) Trigger cascades can be fatal to your database - take for example if you have an after update trigger in one table which updates another table, which also has an after update trigger, then you will run into lots of issues
A few questions:
a) How often are the "computed" summaries used within your application?
b) How often are the "computed" summaries required to be updated?
c) Would you consider creating a "reporting" table which is refreshed on a regular basis with the major "computations" that you need?
Context
StackExchange Database Administrators Q#16152, answer score: 3
Revisions (0)
No revisions yet.