Skip to main content

Why is my primary key not used? How can I check?

Β· 6 min read

Users may see cases where their query is slower than expected, in the belief they are ordering or filtering by a primary key. In this article we show how users can confirm the key is used, highlighting common reasons its not.

Create table​

Consider the following simple table:

CREATE TABLE logs
(
`code` LowCardinality(String),
`timestamp` DateTime64(3)
)
ENGINE = MergeTree
ORDER BY (code, toUnixTimestamp(timestamp))

Note how our ordering key includes toUnixTimestamp(timestamp) as the second entry.

Populate data​

Populate this table with 100m rows:

INSERT INTO logs SELECT
['200', '404', '502', '403'][toInt32(randBinomial(4, 0.1)) + 1] AS code,
now() + toIntervalMinute(number) AS timestamp
FROM numbers(100000000)

0 rows in set. Elapsed: 15.845 sec. Processed 100.00 million rows, 800.00 MB (6.31 million rows/s., 50.49 MB/s.)

SELECT count()
FROM logs

β”Œβ”€β”€β”€count()─┐
β”‚ 100000000 β”‚ -- 100.00 million
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜

1 row in set. Elapsed: 0.002 sec.

Basic filtering​

If we filter by code we can see the number of rows scanned in the output. - 49.15 thousand. Notice how this is a subset of the total 100m rows.

SELECT count() AS c
FROM logs
WHERE code = '200'

β”Œβ”€β”€β”€β”€β”€β”€β”€β”€c─┐
β”‚ 65607542 β”‚ -- 65.61 million
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜

1 row in set. Elapsed: 0.021 sec. Processed 49.15 thousand rows, 49.17 KB (2.34 million rows/s., 2.34 MB/s.)
Peak memory usage: 92.70 KiB.

Furthermore, we can confirm the use of the index with the EXPLAIN indexes=1 clause:

EXPLAIN indexes = 1
SELECT count() AS c
FROM logs
WHERE code = '200'

β”Œβ”€explain────────────────────────────────────────────────────────────┐
β”‚ Expression ((Project names + Projection)) β”‚
β”‚ AggregatingProjection β”‚
β”‚ Expression (Before GROUP BY) β”‚
β”‚ Filter ((WHERE + Change column names to column identifiers)) β”‚
β”‚ ReadFromMergeTree (default.logs) β”‚
β”‚ Indexes: β”‚
β”‚ PrimaryKey β”‚
β”‚ Keys: β”‚
β”‚ code β”‚
β”‚ Condition: (code in ['200', '200']) β”‚
β”‚ Parts: 3/3 β”‚
β”‚ Granules: 8012/12209 β”‚
β”‚ ReadFromPreparedSource (_minmax_count_projection) β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜

Notice how the number of granules scanned 8012 is a fraction of the total 12209. The section higlighted below, confirms use of the primary key code.

PrimaryKey
Keys:
code

Granules are the unit of data processing in ClickHouse, with each typically holding 8192 rows. For further details on granules and how they are filtered we recommend reading this guide.

Note

Filtering on keys later in an ordering key will not be as efficient as filtering on those that are earlier in the tuple. For reasons why, see here

Multi-key filtering​

Suppose we filter, by code and timestamp:

SELECT count()
FROM logs
WHERE (code = '200') AND (timestamp >= '2025-01-01 00:00:00') AND (timestamp <= '2026-01-01 00:00:00')

β”Œβ”€count()─┐
β”‚ 689742 β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜

1 row in set. Elapsed: 0.008 sec. Processed 712.70 thousand rows, 6.41 MB (88.92 million rows/s., 799.27 MB/s.)


EXPLAIN indexes = 1
SELECT count()
FROM logs
WHERE (code = '200') AND (timestamp >= '2025-01-01 00:00:00') AND (timestamp <= '2026-01-01 00:00:00')

β”Œβ”€explain───────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────┐
β”‚ Expression ((Project names + Projection)) β”‚
β”‚ Aggregating β”‚
β”‚ Expression (Before GROUP BY) β”‚
β”‚ Expression β”‚
β”‚ ReadFromMergeTree (default.logs) β”‚
β”‚ Indexes: β”‚
β”‚ PrimaryKey β”‚
β”‚ Keys: β”‚
β”‚ code β”‚
β”‚ toUnixTimestamp(timestamp) β”‚
β”‚ Condition: and((toUnixTimestamp(timestamp) in (-Inf, 1767225600]), and((toUnixTimestamp(timestamp) in [1735689600, +Inf)), (code in ['200', '200']))) β”‚
β”‚ Parts: 3/3 β”‚
β”‚ Granules: 87/12209 β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜

13 rows in set. Elapsed: 0.002 sec.

In this case both ordering keys are used to filter rows, resulting in the need to only read 87 granules.

Using keys in sorting​

ClickHouse can also exploit ordering keys for efficient sorting. Specifically,

When the optimize_read_in_order setting is enabled (by default), the ClickHouse server uses the table index and reads the data in order of the ORDER BY key. This allows us to avoid reading all data in case of specified LIMIT. So, queries on big data with small limits are processed faster. See here and here for further details.

This, however, requires alignment of the keys used.

For example, consider this query:

SELECT *
FROM logs
WHERE (code = '200') AND (timestamp >= '2025-01-01 00:00:00') AND (timestamp <= '2026-01-01 00:00:00')
ORDER BY timestamp ASC
LIMIT 10

β”Œβ”€code─┬───────────────timestamp─┐
β”‚ 200 β”‚ 2025-01-01 00:00:01.000 β”‚
β”‚ 200 β”‚ 2025-01-01 00:00:45.000 β”‚
β”‚ 200 β”‚ 2025-01-01 00:01:01.000 β”‚
β”‚ 200 β”‚ 2025-01-01 00:01:45.000 β”‚
β”‚ 200 β”‚ 2025-01-01 00:02:01.000 β”‚
β”‚ 200 β”‚ 2025-01-01 00:03:01.000 β”‚
β”‚ 200 β”‚ 2025-01-01 00:03:45.000 β”‚
β”‚ 200 β”‚ 2025-01-01 00:04:01.000 β”‚
β”‚ 200 β”‚ 2025-01-01 00:05:45.000 β”‚
β”‚ 200 β”‚ 2025-01-01 00:06:01.000 β”‚
└──────┴─────────────────────────

10 rows in set. Elapsed: 0.009 sec. Processed 712.70 thousand rows, 6.41 MB (80.13 million rows/s., 720.27 MB/s.)
Peak memory usage: 125.50 KiB.

We can confirm that the optimization has not been exploited here by using EXPLAIN pipeline:

EXPLAIN PIPELINE
SELECT *
FROM logs
WHERE (code = '200') AND (timestamp >= '2025-01-01 00:00:00') AND (timestamp <= '2026-01-01 00:00:00')
ORDER BY timestamp ASC
LIMIT 10

β”Œβ”€explain───────────────────────────────────────────────────────────────────────┐
β”‚ (Expression) β”‚
β”‚ ExpressionTransform β”‚
β”‚ (Limit) β”‚
β”‚ Limit β”‚
β”‚ (Sorting) β”‚
β”‚ MergingSortedTransform 12 β†’ 1 β”‚
β”‚ MergeSortingTransform Γ— 12 β”‚
β”‚ LimitsCheckingTransform Γ— 12 β”‚
β”‚ PartialSortingTransform Γ— 12 β”‚
β”‚ (Expression) β”‚
β”‚ ExpressionTransform Γ— 12 β”‚
β”‚ (Expression) β”‚
β”‚ ExpressionTransform Γ— 12 β”‚
β”‚ (ReadFromMergeTree) β”‚
β”‚ MergeTreeSelect(pool: ReadPool, algorithm: Thread) Γ— 12 0 β†’ 1 β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜

15 rows in set. Elapsed: 0.004 sec.

The line MergeTreeSelect(pool: ReadPool, algorithm: Thread) here does not indicate the use of the optimization but rather a standard read. This is caused by our table ordering key using toUnixTimestamp(Timestamp) NOT timestamp. Rectifying this mismatch addresses the issue:

EXPLAIN PIPELINE
SELECT *
FROM logs
WHERE (code = '200') AND (timestamp >= '2025-01-01 00:00:00') AND (timestamp <= '2026-01-01 00:00:00')
ORDER BY toUnixTimestamp(timestamp) ASC
LIMIT 10

β”Œβ”€explain──────────────────────────────────────────────────────────────────────────┐
β”‚ (Expression) β”‚
β”‚ ExpressionTransform β”‚
β”‚ (Limit) β”‚
β”‚ Limit β”‚
β”‚ (Sorting) β”‚
β”‚ MergingSortedTransform 3 β†’ 1 β”‚
β”‚ BufferChunks Γ— 3 β”‚
β”‚ (Expression) β”‚
β”‚ ExpressionTransform Γ— 3 β”‚
β”‚ (Expression) β”‚
β”‚ ExpressionTransform Γ— 3 β”‚
β”‚ (ReadFromMergeTree) β”‚
β”‚ MergeTreeSelect(pool: ReadPoolInOrder, algorithm: InOrder) Γ— 3 0 β†’ 1 β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜

13 rows in set. Elapsed: 0.003 sec.