Unlock this content

Enter your email to unlock this content for free

By continuing, you agree to our Terms of Service and Privacy Notice, and to receive occasional marketing emails.

Sorting Key and Index Granularity

TL;DR

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

Tinybird is not affiliated with, associated with, or sponsored by ClickHouse, Inc. ClickHouse® is a registered trademark of ClickHouse, Inc.

Sorting Key and Index Granularity | ClickHouse for Developers