> For the complete documentation index, see [llms.txt](https://docs.groundcover.com/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.groundcover.com/reference/remote-access-and-apis/api-examples/query-database-measurements.md).

# Query Database Measurements

Query database statement execution counts and time with gcQL, as tables or time series.

Use database measurements to analyze statement execution counts and execution time. These endpoints require a backend with database statement measurements enabled and collected data.

For collection prerequisites, permissions, and measurement semantics, see [PostgreSQL Database Monitoring](https://docs.groundcover.com/collect-data/data-sources/postgresql-database-monitoring.md#how-statement-measurements-work) and [ClickHouse Database Monitoring](https://docs.groundcover.com/collect-data/data-sources/clickhouse-database-monitoring.md#statement-measurements). Health metrics and collected query traces are separate outputs; neither confirms that statement measurements are available. Use [Activity Traces](https://docs.groundcover.com/collect-data/monitor-data-sources-integrations.md#database-monitoring-diagnostics) to diagnose collection.

For [MySQL Database Monitoring](https://docs.groundcover.com/collect-data/data-sources/mysql-database-monitoring.md#statement-measurements), filter measurements with `db_system:mysql`. MySQL measurements use `calls` and `exec_seconds` plus engine-specific fields; omitted numeric fields and observed zeros both appear as zero in storage/API results. A zero-value filter cannot distinguish them. See the MySQL guide for coverage and prepared-statement limitations.

### Endpoints

| Operation                     | Endpoint                                            |
| ----------------------------- | --------------------------------------------------- |
| Search statement measurements | **POST** `/api/dbm-measurements/search`             |
| Query a time series           | **POST** `/api/dbm-measurements/search-time-series` |

### Authentication

Send an [API key](https://docs.groundcover.com/administer/api-keys.md) in `Authorization: Bearer <YOUR_API_KEY>` and select the backend with `X-Backend-Id: <YOUR_BACKEND_ID>`. Self-hosted deployments use their own API base URL.

### Request Body

| Field            | Type   | Description                                                                                       |
| ---------------- | ------ | ------------------------------------------------------------------------------------------------- |
| `start`, `end`   | string | Time range in RFC3339 format.                                                                     |
| `query`          | string | Required gcQL query. This endpoint accepts gcQL, not a raw `pipeline` object.                     |
| `sources`        | array  | Optional structured scope conditions.                                                             |
| `bucketDuration` | string | Required for time series; positive interval such as `1m`.                                         |
| `valueField`     | string | Numeric result alias to chart. Set it explicitly when the query produces several numeric columns. |
| `fillValue`      | number | Optional replacement for empty time buckets; omitted buckets have `null` values.                  |

`count()` counts measurement intervals. Use `sum(calls)` to count database executions, and `sum(exec_seconds)` for cumulative execution time in seconds.

### Example: Most Expensive Statements

```bash
curl --request POST 'https://api.groundcover.com/api/dbm-measurements/search' \
  --header 'Authorization: Bearer <YOUR_API_KEY>' \
  --header 'X-Backend-Id: <YOUR_BACKEND_ID>' \
  --header 'Content-Type: application/json' \
  --data '{
    "start": "2026-10-28T10:00:00Z",
    "end": "2026-10-28T11:00:00Z",
    "query": "* | stats by (query_id, query_fingerprint) sum(exec_seconds) as execution_seconds, sum(calls) as calls | sort by (execution_seconds desc) | limit 20"
  }'
```

The response wraps rows in `data` and lists numeric columns in `valueFieldHints`:

```json
{
  "data": [
    {
      "query_id": "123456789",
      "query_fingerprint": "SELECT * FROM orders WHERE id = ?",
      "execution_seconds": 12.5,
      "calls": 50
    }
  ],
  "valueFieldHints": ["calls", "execution_seconds"]
}
```

The response values above are illustrative. Other useful grouping fields include `server_address`, `server_port`, and `db_system`.

### Example: Database Calls over Time

```bash
curl --request POST 'https://api.groundcover.com/api/dbm-measurements/search-time-series' \
  --header 'Authorization: Bearer <YOUR_API_KEY>' \
  --header 'X-Backend-Id: <YOUR_BACKEND_ID>' \
  --header 'Content-Type: application/json' \
  --data '{
    "start": "2026-10-28T10:00:00Z",
    "end": "2026-10-28T11:00:00Z",
    "query": "* | stats by (db_system) sum(calls) as calls",
    "bucketDuration": "1m",
    "valueField": "calls",
    "fillValue": 0
  }'
```

Time-series responses are arrays of objects with `metric` labels and a `velocity` array of timestamp/value points, rather than the search envelope. The query must aggregate data; the endpoint adds time buckets. See [Time-Series Queries](https://docs.groundcover.com/reference/remote-access-and-apis/api-examples/time-series-queries.md).

### Errors

Invalid gcQL, unknown fields, reversed effective time bounds, non-positive buckets, or invalid value-field selection return HTTP `400`. A backend that does not support statement measurements returns `404`.


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation by asking a question.

Perform an HTTP GET request on the following URL with the `ask` and `goal` query parameters:

```
GET https://docs.groundcover.com/reference/remote-access-and-apis/api-examples/query-database-measurements.md?ask=<question>&goal=<user_goal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is what the user is ultimately trying to achieve, the reason they need the answer. Sharing it helps GitBook give you a better, more relevant answer. A goal is most helpful when it describes the outcome the user wants rather than restating the question. For example, with `ask=how do I create an API token`, a goal like `automate deployments from our CI pipeline` lets GitBook tailor the answer to that use case.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
