Telemetry
Database reliability SQL recipe

Rank Database Queries by Total Time Impact

Combine call volume and latency to find database fingerprints consuming the most application time.

Beginnerdatabase_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 database operation consumes the most cumulative request time?

A rare slow query and a moderately slow high-volume query need different priorities. Total query time estimates aggregate application impact while p95 preserves tail behavior.

Event contract

Fields the query expects

FieldTypeWhy it exists
timestamp_utcTimestampQuery completion time.
database_systemUtf8Database system name.
operation_nameUtf8SELECT, INSERT, or another controlled operation.
query_fingerprintUtf8Normalized statement shape without values.
duration_msFloat64Application-observed query duration.
request_idUtf8Safe parent request identifier.
DataFusion SQL

Copy the query

sql
SELECT
  database_system,
  operation_name,
  query_fingerprint,
  COUNT(*) AS calls,
  SUM(duration_ms) AS total_time_ms,
  approx_percentile_cont(duration_ms, 0.95) AS p95_duration_ms,
  COUNT(DISTINCT request_id) AS affected_requests
FROM database_query_events
WHERE timestamp_utc >= now() - INTERVAL '24 hours'
GROUP BY database_system, operation_name, query_fingerprint
ORDER BY total_time_ms DESC
LIMIT 20;

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

Total query time by fingerprint

The project list has lower tail latency but consumes more aggregate request time.

database_systemoperation_namequery_fingerprintcallstotal_time_msp95_duration_msaffected_requests
postgresqlSELECTproject-list-v418,4001,288,0001429,200
postgresqlSELECTmonthly-report-v2420924,0004,180410

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

Total query time by fingerprint: static chart of synthetic total_time_ms values from the Rank Database Queries by Total Time Impact 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, illustrative input rows, exact SQL, expected output, review notes, and engine version. The CSV contains the displayed result.

How the SQL works

  1. 1Total time combines volume and latency into an impact ranking.
  2. 2p95 keeps a low-volume pathological query visible.
  3. 3Affected requests distinguishes repeated calls such as N+1 behavior.

Edge cases to decide

  • Application duration includes pool and network effects unless separately instrumented.
  • Never log bound SQL values.
  • Sampling needs weighted counts before comparing total impact.

Recommended dashboard

  • Bars: total_time_ms by fingerprint
  • Table: calls, p95, and affected requests
  • Trend: total query time by release

Alert guidance

Review sustained total-time growth; page only when it drives a customer-facing reliability objective.

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