altinity-expert-clickhouse-caches
Analyzes the ClickHouse mark cache, uncompressed cache, query cache and compiled expression cache, their hit ratios and their RAM footprint. Use when cache hit ratios are poor, caches take too much RAM, mark counts look excessive, or cache sizing needs tuning.
How do I install this agent skill?
npx skills add https://github.com/altinity/altinity-skills --skill altinity-expert-clickhouse-cachesIs this agent skill safe to install?
- Gen Agent Trust Hubpass
The skill provides diagnostic SQL queries for ClickHouse cache systems. It is generally safe, with a minor potential for indirect prompt injection through user-defined metadata in database logs.
- Socketpass
No alerts
- Snykwarn
Risk: MEDIUM · 1 issue
What does this agent skill do?
Cache analysis and tuning
Answers "are the caches sized right and are they helping?" from system.metrics, system.asynchronous_metrics, system.events, system.parts, system.server_settings and system.metric_log.
Run altinity-expert-clickhouse-connection first if the connection mode, cluster and time window are not yet established.
Query packs
checks.sql— 12 checks: mark cache health (hit ratio and RAM share), uncompressed cache health, query cache, compiled expression cache, mark bytes per table, primary-key memory per table, cache events over time, current cache server settings, mark-cache sizing analysis, a hit-ratio diagnostic over the tables queried today, tables holding excessive marks, and the mark-cache hit ratio minute by minute for the last hour. Check caches-07 needssystem.asynchronous_metric_logand caches-12 needssystem.metric_log; skip them when those tables are absent.reference.md— background (settings, sizing, anti-patterns); read only when you need to explain a recommendation.
How to run the query packs
- Read each pack file from this skill's directory (the skill loader prints the directory path).
- Run statements one at a time, never a whole file. Statements end with
;and start with a-- @check <id> <title>header; keep the id with its result. - Honor
-- @requires: skip the statement when the named table is missing, whenkeeperis required and the server has no Keeper/ZooKeeper, or when the version condition is not met. List skipped ids with the reason. - Keep
{cluster}as written when a cluster macro exists; otherwise apply the connection skill's rewrite rule. Any other{placeholder}is a template variable: substitute a real value first or skip the statement. - On an error, record the check id and the first line of the error, then continue. Only for
UNKNOWN_IDENTIFIER, runDESCRIBE TABLE system.<table>and drop the missing column. - A
severitycolumn is the verdict for that row. Copy it; do not re-grade.
Interpretation rules
- caches-01 and caches-02 return two verdict columns,
hit_severityandsize_severity. Copy both; a cache can be well sized and still useless, or effective and still too large. - Mark cache hit ratio below 0.7 is
Moderate, below 0.5Major, below 0.3Critical. The causes are, in order: the cache is too small for the working set, queries touch many different tables, or the workload is many small queries against cold data. Use caches-10 to tell these apart before resizing. - Mark cache above 15 percent of RAM is a finding even with a perfect hit ratio. Fix the cause in this order: raise
index_granularityon the tables in caches-11, let merges reduce the number of tiny parts, drop unused tables, and only then lowermark_cache_size. Never lowerindex_granularityto shrink the cache; a smaller granularity produces more marks, not fewer. - caches-09 gives the target: ideal mark cache is min(total marks on disk, 15 percent of RAM). If total marks already exceed that, the cache cannot hold the working set and the schema is the problem.
- An uncompressed cache hit ratio near zero with many misses is
Moderate, notCritical. Size 0 (disabled) is a legitimate production choice; the cache only pays off for repeated small point reads withuse_uncompressed_cacheenabled per query or per profile. - caches-08 lists server settings only.
mark_cache_sizeanduncompressed_cache_sizeare server settings; the query cache size lives in the server config as<query_cache><max_size_in_bytes>and may therefore be absent from the result.use_query_cacheanduse_uncompressed_cacheare query-level settings and never appear here. - Query cache misses dominated by non-deterministic or one-off queries are expected. The query cache only helps identical, repeated, deterministic queries.
- caches-06 primary-key memory is not a cache and is not evictable. Report it separately from cache sizing.
Deep-dive statements
Marks per table inside one database, to find what fills the mark cache:
SELECT table, count() AS parts, sum(marks) AS marks, formatReadableSize(sum(marks_bytes)) AS marks_size, formatReadableSize(sum(bytes_on_disk)) AS data_size FROM system.parts WHERE active AND database = '{database}' GROUP BY table ORDER BY sum(marks_bytes) DESC LIMIT 20;
The index_granularity actually in force for one table, to confirm before recommending a change:
SELECT database, table, engine, create_table_query FROM system.tables WHERE database = '{database}' AND name = '{table}';
Largest query cache entries, when caches-03 shows the cache filling up:
SELECT substring(query, 1, 120) AS query_preview, formatReadableSize(result_size) AS result_size, stale, shared, expires_at FROM system.query_cache ORDER BY result_size DESC LIMIT 20;
Report format
- Header: connection mode, cluster or "single node", ClickHouse version, time window.
- Findings: table with columns
check,severity,object,evidence,recommendation; one row per finding, Critical first. Evidence quotes the numbers from the result rows. - OK checks: one line listing the check ids that returned no problem rows.
- Skipped and failed checks: id and reason or first error line. Never omit this section.
- Next steps: skills to load next and immediate actions.
Next skills
- Caches are a large share of total RAM, or the server is near its memory limit → load skill
altinity-expert-clickhouse-memory - Poor hit ratio together with high disk read IO → load skill
altinity-expert-clickhouse-storage - Excessive marks from small
index_granularityor a wide ORDER BY → load skillaltinity-expert-clickhouse-schema - Many tiny parts inflating the mark count → load skill
altinity-expert-clickhouse-merges - Query cache misses or repeated expensive queries → load skill
altinity-expert-clickhouse-reporting
How can the creator link this skill?
Add the canonical catalog link to the repository README so users can inspect current installs and available audits. The publishing guide covers the complete discovery path.
<a href="https://skillzs.dev/skills/altinity/altinity-skills/altinity-expert-clickhouse-caches">View altinity-expert-clickhouse-caches on skillZs</a>