Solving the 1+N query problem
The N+1 query problem occurs when an ORM's object-access syntax silently triggers separate database queries in a loop, deoptimizing what should be a single join. Acadia, a new query language by Evan Czaplicki, eliminates this entirely by design: it has no for loops and no recursion, drawing inspiration from Datalog (a recursion-free subset of Prolog from the 1970s). This restriction guarantees not only that N+1 queries are impossible, but that every Acadia query terminates, and terminates in polynomial time relative to the data size. The post walks through pl/pgSQL and GraphQL examples showing how Turing-complete looping constructs enable the problem, then shows Acadia's intersect-based syntax as the alternative, along with the SQL it compiles to.
