Senior
Tell me about indexes in ClickHouse.
sobes.tech AI
Answer from AI
In ClickHouse, indexes work differently than in traditional relational databases. Key features:
- ClickHouse uses a primary key for sorting data within partitions, which speeds up search and filtering.
- Instead of classic B-trees, sparse indexes are used — they store minimum and maximum key values for data blocks (e.g., for 64K rows).
- When executing a query, ClickHouse quickly determines which data blocks may contain the needed values and reads only those, significantly speeding up queries.
- There are also skip indexes (e.g., minmax, set, bloom filter) that help skip unnecessary data during filtering.
Example of using a primary key:
CREATE TABLE visits (
UserID UInt64,
VisitDate Date,
Duration UInt32
) ENGINE = MergeTree()
ORDER BY (UserID, VisitDate);
Here, data will be sorted by UserID and VisitDate, which accelerates queries with filters on these fields.
Thus, indexes in ClickHouse are more mechanisms of sorting and metadata for skipping data blocks, rather than classic indexes like in OLTP systems.