Skip to content
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 . We checked the SQL syntax, required event fields, sample results, and limits on using the query. Who reviews this page

Question answered

Which route and query combinations repeat suspiciously within a request?

A query can be fast on its own and still slow a request when repeated many times. Count each query fingerprint per request, then compare affected routes.

Event schema

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. We review the synthetic sample output separately. Check field types, thresholds, and counting rules 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
Download this SVG chart of the sample results for an article, runbook, or design review. Please credit Telemetry.

Reproduce the example

Download the sample data

The JSON bundle includes the event schema with field types, 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 check

  • 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

Set up the events this query needs

Related instrumentation and guides

Continue the analysis

Run it on your 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