SQL Practice Logo

SQLPractice Online

Triggers & Stored Procedures: Performance

Module: Schema Design & Advanced DDL

Triggers add overhead to every operation. Measure the impact:

**Benchmark without trigger:**

```sql

EXPLAIN ANALYZE INSERT INTO orders (customer_id, total) VALUES (123, 100.00);

-- Execution time: 5ms

```

**Benchmark with trigger:**

```sql

-- Same INSERT with audit trigger

-- Execution time: 15ms (+10ms overhead)

```

**Optimization:**

- Use statement-level triggers for bulk operations

- Add WHEN clause to skip unnecessary executions

- Keep trigger logic simple

- Avoid queries in triggers

- Consider async processing for non-critical actions

Simple triggers add 5-10ms, complex triggers add 50-100ms

Use statement-level triggers for bulk operations

Add WHEN clause to filter executions

Avoid queries in triggers when possible

Monitor with EXPLAIN ANALYZE

Complex business logic in triggers - hard to debug

Cascading triggers - performance multiplies

Forgetting triggers exist - hidden behavior

No error handling - failures roll back transaction

Triggers on high-volume tables - performance impact