Abstraction Boundaries Are Optimization Boundaries: Lifting Abstractions for Database Query Optimization
2025-07-01

The N+1 query problem, where an application sends one SQL query per collection element, stems from leaky abstractions. Instead of lowering the abstraction boundary (e.g., explicitly telling the ORM to fetch in bulk), this article proposes raising it. By integrating the ORM into the language, rewrite rules can merge N queries into one. This mirrors Haskell's use of rewrite rules for list optimization, leveraging its declarative nature to abstract away low-level operational semantics for better optimization. The key takeaway: raising the abstraction boundary also raises the optimization boundary.
Read more