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

Question answered

Which database operation consumes the most cumulative request time?

A moderately slow query can consume more time overall than a rare, very slow query. Compare total query time with p95 latency when deciding which operation to fix.

Event schema

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. We review the synthetic sample output separately. Check field types, thresholds, and counting rules 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
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, 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 check

  • 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

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