Przejdź do treści
Biblioteka skilli
Programowanie · Budowanie i kod

n-plus-one-queries

/n-plus-one-queries

Znajduje i eliminuje problem N+1 w dostępie do bazy przez eager loading, batching lub dataloader, gdy endpoint listy zwalnia albo ORM ładuje leniwie w pętli.

Repozytorium
AI-SKILLS ↗
Licencja
MIT
Ostatnia zmiana repozytorium
05.09.2026

Ocena i bezpieczeństwo

90/ 100Klasa: dobry
Format25 / 25
Frontmatter z nazwą i opisem, poprawna nazwa, opis do 1024 znaków, nazwa zgodna z folderem.
Odkrywalność25 / 25
Opis mówi, kiedy użyć skilla, ma sensowną długość i jest w 3. osobie.
Treść25 / 35
Co najmniej 150 słów i do 500 linii, nagłówki, listy kroków, przykłady albo kod.
Higiena15 / 15
Bez zaślepek typu TODO, bez ścieżek Windows, z licencją.
Ryzykobrak sygnałów w skanie
Pliki w paczce
1
Skrypty
0
Komentarze HTML
0
Adresy URL w SKILL.md
0
Przestarzałość

Brak sygnałów. Repozytorium zmienione 05.09.2026, bez odwołań do wycofanych modeli i API.

Licencja

MIT, otwarta: można używać i zmieniać, z zachowaniem informacji o autorze.

Co robi i kiedy

Znajduje i eliminuje problem N+1 w dostępie do bazy przez eager loading, batching lub dataloader, gdy endpoint listy zwalnia albo ORM ładuje leniwie w pętli.

Oryginalny opis (EN)

Find and eliminate N+1 database access where one query per row replaces one query for the set, using eager loading, batching, or a dataloader. Use when a list endpoint is slow, query counts scale with result size, or an ORM lazy-loads inside a loop.

Jak działa

  1. Method
  2. Signals
  3. Boundaries
Sekcje z nagłówków SKILL.md, w oryginale · 48 linii, 399 słów
Podgląd SKILL.md
---
name: n-plus-one-queries
description: Find and eliminate N+1 database access where one query per row replaces one query for the set, using eager loading, batching, or a dataloader. Use when a list endpoint is slow, query counts scale with result size, or an ORM lazy-loads inside a loop.
---

# N+1 queries

The N+1 pattern issues one query to fetch a list, then one more query per row
to fetch a relation: 1 + N round trips where 2 would do. Each query looks fast
in isolation, so the plan never flags it; the cost is the hundreds of network
round trips the loop hides. It is the most common reason a list page crawls.

## Method

1. **Count queries per request, not query duration.** Turn on your ORM's query
   log (Rails `ActiveRecord` log, Django `django-debug-toolbar`, Hibernate
   `show_sql`, Prisma `log: ['query']`) and load one realistic page. If the
   count grows with the number of rows returned, you have N+1.
2. **Trace it to the relation accessed inside the loop.** The tell is a lazy
   association read while iterating: `order.customer.name` for each order in a
   list. The first query fetched orders; each `.customer` fires its own.
3. **Fix owned relations with eager loading.** Load the association in the same
   round trip: Rails `includes(:customer)`, Django
   `select_related`/`prefetch_related`, SQLAlchemy `selectinload`, Prisma
   `include`. This turns 1 + N into 1 or 2 queries, using a join or a single
   `WHERE id IN (...)`.
4. **Batch when eager loading does not fit.** For relations resolved across
   services or resolvers, collect the keys and issue one `IN` query per batch.
   In GraphQL, a per-request DataLoader coalesces the individual field reads
   into one keyed batch and caches within the request.
5. **Verify the query count dropped, and watch the swing.** Eager loading a
   huge fan-out relation can replace N small queries with one enormous join
   that materializes more rows than you want. Compare rows examined before and
   after; prefer a second `IN` query over a cartesian join when the fan-out is
   wide.
6. **Add a regression guard.** Assert a query-count ceiling in a test
   (`assert_queries(2)`, `django-assert-num-queries`) so the next lazy access
   inside a loop fails CI instead of shipping.

## Signals

- Does the query count stay flat as the result set grows from 10 to 100 rows?
- Is every association read in a loop covered by an eager load or a batch?
- Does a test pin the query count for the hot list endpoint?

## Boundaries

This targets access-pattern round trips, not slow individual queries; a single
query that is slow belongs to sql-optimization. When the batched query itself
is expensive, precomputation via materialized-views or a read cache may be the
better lever.

Wersje

W bazie nie ma innych skilli o nazwie n-plus-one-queries.

Migawka bazy z 13.11.2026 · wersja paczki 094c2c5cc31d (skrót SHA-256 całej paczki)

Pobrania w ostatnich 30 dniach: 2 · ranking

Zgłoś problem z tym skillem: szkodliwa treść, błędny opis albo prośba autora o usunięcie.

Podobne skille

  • 95db-advisor
    Monitoruje zapytania do bazy, wykrywa wzorzec N+1 w logach ORM i sugeruje eager loading lub grupowanie zapytań.
  • 90find-n-plus-one
    Znajduje powolne strony i endpointy, które wykonują wiele powtarzających się zapytań, i podpowiada eager loading albo prefetch.
  • 100graphql-n-plus-one-dataloader
    Diagnozuje i usuwa problem N+1 w resolverach GraphQL dzięki batchingowi DataLoader oraz zabezpieczeniom drugiego rzędu: persisted queries, limity głębokości i analiza kosztu.
  • 80Backend Queries
    Pisze bezpieczne i wydajne zapytania do bazy danych z parametryzowanymi instrukcjami, indeksami i optymalizacją, w tym SQL i metody ORM oraz eliminowanie N+1 przez eager loading lub joiny.
  • 100graphql-n-plus-one-remediation
    Wykrywa i naprawia wzorzec N+1 w GraphQL: skanuje repozytorium lub diff, klasyfikuje resolvery pól i stosuje batching DataLoader albo eager projection.
  • 100n-plus-one-hunter
    Wykrywa w logach zapytań wzorce N+1 z ORM i usuwa je zbiorczym eager loadingiem, pozostawiając tanie przypadki, w ActiveRecord, Prisma, SQLAlchemy i Hibernate.

Uwagi i zgłoszenia

Wiadomość trafi do Bartosza Sokolińskiego. Adres e-mail jest potrzebny tylko do odpowiedzi. Szczegóły w polityce prywatności.