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

# optimize_aggregation_in_order_* session settings

> ClickHouse session settings in the optimize_aggregation_in_order_* generated group.

These settings are available in [system.settings](https://clickhouse.com/docs/reference/system-tables/settings.md) and are autogenerated from [source](https://github.com/ClickHouse/ClickHouse/blob/master/src/Core/Settings.cpp).

## optimize\_aggregation\_in\_order

Type

Bool

Default

0

Enables [GROUP BY](https://clickhouse.com/docs/reference/statements/select/group-by.md) optimization in [SELECT](https://clickhouse.com/docs/reference/statements/select/index.md) queries for aggregating data in corresponding order in [MergeTree](https://clickhouse.com/docs/reference/engines/table-engines/mergetree-family/mergetree.md) tables.

It also applies to a single `uniqExact` (or `count(DISTINCT)`) without `GROUP BY` over a table sorted by its argument: the distinct values are counted as they arrive in order, so they are not kept in memory. With several threads, the read is split into ranges of the sorting key that are counted in parallel; when it cannot be split (for example, for a floating-point argument or a function of the sorting key), the usual aggregation is used.

Projections are chosen before the order of aggregation. If a [projection](https://clickhouse.com/docs/reference/engines/table-engines/mergetree-family/mergetree.md#projections) reads fewer granules than the table, it is used even when it is not sorted by the `GROUP BY` key, and the aggregation then keeps all the groups in memory instead of aggregating in order.

Possible values:

* 0 — `GROUP BY` optimization is disabled.
* 1 — `GROUP BY` optimization is enabled.

**See Also**

* [GROUP BY optimization](https://clickhouse.com/docs/reference/statements/select/group-by.md#group-by-optimization-depending-on-table-sorting-key)

## optimize\_aggregation\_in\_order\_limit

Type

Bool

Default

1

When enabled and aggregation in order is active, pushes LIMIT into the aggregation step to enable early termination after producing enough groups. This reduces the amount of data read when ORDER BY matches the GROUP BY key prefix. May reduce the value reported by `rows_before_limit_at_least`; use `exact_rows_before_limit` if exact counts are needed.
