Recognizing the Problem
Explore the N+1 problem that occurs when resolving GraphQL schemas using Ecto and Absinthe in Elixir. Understand how naive implementations cause multiple database queries and learn to use middleware and debugging techniques to identify and prepare for optimizing this issue.
We'll cover the following...
N+1 problem
Anytime we resolve a schema using Ecto in both the resolver and query child values, we can quickly find ourselves stuck in what’s known as the “N + 1 problem." This makes us perform more database work than we expect and can cause unnecessary problems.
Let’s say we want to get the category for a bunch of menu items. The best way is to collect all the category_id values found within your set of menu items and then do a single SQL query for categories with those IDs. The N+1 problem happens when, instead, you do an SQL query for each individual menu item’s category. This is where the problem gets its name. There’s 1 query to get the menu items themselves, and then N queries afterward, where N is the number of menu items.
To illustrate this, let’s review how a document is executed with a naive example and see what happens. We’ll add a small piece of middleware to our schema that will print out each field that executes. To do so, we need to reorganize how we apply middleware a little so we can easily compose different options. Let’s start with the def middleware refactor:
Functionally, this is exactly the same, but it gives us a little more flexibility. Previously, we were pattern matching for objects or fields within the middleware/3 function heads. This ...