Telemetry
Database reliability SQL recipe

Detect N+1 Database Query Patterns

Detect database fingerprints repeated many times inside one application request.

Intermediatedatabase_request_query_eventsReviewed 2026-07-28Tested with Apache DataFusion 45.2.0

Reviewed by the Telemetry product team on . SQL compatibility, event contract, synthetic output, and operational caveats. Editorial ownership

Question answered

Which route and query combinations repeat suspiciously within a request?

N+1 behavior is a correlation problem: a query may be fast and harmless globally but repeated excessively inside individual requests. This pattern counts each fingerprint per request before summarizing affected routes.

Event contract

Fields the query expects

FieldTypeWhy it exists
timestamp_utcTimestampQuery completion time in UTC.
request_idUtf8Bounded request correlation identifier.
route_templateUtf8Stable application route.
query_fingerprintUtf8Normalized query shape without parameters.
sequence_in_requestInt64Query order inside the request.
duration_msFloat64Database operation duration.
environmentUtf8Deployment environment.
DataFusion SQL

Copy the query

sql
WITH queries_per_request AS (
  SELECT
    route_template,
    query_fingerprint,
    request_id,
    COUNT(*) AS executions_in_request
  FROM database_request_query_events
  WHERE timestamp_utc >= now() - INTERVAL '24 hours'
    AND environment = 'production'
  GROUP BY route_template, query_fingerprint, request_id
)
SELECT
  route_template,
  query_fingerprint,
  COUNT(*) AS requests,
  SUM(CASE WHEN executions_in_request >= 5 THEN 1 ELSE 0 END)
    AS requests_with_repeats,
  AVG(executions_in_request) AS average_executions_per_request,
  MAX(executions_in_request) AS maximum_executions_in_request
FROM queries_per_request
GROUP BY route_template, query_fingerprint
ORDER BY requests_with_repeats DESC, route_template;

This read-only query is planned and executed against an empty typed table with Apache DataFusion 45.2.0. The deterministic sample output is synthetic and reviewed separately; validate field types, thresholds, and business definitions against your own data. Read the testing methodology.

Query result

Repeated queries inside requests

The checkout route repeats its line-item fingerprint at least five times in two requests.

route_templatequery_fingerprintrequestsrequests_with_repeatsaverage_executions_per_requestmaximum_executions_in_request
/api/checkoutSELECT line_items BY order_id3246
/api/profileSELECT user WITH preferences3011

Synthetic example output. Run the query against your own event schema and thresholds before using it for operational decisions.

Repeated queries inside requests: static chart of synthetic requests_with_repeats values from the Detect N+1 Database Query Patterns example result
Indexable SVG of the deterministic example output. Download it for an article, runbook, or design review with attribution.

Reproduce the example

Download the public fixture

The JSON bundle includes the typed event contract, reproducible input rows, exact SQL, expected output, review notes, and engine version. The CSV contains the displayed result.

How the SQL works

  1. 1The inner query establishes request-level repetition before any route-level aggregation.
  2. 2A controlled fingerprint groups equivalent parameterized operations without retaining raw SQL values.
  3. 3Both prevalence and worst-case repetition are preserved so one extreme request does not look systemic.

Edge cases to decide

  • Choose a threshold that fits intentional batching and pagination behavior.
  • Async work needs a correlation identifier that survives the handoff from request to worker.
  • Sampling query events can hide exact repetition counts; use trace-aware sampling or document the estimate.

Recommended dashboard

  • Bars: requests_with_repeats by route and fingerprint
  • Trend: average executions per request by release
  • Trace link: query sequence for a selected request_id

Alert guidance

Prefer a release regression alert when repeated-query prevalence rises materially, rather than paging on one request.

Read alert setup

Put the recipe to work

Related instrumentation and guides

Continue the analysis

Run it on real events

Create a table, adapt the fields, and save the result

Start free, send structured events, and use the query result as a chart, shared dashboard widget, or alert input.

Get an API key