
Unlock this content
Enter your email to unlock this content for free
Sorting Key and Index Granularity
The sorting key determines the physical order of data on disk and enables data skipping. Index granularity controls how often the primary index is stored. Together, they determine how efficiently ClickHouse can skip data during queries. Put frequently filtered columns first in the sorting key, use low-cardinality columns early, and choose appropriate index granularity based on your data patterns.
The sorting key and index granularity are fundamental to ClickHouse query performance. Understanding how they work together is essential for writing efficient queries.
What is a Sorting Key?
The sorting key (defined by ORDER BY in table creation) determines the physical order of data on disk. ClickHouse stores data sorted by the sorting key, which enables:
- Data skipping: ClickHouse can skip entire blocks of data that don't match query filters
- Efficient range queries: Queries filtering by sorting key columns are very fast
- Partition pruning: Works together with partition keys to reduce data scanned