DEV Community

kawasaki031677
kawasaki031677

Posted on

Reproducing the N+1 Problem in Practice

Introduction

When developing systems, you often hear about the "N+1 problem."
In this article, I'll demonstrate how to actually reproduce the N+1 problem using PostgreSQL.

What is the N+1 Problem?

The N+1 problem occurs when you fetch N records, then retrieve related data using N separate SQL queries.

For example, suppose you have the following tables:

  • users: User information
  • posts: User posts

You want to "fetch all users and the articles they posted." A simple implementation often looks like this:

  1. Fetch users from the users table
  2. Loop through the fetched users
  3. Search posts for each user

For example, if there are 10,000 users, you'd execute:

SELECT * FROM users;

SELECT * FROM posts WHERE user_id = 1;
SELECT * FROM posts WHERE user_id = 2;
SELECT * FROM posts WHERE user_id = 3;
...
SELECT * FROM posts WHERE user_id = 10000;
Enter fullscreen mode Exit fullscreen mode

This results in a total of 10,001 SQL queries being executed.

This is the N+1 problem.

Setup

Let's create experimental data.

CREATE TABLE users (
    id BIGSERIAL PRIMARY KEY,
    name TEXT NOT NULL
);

CREATE TABLE posts (
    id BIGSERIAL PRIMARY KEY,
    user_id BIGINT NOT NULL REFERENCES users(id),
    title TEXT NOT NULL
);

-- 10,000 users
INSERT INTO users (name)
SELECT 'user-' || generate_series
FROM generate_series(1, 10000);

-- 10 posts per user, 100,000 total
INSERT INTO posts (user_id, title)
SELECT
    u.id,
    'post-' || gs
FROM users u
CROSS JOIN generate_series(1, 10) AS gs;
Enter fullscreen mode Exit fullscreen mode

For this experiment, I prepared the following data:

  • users: 10,000 records
  • posts: 100,000 records

Fetching with N+1

Fetch posts for each user:

SELECT * FROM users;

SELECT id, user_id, title FROM posts WHERE user_id = 1;
SELECT id, user_id, title FROM posts WHERE user_id = 2;
SELECT id, user_id, title FROM posts WHERE user_id = 3;
...
SELECT id, user_id, title FROM posts WHERE user_id = 10000;
Enter fullscreen mode Exit fullscreen mode

In total, 10,001 SQL queries are executed.

In my environment, this took approximately 30.3 seconds.

Fetching with a Single Query

Next, let's fetch everything with a single SQL query:

SELECT id, user_id, title
FROM posts
WHERE user_id IN (
    SELECT id
    FROM users
);
Enter fullscreen mode Exit fullscreen mode

In my environment, this took approximately 2.6 seconds.
There's a significant difference compared to executing many queries.

Why Such a Difference?

Each time you execute an SQL query, processing occurs between the application and the database.

Especially when the application and database are on separate servers, network communication happens every time you execute a query.

What might be acceptable for a single query becomes a significant cost when you repeat it 10,000 times.

Warning: The scary part of the N+1 problem isn't that individual SQL queries are slow, but that you end up executing far more SQL than necessary.

Summary

The results from this experiment are as follows:

Method SQL Queries Execution Time
N+1 fetching 10,001 ~30.3 seconds
Single SQL query 1 ~2.6 seconds

Although execution time varies depending on the environment,
it's clear that increasing the number of SQL queries leads to significant performance differences.

Info: To avoid the N+1 problem, it's important to be mindful of the number of SQL queries you execute.

Top comments (0)