ClickHouse Performance Tuning: ORDER BY, Keys & Query Optimization

Key Takeaways

Measure rows and bytes read first. Align ORDER BY with dominant filters, then optimize selected columns, aggregation, joins, memory, and concurrency before adding compute.

Serdar Tekin
Serdar Tekin
Co-Founder & CTO
Updated 13 min read
Written for: Data engineers and developers optimizing ClickHouse tables and queries for production dashboards, event analytics, and high-concurrency analytical workloads.
ClickHousePerformance TuningQuery OptimizationORDER BYSorting Keys
Last reviewed:
Guide
ClickHouse Performance Tuning: ORDER BY, Keys & Query Optimization
rafftechnologies.com/learn

Tune your queries on managed ClickHouse without running servers.

PostgreSQL, MySQL, Valkey, ClickHouse and Kafka, with backups included and high availability when you need it.

ClickHouse performance tuning starts with a simple question: how many rows and bytes does the query read before it returns the result? For MergeTree tables, ORDER BY and the sorting key are usually the first places to look because they control physical row order and determine how effectively ClickHouse can skip data.

Raff Technologies recommends fixing the read path before adding compute. If an analytical query returns a small result but scans a large share of the table, increasing CPU or memory may reduce latency temporarily without fixing the underlying query-design problem.

For most production workloads, use this sequence:

  1. check rows and bytes read;
  2. verify that filters align with the sorting key;
  3. review selected columns and PREWHERE;
  4. inspect aggregation, join, and memory pressure;
  5. test realistic concurrency;
  6. resize infrastructure only after the query path is understood.

This guide covers ClickHouse query optimization across ORDER BY, primary and sorting keys, filters, memory, joins, and concurrency. For lifecycle design, see ClickHouse Partitioning & TTL. For repeated aggregations, see ClickHouse Materialized Views. For infrastructure ownership, see Managed ClickHouse vs Self-Hosted ClickHouse.

ORDER BY is physical storage design in ClickHouse

In MergeTree-family tables, ORDER BY determines how rows are sorted on disk.

That ordering lets ClickHouse maintain a sparse primary index over granules and skip ranges that cannot satisfy a filter.

This is different from an OLTP database where a primary key often means uniqueness and row identity.

ClickHouse conceptMain role
ORDER BYphysical row sorting and data locality
PRIMARY KEYsparse indexing expression, often same as ORDER BY prefix
PARTITION BYcoarse lifecycle boundary
skipping indexesadditional pruning for selected patterns

The sorting key should follow the dominant query filters, not the application's entity identifier by default.

Put frequent selective filters early in the sorting key

Suppose dashboards usually query:

Text
account_id = ? AND event_time BETWEEN ? AND ?

A table ordered by:

Sql
ORDER BY (account_id, event_time)

can keep rows for an account close together and use time as the next pruning dimension.

An ordering of only:

Sql
ORDER BY event_time

may be better for global time-range scans but less efficient for tenant-specific dashboards.

The correct key comes from the actual workload.

Collect the most important queries and record:

  • equality filters;
  • range filters;
  • grouping dimensions;
  • sort requirements;
  • query frequency;
  • expected time range.

Then design the ordering to reduce the data read by the highest-value workload.

Low-cardinality-first is a useful heuristic, not a universal rule

ClickHouse guidance often recommends considering lower-cardinality columns earlier when they are frequently filtered, followed by more selective dimensions and time.

But no universal column order replaces testing.

For multi-tenant analytics, an account/tenant dimension can be valuable early even when cardinality is high if nearly every customer-facing query filters by it.

The test is whether the key improves pruning for the workload.

Measure:

  • rows read;
  • bytes read;
  • query duration;
  • mark/granule pruning;
  • compression and storage impact.

A key that looks elegant on paper but reads most of the table is not effective.

Primary key does not need to enforce uniqueness

ClickHouse primary keys are sparse indexes and do not provide uniqueness enforcement like a conventional transactional primary key.

Duplicate rows are possible unless the table engine and ingestion model handle them deliberately.

This means a source event ID should not automatically become the ClickHouse primary/sorting key just because it is unique.

A random/high-cardinality ID often provides poor locality for time- or account-oriented analytical queries.

Keep business uniqueness and ClickHouse physical ordering as separate design questions.

Measure rows and bytes read before tuning CPU

Query latency should be interpreted alongside scan volume.

A useful diagnosis table is:

SymptomLikely direction
High rows read, few rows returnedfiltering/order-key problem
Low rows read, high CPUexpensive aggregation/expression/join
High memory, GROUP BYcardinality or aggregation strategy
Good single query, poor concurrent latencyconcurrency/resource contention
Query slowed after data growthpruning/working-set issue
Time range reads too muchorder key / partition / predicate issue

If a query returns 500 rows but reads 500 million, adding vCPU should not be the first response.

Reduce unnecessary reads first.

PREWHERE can reduce I/O for selective filters

ClickHouse can use PREWHERE to read filtering columns first and avoid reading other columns for rows that will be discarded.

The optimizer can apply PREWHERE automatically in many cases, but query design still matters.

Avoid selecting wide unused columns:

Sql
SELECT *

when a dashboard needs only a few fields.

Columnar storage is most efficient when the query reads only the required columns.

A narrow projection plus selective filters can reduce both I/O and decompression work substantially.

Data types affect memory and scan efficiency

Choose the narrowest correct types.

Examples include:

  • avoid storing numbers as String;
  • use appropriate integer widths;
  • use Date/DateTime types for time;
  • consider LowCardinality for suitable repeated string dimensions;
  • avoid unnecessary Nullable use when null semantics are not required.

Data type decisions affect:

  • compressed size;
  • memory during grouping/joining;
  • comparison cost;
  • index effectiveness.

Do not change types solely for theoretical savings; validate compatibility and measured query behavior.

GROUP BY memory follows cardinality and concurrency

Aggregations build in-memory state according to grouping dimensions and aggregate functions.

A query grouping by a low-cardinality region is very different from grouping by millions of user IDs.

Estimate group cardinality before running large production aggregations.

Watch:

  • peak query memory;
  • number of groups;
  • concurrent heavy queries;
  • spill/external aggregation behavior;
  • result-set size.

If the same high-cardinality aggregation runs constantly, a materialized view or pre-aggregated table may be more efficient than repeating it at query time.

Joins should be sized from the right-hand side and workload

ClickHouse supports several join algorithms and can adapt based on settings and memory.

For production designs, ask:

  • how large is each side?
  • can dimensions be denormalized at ingestion?
  • is the joined table reused frequently?
  • does a dictionary or precomputed representation fit?
  • does join memory fit under concurrency?

Do not denormalize everything blindly, but remember that analytical systems often benefit from moving stable relationships into a query-friendly structure.

A complex join repeated for every dashboard request can become a better candidate for ingestion-time transformation.

Materialized views fit repeated expensive aggregations

When the same stable aggregation dominates query cost, pre-aggregation can reduce rows and CPU dramatically.

Use ClickHouse Materialized Views when:

  • query pattern is stable;
  • aggregate grain is known;
  • source data can rebuild the result;
  • added write/storage cost is acceptable.

Do not create a materialized view before fixing an obviously poor base-table ordering key.

The two optimizations solve different layers.

Query concurrency can be the bottleneck even when one query is fast

Benchmarking one query at a time can hide production contention.

Test representative concurrency levels, for example:

Text
1 query 10 concurrent queries 50 concurrent queries

As concurrency rises, track changes in:

  • p95 latency;
  • memory use;
  • CPU utilization;
  • disk throughput;
  • merge activity;
  • query failures.

A query that uses 8 GB of memory comfortably alone may become impossible when ten copies run together.

Capacity planning should use the production concurrency model, not only individual query speed.

Background merges compete with queries for resources

ClickHouse performs merges continuously for MergeTree tables.

High ingest, too many small parts, TTL work, or mutations can increase background CPU and disk activity.

If query latency worsens only during merge pressure, the fix may involve:

  • larger insert batches;
  • fewer parts;
  • scheduling heavy mutations;
  • storage headroom;
  • capacity separation.

Use ClickHouse Monitoring to correlate foreground query cost with background work.

Benchmark with realistic data distribution

A query benchmark on 1% of production data can choose a completely different execution profile.

Use representative:

  • table size;
  • time range;
  • tenant distribution;
  • high/low-cardinality dimensions;
  • hot and cold periods;
  • concurrent load.

Record:

Text
query pattern rows read bytes read elapsed time memory result rows concurrency

Then compare before and after schema or query changes.

This produces evidence that survives data growth better than an isolated millisecond result.

Managed ClickHouse reduces infrastructure tuning, not query design

Raff Managed ClickHouse reduces the work around database hosts, monitoring, backups, storage expansion, scaling workflows, and optional HA.

The data/application team still owns:

  • ORDER BY and primary-key design;
  • query filters;
  • selected columns;
  • joins;
  • aggregation cardinality;
  • materialized views;
  • workload/concurrency behavior.

At Raff, the performance rule is: before resizing ClickHouse, identify how many rows and bytes the important query reads and whether the physical ordering is helping it skip data.

Query-performance checklist

  • Top query patterns are ranked by business importance and frequency.
  • ORDER BY matches dominant filters.
  • Primary key is treated as sparse-index design, not uniqueness enforcement.
  • Rows/bytes read are measured.
  • Queries select only required columns.
  • Data types fit the workload.
  • GROUP BY cardinality and memory are understood.
  • Repeated joins are reviewed for denormalization/precomputation.
  • Repeated aggregations are evaluated for materialized views.
  • Tests include realistic concurrency.
  • Merge/mutation pressure is correlated with query latency.
  • Capacity increases follow measured resource constraints.

Conclusion

ClickHouse performance tuning begins with physical data access.

Design ORDER BY around the dominant analytical filters, measure rows and bytes read, then optimize aggregation, joins, memory, and concurrency. Partitioning, materialized views, and larger compute can all help, but they work best after the base table makes important queries read as little unnecessary data as possible.

Use managed ClickHouse infrastructure while keeping query design and workload optimization with your data team.

Was this article helpful?
Serdar Tekin
Serdar Tekin
Co-Founder & CTO

Co-Founder & CTO at Raff, focused on cloud infrastructure, systems architecture, security, reliability, and production operations.

Tune your queries on managed ClickHouse without running servers.

PostgreSQL, MySQL, Valkey, ClickHouse and Kafka, with backups included and high availability when you need it.

14-day money-back guarantee

Frequently asked questions

Why is ORDER BY important in ClickHouse?
For MergeTree tables it determines physical row ordering, which enables sparse-index data skipping and strongly affects how much data important queries must read.
Does a ClickHouse primary key enforce uniqueness?
No. It defines a sparse index expression for data skipping and does not provide OLTP-style uniqueness enforcement.
What should I check first when a ClickHouse query is slow?
Measure rows and bytes read, then inspect whether ORDER BY and filters allow useful pruning before increasing compute.
Should I partition ClickHouse to improve query performance?
Partitioning can skip coarse partitions when predicates align, but ORDER BY is usually the more important query-performance design. Partition primarily for lifecycle operations.
When should I use a materialized view for performance?
Use one when the same stable, expensive transformation or aggregation runs repeatedly and the derived result can be validated and rebuilt.
Why does a fast ClickHouse query become slow under concurrency?
Concurrent analytical queries compete for memory, CPU, disk bandwidth, and cache. Capacity planning must test realistic simultaneous workload, not only single-query latency.