# Inspect query history

Use system.query_log to inspect finished work and system.processes to inspect requests that are still running. A query ID connects the request with response metadata and diagnostics.

## Enable history

```sql
SET log_queries = 1;
```

The setting persists for the selected database. log_queries_min_query_duration_ms can restrict history to requests meeting a duration threshold.

## Read terminal events

```sql
SELECT type, exception_code
FROM system.query_log
WHERE type = 'QueryFinish'
ORDER BY event_time DESC
LIMIT 1
FORMAT csv;
```

```text
"QueryFinish",0
```

A direct single-table SELECT from system.query_log waits for earlier queued events before reading. The current request's own terminal event appears only after it finishes. A duration threshold can omit short requests and their start events.

## Follow a request

Use its query_id to select duration, row/byte counters and exception details. Inspect system.processes for an active request rather than expecting its terminal history record before completion.

See [query_log](https://coordinatordb.com/docs/reference/system-tables/query-log), [processes](https://coordinatordb.com/docs/reference/system-tables/processes) and [query measurements](https://coordinatordb.com/docs/guides/clickhouse/performance-and-monitoring/isolate-query-bottlenecks).
