Grafana Cloud

Examine query samples

Query samples capture actual query executions from your database, including the specific execution time, duration, and associated wait events. Use samples to understand real-world query behavior and identify patterns that aggregated metrics might miss.

What query samples capture

Unlike aggregated metrics that show averages and totals, query samples capture individual query executions. Both MySQL and PostgreSQL samples include the timestamp, duration, database/schema, CPU time, wait events, and full query text (shown when redaction is disabled). Beyond these shared fields, each engine provides additional details.

MySQL samples

MySQL samples include resource and row-level metrics from Performance Schema:

FieldDescription
Rows examinedNumber of rows scanned during execution
Rows sentNumber of rows returned to the client
Rows affectedNumber of rows modified
ErrorsError count
Max controlled memoryControlled memory used by the query
Max total memoryTotal memory used by the query

PostgreSQL samples

PostgreSQL samples include connection and process context from pg_stat_activity:

FieldDescription
Transaction timeDuration of the enclosing transaction
UserWhich database user ran the query
Backend typeType of backend process
ClientClient address
Process IDBackend process ID
StateQuery state when sampled (active, idle, etc.)
Leader PIDLeader process ID for parallel queries
Transaction ID (Xid)Transaction identifier
Xmin horizonXmin snapshot value

Why query samples matter

Aggregated metrics can hide important patterns:

  • Bimodal distributions: A query might usually take 10ms but occasionally take 10 seconds
  • Parameter-specific issues: Certain parameter values might cause slow execution
  • Time-based patterns: Performance might degrade during specific time windows
  • Correlation with events: You can match slow queries with application events

Access query samples

To view samples for a query:

  1. Navigate to Database Observability in Grafana Cloud.
  2. In the Queries Overview, click a query to open its details.
  3. Select the Query Samples tab.
  4. View the time series chart and sample list.

Time series visualization

The Query Samples tab shows a chart of sample executions over time:

  • X-axis: Time
  • Y-axis: Query duration
  • Points: Individual query executions

Hover over a point to see a tooltip with sample details. The fields shown vary by database engine — refer to the table above for the full list. Click a sample to select it and view the full query text (when redaction is disabled).

View full query text

By default, Database Observability redacts query parameters for security:

Redacted:

SQL
SELECT * FROM users WHERE email = ?

Full text (when redaction disabled):

SQL
SELECT * FROM users WHERE email = 'user@example.com'

To enable full query text including parameters:

  1. For MySQL, configure Alloy:
Alloy
database_observability.mysql "default" {
  query_samples {
    disable_query_redaction = true
  }
}
  1. For PostgreSQL, configure Alloy:
Alloy
database_observability.postgres "default" {
  query_samples {
    disable_query_redaction = true
  }
}

Warning

Disabling redaction exposes actual query parameters in logs. Only enable this in environments where parameter values don’t contain sensitive data, or ensure appropriate access controls are in place.

Configure sample collection

You can adjust how query samples are collected, including collection intervals, query redaction settings, and which users to exclude. For all configuration options, refer to Tune Alloy collection.