Overview
This article explains how ClickHouse optimizes Top-N queries (ORDER BY ... LIMIT N) using granule-level data skipping indexes. By leveraging min/max metadata stored in data-skipping indexes, ClickHouse can eliminate entire granules before reading any data, achieving 5-10x faster query execution and 10-100x less data read, with both static (no predicates) and dynamic (with predicates) filtering approaches.
What You'll Learn
How ClickHouse uses minmax data skipping indexes to eliminate granules before reading any data for Top-N queries
How static Top-N filtering works by preselecting granules based on min/max metadata without predicates
How dynamic Top-N threshold filtering progressively tightens pruning during query execution with predicates
When to enable use_skip_indexes_for_top_k and use_top_k_dynamic_filtering settings for optimal Top-N performance
Why granule-level metadata pruning becomes increasingly impactful as table sizes grow into billions of rows
Prerequisites & Requirements
- Understanding of ClickHouse granules as the smallest processing units (8,192 rows by default)
- Familiarity with SQL ORDER BY and LIMIT clauses for Top-N query patterns
- Basic understanding of data skipping indexes and primary indexes in ClickHouse
- Experience running analytical queries on large datasets in ClickHouse(optional)
Key Questions Answered
How does ClickHouse optimize Top-N queries using data skipping indexes?
What is the difference between static and dynamic Top-N filtering in ClickHouse?
How do I enable granule-level data skipping for Top-N queries in ClickHouse?
What existing Top-N optimizations does ClickHouse already apply before granule skipping?
How much faster are ClickHouse Top-N queries with granule-level data skipping?
Does ClickHouse Top-N data skipping work on very large tables with billions of rows?
What is streaming for secondary indices in ClickHouse and how does it relate to Top-N?
Why is metadata-driven Top-N pruning especially useful with object storage architectures?
Key Statistics & Figures
Technologies & Tools
Some links below are affiliate links. We may earn a commission if you make a purchase.
Key Actionable Insights
1Add minmax data skipping indexes on columns frequently used in ORDER BY clauses for Top-N queries. This enables ClickHouse to compare granule-level min/max metadata against Top-N thresholds and skip entire granules without reading any data, dramatically reducing I/O.This is most impactful on large tables (billions+ rows) and cold cache scenarios where avoiding unnecessary disk or network reads provides the greatest benefit.
2Enable the use_skip_indexes_for_top_k setting for Top-N queries without predicates to activate static granule-level pruning. In benchmarks, this reduced rows processed from 100 million to 163 thousand and data read from 1.2 GB to 4.95 MB.This setting is available in ClickHouse 25.12 and works by preselecting only the granules with the most promising min/max values for the ORDER BY column.
3For Top-N queries with WHERE clauses, enable both use_top_k_dynamic_filtering and use_skip_indexes_on_data_read alongside use_skip_indexes_for_top_k. This activates dynamic threshold filtering that progressively prunes more granules as better Top-N candidates are found during execution.Dynamic filtering achieved 10x speedup in benchmarks by reducing processed rows from 100 million to 7 million. The threshold tightens automatically as execution progresses.
4Recognize that these new optimizations compose with existing Top-N techniques (streaming execution, read-in-order, and lazy reading). Each addresses a different bottleneck, so combining them provides cumulative benefits rather than requiring you to choose one approach.Streaming limits memory usage, read-in-order avoids sorting, lazy reading defers non-order column I/O, and data skipping eliminates granules entirely—all applicable independently within a single query.
5Consider the impact of granule-level pruning when designing table schemas and choosing primary keys. Since data is organized into granules of 8,192 rows by default, the distribution and ordering of ORDER BY column values across granules affects how many granules can be skipped.In practice, more than the minimum number of granules may be read because data is processed in blocks spanning multiple adjacent granules and multiple threads process data in parallel.
6Prioritize this optimization for cloud-native and disaggregated compute architectures where data resides in object storage. Metadata-driven pruning avoids not just CPU work but also network I/O and latency from unnecessary remote storage reads.This makes the optimization especially valuable in environments where storage latency is higher than local disk, turning Top-N into a metadata problem that can be resolved before touching the actual data.