Trigger best practices and limitations in MySQL - Time & Space Complexity
When using triggers in MySQL, it's important to understand how their execution time grows as the data changes.
We want to know how triggers affect the speed of database operations as more rows are involved.
Analyze the time complexity of this trigger example.
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;
This trigger updates the stock count in the products table every time a new sale is inserted.
Look at what repeats when the trigger runs.
- Primary operation: The UPDATE statement runs once for each inserted sale row.
- How many times: Once per inserted row, so if multiple rows are inserted, it runs that many times.
As the number of inserted rows grows, the trigger runs more times.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 10 UPDATEs |
| 100 | 100 UPDATEs |
| 1000 | 1000 UPDATEs |
Pattern observation: The work grows directly with the number of inserted rows.
Time Complexity: O(n)
This means the time to complete grows in a straight line as more rows are inserted.
[X] Wrong: "Triggers run once no matter how many rows are inserted."
[OK] Correct: In MySQL, triggers run once per row, so inserting many rows means the trigger runs many times, increasing total work.
Understanding how triggers scale helps you design efficient databases and avoid slowdowns as data grows.
What if the trigger was changed to run AFTER UPDATE instead of AFTER INSERT? How would that affect the time complexity?
Practice
Solution
Step 1: Understand trigger performance impact
Triggers run automatically on data changes, so complex logic can slow down database operations.Step 2: Identify best practice for trigger design
Keeping trigger logic simple helps maintain good performance and avoids unexpected delays.Final Answer:
Keep trigger logic simple to avoid performance issues. -> Option AQuick Check:
Simple trigger logic = Better performance [OK]
- Trying to put all business logic inside triggers
- Allowing infinite recursion in triggers
- Using triggers for heavy data processing
Solution
Step 1: Identify trigger timing and event
BEFORE INSERT triggers run before a new row is inserted.Step 2: Match syntax to BEFORE INSERT event
CREATE TRIGGER trg BEFORE INSERT ON table_name FOR EACH ROW BEGIN ... END; uses BEFORE INSERT, correct timing and event for this trigger.Final Answer:
CREATE TRIGGER trg BEFORE INSERT ON table_name FOR EACH ROW BEGIN ... END; -> Option AQuick Check:
BEFORE INSERT syntax = CREATE TRIGGER trg BEFORE INSERT ON table_name FOR EACH ROW BEGIN ... END; [OK]
- Confusing BEFORE and AFTER keywords
- Using wrong event like UPDATE or DELETE
- Missing FOR EACH ROW clause
CREATE TRIGGER trg BEFORE INSERT ON employees FOR EACH ROW BEGIN SET NEW.salary = NEW.salary * 1.1; END;
Solution
Step 1: Analyze trigger action on BEFORE INSERT
The trigger modifies the NEW.salary value by multiplying it by 1.1 before the row is inserted.Step 2: Understand effect on inserted data
The inserted row will have salary increased by 10% automatically.Final Answer:
The salary is increased by 10% before insertion. -> Option BQuick Check:
BEFORE INSERT modifies NEW.salary = increased salary [OK]
- Thinking triggers run after insertion
- Assuming salary stays the same
- Confusing NEW and OLD keywords
CREATE TRIGGER trg AFTER UPDATE ON employees FOR EACH ROW BEGIN UPDATE employees SET salary = salary * 1.05 WHERE id = NEW.id; END;
Solution
Step 1: Identify trigger action on AFTER UPDATE
The trigger updates the same table employees inside an AFTER UPDATE trigger.Step 2: Understand recursion risk
Updating the same table inside its own UPDATE trigger causes the trigger to call itself repeatedly, causing infinite recursion.Final Answer:
It causes infinite recursion by updating the same table inside the trigger. -> Option DQuick Check:
Trigger updating same table = recursion risk [OK]
- Ignoring recursion risk
- Assuming syntax error without DELIMITER
- Thinking triggers can't run UPDATE statements
orders table without slowing down the delete operation. Which trigger design is best?Solution
Step 1: Choose trigger timing for logging
AFTER DELETE triggers run after the row is deleted, so they don't block the delete operation.Step 2: Select action that avoids slowing delete
Inserting a log record into a separate table is efficient and safe, avoiding recursion or delays.Final Answer:
Use an AFTER DELETE trigger that inserts a log record into a separate table. -> Option CQuick Check:
AFTER DELETE + separate log table = best practice [OK]
- Updating same table inside trigger causing recursion
- Using BEFORE DELETE for logging causing delays
- Doing heavy calculations inside triggers
Ready to prove it?
Pass the test and this pattern moves unknown
