# Fetch query statistics for Managed ClickHouse®


Query statistics in ClickHouse are available in the `system.query_log` table, which stores
statistics of each executed query, including memory usage, duration, rows and bytes read.

In Managed ClickHouse®, `system.query_log` is accessible with standard SQL:

```sql
SELECT
    query_duration_ms,
    read_rows,
    formatReadableSize(read_bytes) AS data_read
FROM system.query_log
WHERE type = 'QueryFinish'
ORDER BY event_time DESC
LIMIT 10;
```

`type = 'QueryFinish'` matches only queries that completed successfully. Failed queries are
logged with `type` set to `ExceptionBeforeStart` or `ExceptionWhileProcessing`, so errors need
their own query:

```sql
SELECT
    event_time,
    type,
    exception_code,
    exception,
    query
FROM system.query_log
WHERE type IN ('ExceptionBeforeStart', 'ExceptionWhileProcessing')
ORDER BY event_time DESC
LIMIT 10;
```

To inspect one specific statement, for example an ingest you have just run, filter on the query
text and read the memory peak alongside the duration:

```sql
SELECT
    query_duration_ms,
    read_rows,
    formatReadableSize(read_bytes) AS data_read,
    formatReadableSize(memory_usage) AS memory_peak
FROM system.query_log
WHERE type = 'QueryFinish' AND query LIKE 'INSERT INTO trips%'
ORDER BY event_time DESC
LIMIT 1;
```

> [!NOTE]
> System log tables in Managed ClickHouse keep data for 1 day. To retain query statistics for
> longer, persist them with a materialized view. See
> [ClickHouse system tables](/product/dbaas/service-specific/clickhouse/reference/clickhouse-system-tables/)
> for the retention details and the materialized view pattern.

