Introduction
Analytical platforms often execute the same SQL queries repeatedly. Business Intelligence (BI) dashboards refresh every few minutes, scheduled reports run on fixed intervals, and monitoring systems continuously request identical metrics. Recomputing these results every time consumes CPU resources and increases response times.
To address this challenge, ClickHouse® offers the Query Cache, a server-side caching mechanism that stores the results of eligible SELECT queries. When an identical query is executed again, ClickHouse® can return the cached result directly from memory instead of reprocessing the data.
For repetitive analytical workloads, this can significantly reduce query latency and lower server resource consumption.
However, Query Cache isn't a universal performance solution. Unlike transactional databases that immediately invalidate cached results after data changes, ClickHouse® follows a different approach that prioritizes analytical performance over strict real-time consistency.
In this article, you'll learn how the Query Cache works, how to configure it effectively, its limitations, and the scenarios where it provides the greatest benefit.
What Is the ClickHouse® Query Cache?
The Query Cache stores the final output of a SELECT query.
When the exact same query is executed again with caching enabled, ClickHouse® skips the entire execution process and returns the previously stored result.
Without Query Cache:
SQL Query
│
Read Data
│
Filter
│
Aggregate
│
Sort
│
Return Result
With Query Cache:
SQL Query
│
Query Cache
│
Return Cached Result
Instead of reading data from disk and performing computations again, ClickHouse® simply retrieves the cached response.
This can dramatically improve performance for expensive analytical queries that are executed frequently.
Understanding Eventual Freshness
One of the most important characteristics of Query Cache is that it does not provide transactional consistency.
Traditional OLTP databases often invalidate cached results immediately whenever underlying data changes.
ClickHouse® takes a different approach because it is designed for analytical workloads where:
- Dashboards refresh every few minutes
- Reports are repeatedly viewed
- Small delays in reflecting new data are usually acceptable
Rather than invalidating cache entries after every insert, ClickHouse® keeps cached results valid for a configurable period known as the Time-To-Live (TTL).
This approach offers several advantages:
- Lower CPU utilization
- Faster repeated query execution
- Simpler cache management
The trade-off is that users may temporarily see stale results until the cache expires.
Enabling Query Cache
Query Cache is disabled by default and must be enabled explicitly.
For an individual query:
SELECT
customer_id,
sum(amount)
FROM sales
GROUP BY customer_id
SETTINGS use_query_cache = 1;
To enable it for an entire session:
SET use_query_cache = 1;
Once enabled, ClickHouse® can store eligible query results and reuse them for subsequent identical queries.
Configuring Query Cache
Server-wide configuration is managed through the <query_cache> section.
Example:
<query_cache>
<max_size_in_bytes>1073741824</max_size_in_bytes>
<max_entries>1024</max_entries>
<max_entry_size_in_bytes>1048576</max_entry_size_in_bytes>
<max_entry_size_in_rows>30000000</max_entry_size_in_rows>
</query_cache>
Important configuration parameters include:
| Setting | Purpose |
|---|---|
max_size_in_bytes |
Maximum memory allocated for the cache |
max_entries |
Maximum number of cached query results |
max_entry_size_in_bytes |
Maximum size allowed for a cached result |
max_entry_size_in_rows |
Maximum number of rows stored in a cache entry |
Because the Query Cache resides entirely in RAM, these limits help prevent excessive memory usage.
Cache Lifetime (TTL)
Every cached query result has a configurable lifetime.
By default, cached results remain valid for 60 seconds.
You can override this using query_cache_ttl.
Example:
SELECT *
FROM sales
SETTINGS
use_query_cache = 1,
query_cache_ttl = 300;
This configuration keeps the cached result valid for five minutes.
Expired cache entries are removed using lazy eviction.
Instead of deleting entries immediately after expiration, ClickHouse® removes expired data only when additional cache space is needed.
This minimizes unnecessary cache maintenance.
Caching Only Expensive Queries
Not every query should be cached.
Simple queries often execute faster than retrieving results from memory.
ClickHouse® provides settings such as:
query_cache_min_query_durationquery_cache_min_query_runs
These allow administrators to cache only:
- Queries that exceed a minimum execution time
- Queries executed multiple times
This prevents the cache from being filled with one-off or inexpensive queries.
Query Cache in ClickHouse® Cloud
For ClickHouse® Cloud deployments, Query Cache is managed through query-level settings instead of editing server configuration files.
This provides greater flexibility while maintaining managed infrastructure.
Query Cache for Subqueries
Modern ClickHouse® versions also support caching subqueries.
Example:
SELECT *
FROM
(
SELECT *
FROM sales
SETTINGS use_query_cache = 1
);
For broader coverage, enable:
SETTINGS use_query_cache_for_subqueries = 1;
This allows individual components of complex analytical queries to benefit from caching.
Common Caveats
1. Cached Results May Become Stale
Suppose the following query is cached:
SELECT
sum(revenue)
FROM sales;
A few seconds later, new records are inserted:
INSERT INTO sales ...
The cached result is not immediately invalidated.
Users continue receiving the previous result until the cache expires.
For financial reporting or real-time monitoring, this behavior may not be acceptable.
2. Query Cache Cannot Replace Proper Optimization
Query Cache does not fix inefficient schema design.
If a query is slow because:
- Primary keys are poorly designed
- Excessive columns are scanned
- JOIN operations are inefficient
- Materialized views are missing
Only repeated executions benefit from caching.
The initial execution still incurs the full cost.
Schema optimization should always come first.
3. Cache Hits Require Repeated Queries
The Query Cache only helps when identical SQL statements are executed repeatedly.
It provides little value for:
- Interactive SQL exploration
- Ad-hoc analytics
- Frequently changing queries
Workloads with predictable query patterns benefit the most.
4. Memory Usage
Every cached result occupies RAM.
Caching numerous large result sets can consume significant memory if limits are not configured carefully.
Best practices include:
- Setting reasonable cache size limits
- Restricting maximum entry size
- Avoiding caching extremely large result sets
5. Highly Dynamic Data
When data changes continuously and users expect immediate visibility of new records, Query Cache offers limited value.
Cached results may expire before reuse or become stale too quickly to justify caching.
Ideal Use Cases
Query Cache performs well for:
- Business Intelligence dashboards
- Scheduled reports
- Frequently accessed analytics
- Executive dashboards
- Monitoring systems where slight staleness is acceptable
These workloads repeatedly execute identical SQL statements, making cache reuse highly effective.
When to Avoid Query Cache
Query Cache is generally unsuitable for:
- Real-time financial systems
- Operational dashboards requiring immediate updates
- Ad-hoc analytical exploration
- Highly dynamic workloads
- Queries where every execution differs
In these scenarios, caching provides minimal benefit while increasing the possibility of stale results.
Best Practices
To maximize the effectiveness of Query Cache:
- Enable caching selectively instead of globally.
- Cache only expensive or frequently executed queries.
- Configure TTL based on business requirements.
- Apply conservative memory limits.
- Continue optimizing schema design and SQL queries.
- Measure cache hit rates before relying on Query Cache for performance improvements.
Treat Query Cache as an optimization layer rather than a substitute for good database design.
Conclusion
The ClickHouse® Query Cache is a valuable feature for accelerating repetitive analytical workloads. By storing complete query results in memory, it reduces query latency, lowers CPU usage, and improves responsiveness for dashboards, reports, and recurring analytics.
However, its design intentionally favors performance over strict transactional consistency. Cached results remain valid until their configured TTL expires, making the feature best suited for workloads where a small amount of data staleness is acceptable.
When combined with efficient schema design, optimized queries, projections, and materialized views, Query Cache becomes another powerful tool for building high-performance analytical systems.
Understanding both its strengths and limitations ensures you can apply it where it delivers the greatest value while avoiding scenarios where fresh, real-time results are essential.
Top comments (0)