N+1 problem

The N+1 problem occurs when an application runs one query for the list, then one query per row for associated data, multiplying round trips to the database. You fix it with a join, batch loading (WHERE id = ANY(...)) or the ORM's eager loading. pg_stat_statements helps spot it.

Example

SELECT * FROM commandes WHERE client_id = ANY(ARRAY[1, 2, 3]);

Related concepts

The whole glossary