Index of every page on this site: llms.txt
All skills
jeremylongshore avatar

/clickhouse-core-workflow-b

@0d57af8
Run it yourself

Insert, query, and aggregate data in ClickHouse with real SQL patterns. Use when writing analytical queries, inserting data at scale, building dashboards, or implementing materialized views for pre-aggregation. Trigger with "clickhouse query", "clickhouse insert", "clickhouse aggregate", "clickhouse materialized view", "clickhouse SQL".

Use this Skill: https://skilld.dev/gh/jeremylongshore/tons-of-skills-marketplace/clickhouse-core-workflow-b

Nothing lands on disk. Nothing to clean up.

Fork this Skill

Edit a local copy. It keeps the author and licence.

referencesadvanced.md

≈761 tokens on demand. Your agent reads this file only when SKILL.md points to it.

Advanced ClickHouse: Materialized Views, Window Functions & Function Reference

Pre-aggregation, analytical windowing, and the day-to-day function toolbox. These are the deep patterns extracted from the core workflow — reach for them once basic inserts and queries are working.

Materialized Views (pre-aggregation)

A materialized view aggregates rows automatically on every INSERT into the source table, so dashboard reads hit small pre-rolled tables instead of scanning raw events. Pair the view with an AggregatingMergeTree target and merge the partial aggregation states at read time.

-- Source table receives raw events
-- Materialized view aggregates automatically on INSERT

CREATE MATERIALIZED VIEW analytics.hourly_stats_mv
TO analytics.hourly_stats  -- target table
AS
SELECT
    toStartOfHour(created_at) AS hour,
    tenant_id,
    event_type,
    count()                   AS event_count,
    uniqState(user_id)        AS unique_users_state
FROM analytics.events
GROUP BY hour, tenant_id, event_type;

-- Target table uses AggregatingMergeTree
CREATE TABLE analytics.hourly_stats (
    hour              DateTime,
    tenant_id         UInt32,
    event_type        LowCardinality(String),
    event_count       UInt64,
    unique_users_state AggregateFunction(uniq, UInt64)
)
ENGINE = AggregatingMergeTree()
ORDER BY (tenant_id, event_type, hour);

-- Query the materialized view (merge aggregation states)
SELECT
    hour,
    sum(event_count)           AS events,
    uniqMerge(unique_users_state) AS unique_users
FROM analytics.hourly_stats
WHERE tenant_id = 1
GROUP BY hour
ORDER BY hour;

Key rule: columns stored with a *State function (uniqState) must be read back with the matching *Merge function (uniqMerge) to produce a final value.

Window Functions

-- Running total and rank within each tenant
SELECT
    tenant_id,
    event_type,
    count()   AS cnt,
    sum(count()) OVER (PARTITION BY tenant_id ORDER BY count() DESC) AS running_total,
    row_number() OVER (PARTITION BY tenant_id ORDER BY count() DESC) AS rank
FROM analytics.events
WHERE created_at >= today() - 7
GROUP BY tenant_id, event_type
ORDER BY tenant_id, rank;

Common ClickHouse Functions

Function Description Example
count() Row count count()
uniq(col) Approximate distinct count (HyperLogLog) uniq(user_id)
uniqExact(col) Exact distinct count uniqExact(user_id)
quantile(0.95)(col) Percentile quantile(0.95)(latency_ms)
arrayJoin(arr) Unnest array to rows arrayJoin(tags)
JSONExtractString(col, key) Extract from JSON string JSONExtractString(properties, 'plan')
toStartOfHour(dt) Truncate to hour toStartOfHour(created_at)
formatReadableSize(n) Human-readable bytes formatReadableSize(bytes)
if(cond, then, else) Conditional if(cnt > 0, cnt, NULL)
multiIf(...) Multi-branch conditional multiIf(x>10, 'high', x>5, 'med', 'low')

Source: SKILL.md on GitHub

skilld matched fixed text patterns in SKILL.md and file names. Patterns miss obfuscated code.

skilld run checks every file with the same patterns. It asks for approval before it loads a Skill with a behavior marked Needs approval.

No alerts2mo3 checks · Risk SAFE
  • Gen Agent Trust Hub2mo

    The skill provides safe code templates and best practices for interacting with a ClickHouse database. It correctly demonstrates the use of parameterized queries to prevent SQL injection and uses environment variables for sensitive credential management.

  • Socket2mo

    No alerts

  • Snyk2mo

    Risk: LOW · No issues

Signed by skilld at 0d57af8. This ties the file your Agent reads to that commit on GitHub. It does not review the instructions.

Last checked against GitHub yesterday.

Activeupdated 4 months ago
What it can do
Reads files Edits files Runs commands
version
1.7.0
author
Jeremy Longshore <jeremy@intentsolutions.io>
tags
[
  "saas",
  "database",
  "analytics",
  "clickhouse",
  "olap"
]
All 4 allowed tools
ReadWriteEditBash(npm:*)
Other metadata
compatibility
Designed for Claude Code

README badge

README badge for jeremylongshore/tons-of-skills-marketplace/clickhouse-core-workflow-b