Why triggers automate responses in MySQL - Performance Analysis
When using triggers in MySQL, it's important to know how their execution time grows as data changes.
We want to understand how triggers affect the speed of database operations as more rows are involved.
Analyze the time complexity of this trigger that runs after an insert.
CREATE TRIGGER after_insert_example
AFTER INSERT ON orders
FOR EACH ROW
BEGIN
INSERT INTO audit_log(order_id, action_time)
VALUES (NEW.id, NOW());
END;
This trigger adds a record to an audit table every time a new order is inserted.
Look at what repeats when this trigger runs.
- Primary operation: The trigger runs once for each inserted row.
- How many times: Exactly once per new order inserted.
As you insert more rows, the trigger runs that many times.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 10 trigger executions |
| 100 | 100 trigger executions |
| 1000 | 1000 trigger executions |
Pattern observation: The work grows directly with the number of inserted rows.
Time Complexity: O(n)
This means the trigger's work increases in a straight line as you add more rows.
[X] Wrong: "The trigger runs only once no matter how many rows are inserted."
[OK] Correct: Actually, triggers run once per row, so inserting many rows means many trigger executions.
Understanding how triggers scale helps you design efficient databases and shows you think about real-world data growth.
"What if the trigger was defined FOR EACH STATEMENT instead of FOR EACH ROW? How would the time complexity change?"
Practice
Solution
Step 1: Understand what triggers do
Triggers run automatically when certain data changes happen in the database, like insert, update, or delete.Step 2: Compare options with trigger purpose
Only To automatically run code when data changes describes automatic code running on data changes, which matches the trigger's role.Final Answer:
To automatically run code when data changes -> Option DQuick Check:
Trigger = automatic response to data change [OK]
- Thinking triggers create tables
- Confusing triggers with manual commands
- Assuming triggers backup data
Solution
Step 1: Recall MySQL trigger syntax
The correct syntax starts with CREATE TRIGGER, followed by trigger name, timing (BEFORE/AFTER), event (INSERT/UPDATE/DELETE), and table name.Step 2: Check each option's syntax
Only CREATE TRIGGER trigger_name BEFORE INSERT ON table_name FOR EACH ROW matches the correct syntax format exactly.Final Answer:
CREATE TRIGGER trigger_name BEFORE INSERT ON table_name FOR EACH ROW -> Option AQuick Check:
Correct syntax starts with CREATE TRIGGER [OK]
- Using MAKE or START instead of CREATE
- Missing FOR EACH ROW clause
- Wrong order of keywords
CREATE TRIGGER update_stock AFTER INSERT ON sales FOR EACH ROW BEGIN UPDATE products SET stock = stock - NEW.quantity WHERE product_id = NEW.product_id; END;
What happens when a new sale is inserted?
Solution
Step 1: Analyze the trigger event and action
The trigger runs AFTER INSERT on sales table, so after a sale is added, it updates products stock.Step 2: Understand the update statement
The update reduces stock by NEW.quantity for the product matching NEW.product_id, meaning stock decreases for sold product.Final Answer:
The stock of the sold product decreases by the sold quantity -> Option AQuick Check:
AFTER INSERT trigger updates stock correctly [OK]
- Thinking stock increases instead of decreases
- Assuming trigger deletes tables
- Believing trigger does nothing automatically
CREATE TRIGGER wrong_trigger BEFORE UPDATE ON orders BEGIN SET NEW.status = 'processed'; END;
What is the main problem?
Solution
Step 1: Check trigger syntax requirements
MySQL requires FOR EACH ROW clause after the table name in triggers that act on rows.Step 2: Identify missing part in code
The code misses FOR EACH ROW after ON orders, causing syntax error.Final Answer:
Missing FOR EACH ROW clause after table name -> Option BQuick Check:
FOR EACH ROW is mandatory in row-level triggers [OK]
- Thinking BEFORE UPDATE triggers are not allowed
- Believing NEW cannot be assigned
- Assuming trigger names cannot have underscores
Solution
Step 1: Identify correct trigger timing and event
We want to log after the email changes, so AFTER UPDATE is correct to capture new and old values.Step 2: Check trigger syntax and usage of OLD and NEW
CREATE TRIGGER log_email_change AFTER UPDATE ON users FOR EACH ROW BEGIN INSERT INTO email_log(user_id, old_email, new_email) VALUES (NEW.id, OLD.email, NEW.email); END; uses FOR EACH ROW and correctly inserts old and new email values into log table after update.Final Answer:
CREATE TRIGGER log_email_change AFTER UPDATE ON users FOR EACH ROW BEGIN INSERT INTO email_log(user_id, old_email, new_email) VALUES (NEW.id, OLD.email, NEW.email); END; -> Option CQuick Check:
AFTER UPDATE with OLD and NEW logs changes [OK]
- Using BEFORE INSERT instead of AFTER UPDATE
- Missing FOR EACH ROW clause
- Using AFTER DELETE when updating data
Ready to prove it?
Pass the test and this pattern moves unknown
