Trigger performance considerations in SQL - Time & Space Complexity
Triggers run automatically when data changes in a table. Understanding their time cost helps keep databases fast.
We ask: How does trigger execution time grow as more data changes happen?
Analyze the time complexity of the following trigger example.
CREATE TRIGGER update_log
AFTER INSERT ON orders
FOR EACH ROW
BEGIN
INSERT INTO order_log(order_id, created_at)
VALUES (NEW.id, NOW());
END;
This trigger adds a log entry every time a new order is inserted.
Look for repeated actions that affect performance.
- Primary operation: The trigger runs once for each inserted row.
- How many times: Equal to the number of rows inserted in the orders table.
Each inserted row causes the trigger to run once, so the work grows with the number of rows.
| Input Size (rows inserted) | Approx. Operations (trigger runs) |
|---|---|
| 10 | 10 |
| 100 | 100 |
| 1000 | 1000 |
Pattern observation: The number of trigger executions grows directly with the number of inserted rows.
Time Complexity: O(n)
This means the trigger's work increases in direct proportion to how many rows are inserted.
[X] Wrong: "Triggers run once no matter how many rows are inserted."
[OK] Correct: Triggers defined FOR EACH ROW run once per row, so more rows mean more trigger executions.
Knowing how triggers scale helps you design efficient databases and avoid slowdowns as data grows.
"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 trigger execution timing
Triggers run automatically during data changes, so their speed affects overall database performance.Step 2: Consider impact of complex triggers
Complex or slow triggers can delay data operations, causing performance issues.Final Answer:
To avoid slowing down database operations -> Option AQuick Check:
Trigger speed affects database speed = A [OK]
- Thinking triggers run only once a day
- Assuming triggers can modify any table without limits
- Believing triggers run without conditions
Solution
Step 1: Check trigger timing and event
The trigger is AFTER INSERT on the employees table, so syntax must include 'AFTER INSERT ON employees'.Step 2: Verify trigger body structure
The trigger body must be enclosed in BEGIN ... END; and include FOR EACH ROW for row-level triggers.Final Answer:
CREATE TRIGGER trg AFTER INSERT ON employees FOR EACH ROW BEGIN UPDATE stats SET count = count + 1; END; -> Option BQuick Check:
Correct syntax includes timing, event, table, and body [OK]
- Omitting 'ON' keyword before table name
- Missing 'FOR EACH ROW' for row-level triggers
- Incorrect order of keywords
CREATE TRIGGER trg_update AFTER UPDATE ON orders FOR EACH ROW BEGIN UPDATE inventory SET stock = stock - NEW.quantity WHERE product_id = NEW.product_id; END;
What happens if an update changes 10 units of product_id 5 in orders?
Solution
Step 1: Understand trigger action on update
The trigger runs AFTER UPDATE on orders and adjusts inventory stock by subtracting NEW.quantity.Step 2: Apply the update values
For product_id 5, stock is reduced by 10 units as NEW.quantity is 10.Final Answer:
The inventory stock for product_id 5 decreases by 10 -> Option CQuick Check:
Trigger subtracts NEW.quantity from stock = B [OK]
- Confusing NEW with OLD values
- Assuming stock increases instead of decreases
- Thinking trigger causes syntax error
CREATE TRIGGER trg_log AFTER INSERT ON sales FOR EACH ROW BEGIN INSERT INTO sales_log VALUES (NEW.id, NEW.amount); UPDATE sales SET amount = amount * 1.1 WHERE id = NEW.id; END;
What is the likely cause of the error?
Solution
Step 1: Identify trigger actions
The trigger inserts into sales_log and then updates sales table, which is the same table that fired the trigger.Step 2: Understand recursion risk
Updating sales inside an AFTER INSERT trigger on sales causes the trigger to fire again, leading to infinite recursion and runtime error.Final Answer:
Trigger tries to update the same table it is triggered on, causing recursion -> Option AQuick Check:
Updating trigger table inside trigger causes recursion error [OK]
- Ignoring recursion from updating same table
- Assuming missing semicolon causes runtime error
- Believing NEW is invalid in AFTER INSERT triggers
customers table without slowing down transactions. Which approach best improves trigger performance?Solution
Step 1: Consider trigger timing and complexity
AFTER UPDATE triggers run after the change, so they don't block the main transaction as much as BEFORE triggers.Step 2: Keep trigger code minimal
Inserting only essential data into a log table keeps the trigger fast and reduces performance impact.Final Answer:
Use a simple AFTER UPDATE trigger that inserts minimal data into a log table -> Option DQuick Check:
Simple AFTER triggers with minimal work improve performance [OK]
- Using BEFORE triggers with complex logic
- Splitting logging into multiple triggers causing overhead
- Updating many tables inside triggers
Ready to prove it?
Pass the test and this pattern moves unknown
