Potential N+1 problem in n8n
Why database operations inside loops can cause severe performance issues
What is this issue?
The N+1 problem occurs when a database write or read operation runs for each item in a loop. Instead of one batch operation, you make N individual calls, dramatically increasing database load and execution time.
Patterns indicating N+1:
•INSERT single row inside SplitInBatches loop•UPDATE per item instead of bulk UPDATE•Individual SELECT queries instead of JOIN or IN clause•Postgres/MySQL node connected after SplitInBatches
Why is this dangerous?
Database overload
1000 items = 1000 database connections and queries, potentially crashing your database.
Slow execution
Each query adds network latency. 1000 x 50ms = 50 seconds vs. 50ms for a batch.
Connection exhaustion
You can run out of database connections, blocking other workflows and applications.
Transaction issues
Individual operations lack atomicity—partial failures leave data inconsistent.
How to fix it
- 1
Use bulk operations
Replace individual INSERT/UPDATE with bulk operations that handle all items at once.
- 2
Process before loop
Move database operations before SplitInBatches to batch data, then loop for other processing.
- 3
Use SQL batch syntax
Write raw SQL with VALUES lists or UPDATE FROM syntax to handle multiple rows in one query.
- 4
Collect then write
Aggregate items first with Item Lists, then write all at once after the loop completes.
Scan your workflow now
Upload your n8n workflow JSON and detect database operations inside loops that need optimization.