> ## Documentation Index
> Fetch the complete documentation index at: https://clickhouse.com/docs/llms.txt
> Use this file to discover all available pages before exploring further.

# Why does my query return zero rows without an error when max_execution_time is set?

> A query stopped by max_execution_time with timeout_overflow_mode = break ends without an error and can return an empty result. How to confirm it in system.query_log and fix it.

When a query runs longer than [`max_execution_time`](https://clickhouse.com/docs/reference/settings/session-settings/max-execution#max_execution_time) and [`timeout_overflow_mode`](https://clickhouse.com/docs/reference/settings/session-settings/timeout-overflow-mode#timeout_overflow_mode) is set to `break`, ClickHouse stops the query without an error and returns what it has produced so far. A query that aggregates or sorts has produced nothing at that moment, so the result is empty. The same query returns rows when it finishes within the limit, or when it runs in a session with a higher limit or with the default mode `throw`, which is why the behaviour looks inconsistent.

## Reproduce the behaviour

In the following query `sleepEachRow(0.3)` makes every row take 0.3 seconds and `max_block_size = 1` puts every row into its own block, so the query needs about three seconds:

```sql theme={null}
SELECT count()
FROM numbers(10)
WHERE sleepEachRow(0.3) = 0
SETTINGS max_block_size = 1;
```

```text theme={null}
10
```

With a limit of one second and the mode `break`, the query stops after about one second. It returns no rows and reports no error, although `count` without `GROUP BY` normally always returns one row:

```sql theme={null}
SELECT count()
FROM numbers(10)
WHERE sleepEachRow(0.3) = 0
SETTINGS max_block_size = 1, max_execution_time = 1, timeout_overflow_mode = 'break';
```

The aggregation needs all of its input before it can return the first row, so there is nothing to return when the query is stopped. Queries with `ORDER BY` behave the same way. A query that streams rows to the client as it reads them returns the rows that were ready before the limit:

```sql theme={null}
SELECT number
FROM numbers(10)
WHERE sleepEachRow(0.3) = 0
SETTINGS max_block_size = 1, max_execution_time = 1, timeout_overflow_mode = 'break';
```

```text theme={null}
0
1
2
```

In both cases the result is incomplete, and nothing in the response tells the client so.

## Confirm it in the query log

[`system.query_log`](https://clickhouse.com/docs/reference/system-tables/query_log) records the settings that were changed for a query and a counter of stopped executions. Entries are written with a delay of a few seconds; `SYSTEM FLUSH LOGS` writes them immediately.

```sql theme={null}
SYSTEM FLUSH LOGS;

SELECT
    query_duration_ms,
    result_rows,
    exception_code,
    Settings['max_execution_time'] AS max_execution_time,
    Settings['timeout_overflow_mode'] AS timeout_overflow_mode,
    ProfileEvents['OverflowBreak'] AS overflow_break
FROM system.query_log
WHERE event_date >= today() - 1
    AND type = 'QueryFinish'
    AND Settings['timeout_overflow_mode'] = 'break'
    AND ProfileEvents['OverflowBreak'] > 0
ORDER BY event_time;
```

For the two queries above it returns the following (the durations and the exact value of the counter vary a little between runs):

```text theme={null}
   ┌─query_duration_ms─┬─result_rows─┬─exception_code─┬─max_execution_time─┬─timeout_overflow_mode─┬─overflow_break─┐
1. │              1212 │           0 │              0 │ 1                  │ break                 │              8 │
2. │              1204 │           3 │              0 │ 1                  │ break                 │              8 │
   └───────────────────┴─────────────┴────────────────┴────────────────────┴───────────────────────┴────────────────┘
```

A query that was stopped by the time limit in the `break` mode has this signature:

* `type` is `QueryFinish` and `exception_code` is `0`: the query is recorded as successful.
* `query_duration_ms` is equal to `max_execution_time` or a little above it. The limit is checked only at certain points of data processing, so the query overruns it slightly.
* `Settings['timeout_overflow_mode']` is `break`. The `Settings` map contains only the settings that were changed for the query, so an empty value means the default, `throw`.
* `ProfileEvents['OverflowBreak']` is `1` or more. This counter is incremented every time data processing is stopped by a limit whose overflow mode is `break`. Other limits use it too, for example `max_rows_to_read` with `read_overflow_mode = 'break'`, which is why the query also filters on `timeout_overflow_mode`.

To find the runs of one particular query, add `event_time`, `query_id`, `user` and `query` to the select list and filter on `normalized_query_hash` or on a fragment of the query text.

Every replica has its own query log. In ClickHouse Cloud, or on a self-managed cluster, read the logs of all replicas with `clusterAllReplicas`, as described in [Querying across nodes and versions](https://clickhouse.com/docs/products/cloud/features/monitoring/system-tables.md#querying-across-nodes).

## How to fix it

First find out where the two settings come from. Run this in the same kind of connection that the affected application or tool uses:

```sql theme={null}
SELECT name, value, changed
FROM system.settings
WHERE name IN ('max_execution_time', 'timeout_overflow_mode');
```

The defaults are no time limit and the mode `throw`:

```text theme={null}
   ┌─name──────────────────┬─value─┬─changed─┐
1. │ max_execution_time    │ 0     │       0 │
2. │ timeout_overflow_mode │ throw │       0 │
   └───────────────────────┴───────┴─────────┘
```

A changed value can come from the settings of the user or of a settings profile, from a `SET` statement in the session, from the `SETTINGS` clause of the query, or from the connection options of a client, a driver or a BI tool. See [Query-level session settings](https://clickhouse.com/docs/concepts/features/configuration/settings/settings-query-level.md) for the order of priority.

Then choose one of the following:

* **Fail instead of returning an incomplete result.** Set `timeout_overflow_mode = 'throw'` for the queries, users or tools for which an empty or partial result is misleading. The query then fails with `TIMEOUT_EXCEEDED` (code 159), and the client can report the error or retry. For a user:

  ```sql theme={null}
  ALTER USER report_user SETTINGS timeout_overflow_mode = 'throw';
  ```

* **Give the query more time.** Raise `max_execution_time` for this workload, in the same place where the current value is set.

* **Make the query finish within the limit.** Narrow the time range, filter on the columns of the primary key, or read from a pre-aggregated table.

* **Keep `break` only where an incomplete result is acceptable**, for example for queries that stream rows to a tool that shows whatever arrived in time.

## Related settings

* [`max_execution_time_leaf`](https://clickhouse.com/docs/reference/settings/session-settings/max-execution#max_execution_time_leaf) and [`timeout_overflow_mode_leaf`](https://clickhouse.com/docs/reference/settings/session-settings/timeout-overflow-mode#timeout_overflow_mode_leaf) are the same pair of settings for the leaf nodes of distributed queries.
* [Restrictions on query complexity](https://clickhouse.com/docs/concepts/features/configuration/settings/query-complexity.md) describes the other limits and their overflow modes.
* [Setting a limit on query execution time](https://clickhouse.com/docs/resources/support-center/knowledge-base/performance-optimization/query-max-execution-time.md) shows the default behaviour, when the query fails with `TIMEOUT_EXCEEDED`.
