> ## 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.

# How to get the history of the size of a ClickHouse Cloud service from system tables

> Query system.asynchronous_metric_log to see the number of replicas, the total CPU and the total memory of a ClickHouse Cloud service over time, and when they changed.

Every replica of a ClickHouse Cloud service records the CPU and memory allocated to it as the `CGroupMaxCPU` and `CGroupMemoryTotal` metrics in [`system.asynchronous_metric_log`](https://clickhouse.com/docs/reference/system-tables/asynchronous_metric_log). Aggregate these rows per minute to get the number of replicas, the total CPU and the total memory of the service over time. Compare every minute with the previous one to get the list of moments when the size changed.

## Replicas, CPU and memory per minute

Set `start_time` and `end_time` to the period you are interested in:

```sql theme={null}
WITH
    toDateTime('2026-11-30 00:00:00') AS start_time,
    toDateTime('2026-12-01 00:00:00') AS end_time
SELECT
    t,
    count() AS replicas,
    sum(cpu_cores) AS total_cpu_cores,
    formatReadableSize(sum(memory_bytes)) AS total_memory
FROM
(
    SELECT
        toStartOfMinute(event_time) AS t,
        hostname,
        maxIf(value, metric = 'CGroupMaxCPU') AS cpu_cores,
        maxIf(value, metric = 'CGroupMemoryTotal') AS memory_bytes
    FROM clusterAllReplicas('default', merge('system', '^asynchronous_metric_log'))
    WHERE metric IN ('CGroupMaxCPU', 'CGroupMemoryTotal')
        AND event_date BETWEEN toDate(start_time) AND toDate(end_time)
        AND event_time >= start_time AND event_time < end_time
    GROUP BY t, hostname
)
GROUP BY t
ORDER BY t ASC
SETTINGS skip_unavailable_shards = 1;
```

The inner query returns the allocation of every replica in every minute. The `hostname` column is the name of the server that wrote the row, so the outer query counts the replicas that were running in that minute and adds up their CPU cores and memory.

## Only the moments when the size changed

For a long period, keep only the minutes in which the number of replicas, the CPU or the memory differs from the previous minute:

```sql theme={null}
WITH
    toDateTime('2026-11-01 00:00:00') AS start_time,
    toDateTime('2026-12-01 00:00:00') AS end_time
SELECT
    t,
    replicas,
    total_cpu_cores,
    formatReadableSize(total_memory_bytes) AS total_memory
FROM
(
    SELECT
        *,
        lagInFrame((replicas, total_cpu_cores, total_memory_bytes)) OVER (ORDER BY t ASC) AS previous
    FROM
    (
        SELECT
            t,
            count() AS replicas,
            sum(cpu_cores) AS total_cpu_cores,
            sum(memory_bytes) AS total_memory_bytes
        FROM
        (
            SELECT
                toStartOfMinute(event_time) AS t,
                hostname,
                maxIf(value, metric = 'CGroupMaxCPU') AS cpu_cores,
                maxIf(value, metric = 'CGroupMemoryTotal') AS memory_bytes
            FROM clusterAllReplicas('default', merge('system', '^asynchronous_metric_log'))
            WHERE metric IN ('CGroupMaxCPU', 'CGroupMemoryTotal')
                AND event_date BETWEEN toDate(start_time) AND toDate(end_time)
                AND event_time >= start_time AND event_time < end_time
            GROUP BY t, hostname
        )
        GROUP BY t
        ORDER BY t ASC WITH FILL FROM toStartOfMinute(start_time) TO end_time STEP INTERVAL 1 MINUTE
    )
)
WHERE (replicas, total_cpu_cores, total_memory_bytes) != previous
ORDER BY t ASC
SETTINGS skip_unavailable_shards = 1;
```

`WITH FILL` adds a row of zeros for every minute of the period without data, including the minutes before the first and after the last row of the log, and [`lagInFrame`](https://clickhouse.com/docs/reference/functions/window-functions/lagInFrame) returns the values of the previous minute. The first row of the result is the size at the start of the period or, if the service was not running then, its first start. Every following row is a change, and a stop that lasts until the end of the period is the last row.

In both queries, set `end_time` at least one minute before the current time. The server writes rows to the log tables with a delay of a few seconds, so later minutes have no rows yet, or not all of them, and the result would show them as a stop or as a smaller size.

## How to read the result

* The numbers are totals over all replicas. To see every replica separately, run the inner `SELECT` of the first example together with its `WITH` clause.
* ClickHouse Cloud scales and upgrades a service by adding new replicas before it removes the old ones, see [make before break](https://clickhouse.com/docs/products/cloud/features/autoscaling/make-before-break.md). While both exist, the result shows more replicas, CPU and memory than the service is configured for.
* A row with zero replicas is not a measured size. It means that the log tables have no rows for that minute. Usually the service was idle or stopped, but the query cannot tell this apart from a minute in which running replicas wrote no metrics, and it reports the next minute with rows as a start.
* The queries read the log tables of the replicas that exist when you run them. The make before break page says that 30 days of system table data are kept when replicas are replaced, so do not expect older history.
* Queries on system tables [prevent the service from idling](https://clickhouse.com/docs/products/cloud/features/monitoring/system-tables.md#system-considerations), so do not run them on a tight schedule.

## Related pages

* [How to check the CPU and memory allocated to and used by a ClickHouse Cloud service with SQL](https://clickhouse.com/docs/resources/support-center/knowledge-base/cloud-services/cloud-service-cpu-memory-allocation-and-usage-sql.md)
* [Scheduled scaling](https://clickhouse.com/docs/products/cloud/features/autoscaling/scheduled-scaling.md#handling-bursty-workloads), which has the query for the current size of every replica
* [Automatic scaling](https://clickhouse.com/docs/products/cloud/features/autoscaling/overview.md)
* [Querying ClickHouse's system database](https://clickhouse.com/docs/products/cloud/features/monitoring/system-tables.md)
