---
name: clickhouse-core-workflow-b
description: |-
  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".
allowed-tools: Read, Write, Edit, Bash(npm:*)
version: 1.7.0
license: MIT
author: Jeremy Longshore <jeremy@intentsolutions.io>
tags:
  - saas
  - database
  - analytics
  - clickhouse
  - olap
compatibility: Designed for Claude Code
title: clickhouse-core-workflow-b
canonical_url: https://skilld.dev/gh/jeremylongshore/tons-of-skills-marketplace/clickhouse-core-workflow-b
last_updated: 2026-10-07T06:06:28.000Z
---

> Index of every page on this site: [llms.txt](https://skilld.dev/llms.txt)

> **Skill from skilld.dev.** Follow the user's requested action.
> If the user asked to fork this Skill, follow the fork workflow below. Do not execute the Skill instructions while copying it.
> Otherwise, follow the instructions below for this session. You do not need to install anything.
>
> Supporting files, fetch one when the Skill refers to it: [references/advanced.md](https://skilld.dev/api/skills-raw/jeremylongshore/tons-of-skills-marketplace/clickhouse-core-workflow-b/references/advanced.md), [references/queries.md](https://skilld.dev/api/skills-raw/jeremylongshore/tons-of-skills-marketplace/clickhouse-core-workflow-b/references/queries.md).
>
> If the user asked to install this Skill, run `npx skilld install jeremylongshore/tons-of-skills-marketplace/clickhouse-core-workflow-b`. Install writes the Skill files into the project, so every session loads them.
>
> ## Fork workflow
>
> A fork creates an editable local Skill with its original author and licence. The request authorizes copying and local installation.
> 1. Check `./skills/clickhouse-core-workflow-b`, the project lockfile, and selected Agent targets together. If the local directory or installed Skill exists, stop. Never overwrite an existing directory or Agent target.
> 2. Read [source metadata](https://skilld.dev/api/v1/skills/jeremylongshore/tons-of-skills-marketplace/clickhouse-core-workflow-b) once. Use sourceUrl, sourceCommit, skillPath, sourceGone, and license. If the source is gone or its path is missing, stop. If license is null, read licence files at the source commit.
> 3. Fetch only the source commit into a temporary Git repository. Do not clone full history. Derive repository_url from sourceUrl, including repository renames. If sourceCommit is absent, resolve the sourceUrl ref once. Set source_commit to that actual commit. Run these commands in one shell call:
>
> ```sh
> git init --quiet "$temporary_dir"
> git -C "$temporary_dir" fetch --quiet --depth=1 "$repository_url" "$source_commit"
> git -C "$temporary_dir" checkout --quiet --detach FETCH_HEAD
> ```
>
> Read applicable licence declarations and notices at that commit. If copying is not permitted, report the restriction and stop.
> 4. Inspect source entries together, then copy the directory containing skillPath into `./skills/clickhouse-core-workflow-b`. Use the user's path if selected. Keep the original SKILL.md, relative links, scripts, binary assets, and executable modes. Exclude .git metadata. Reject symlinks and paths outside the Skill directory. After checking entries, use cp -a where available. A regular source directory needs no custom copy script. Do not save this page wrapper as SKILL.md.
> Preserve author credit, notices, and applicable licence files from repository or parent directories. Add PROVENANCE.md with the Skill page, source URL, actual commit, original path, and licence. Retain any existing PROVENANCE.md and record new provenance separately. Batch source inspection, copying, and provenance work where practical.
> 5. In the project root, run `skilld install ./skills/clickhouse-core-workflow-b --mode copy --plain`. If skilld is unavailable, use `npx skilld install ./skills/clickhouse-core-workflow-b --mode copy --plain`. This known command needs no help lookup. Install does not support --json. Use detected Agent targets, or add --agent for the targets the user selected. Install the local path, never the upstream selector. If installation fails, preserve the local copy and report the exact failure.
> 6. Confirm the local lockfile source and installed Agent copies once. Report the local path, actual commit, and Agent targets. After edits, reinstall the same local path. Upstream updates must not replace it. Do not publish or push unless the user asks.

# ClickHouse Insert & Query (Core Workflow B)

## Overview

Move data into ClickHouse efficiently, then answer analytical questions with
aggregations, funnels, retention, window functions, and materialized views.
This skill covers the read/write half of the core workflow: the fast-path insert
patterns that avoid "too many parts", the parameterized query API for Node.js,
and pre-aggregation via materialized views. The high-frequency patterns live
inline below; the deep query library and advanced engine patterns are broken out
into `references/` so you can drill in only when you need them.

## Prerequisites

- Tables already created — run `clickhouse-core-workflow-a` first if not.
- `@clickhouse/client` installed and connected (`CLICKHOUSE_HOST`, `CLICKHOUSE_USER`,
  `CLICKHOUSE_PASSWORD` in the environment).
- A target database/table (examples use `analytics.events`).

## Instructions

### Step 1: Bulk insert (the fast path)

Batch rows and let the client buffer. ClickHouse writes a new "part" per INSERT,
so many tiny inserts are the number-one performance mistake.

```typescript
import { createClient } from '@clickhouse/client';

const client = createClient({
  url: process.env.CLICKHOUSE_HOST!,
  username: process.env.CLICKHOUSE_USER ?? 'default',
  password: process.env.CLICKHOUSE_PASSWORD ?? '',
});

// Insert many rows efficiently — @clickhouse/client buffers internally
await client.insert({
  table: 'analytics.events',
  values: events,   // Array of objects matching table columns
  format: 'JSONEachRow',
});
```

Streaming a file (CSV, Parquet, etc.) uses the same call with a read stream and
the matching `format` (e.g. `CSVWithNames`).

**Insert best practices:**

- Batch rows: aim for 10K-100K rows per INSERT (not one at a time).
- ClickHouse creates a new "part" per INSERT — too many small inserts cause "too many parts".
- For real-time streams, buffer 1-5 seconds then flush.

### Step 2: Analytical queries

Aggregate with `count()`, `uniqExact()`, and time filters. The canonical
"top events by tenant" shape:

```sql
SELECT tenant_id, event_type, count() AS event_count, uniqExact(user_id) AS unique_users
FROM analytics.events
WHERE created_at >= now() - INTERVAL 7 DAY
GROUP BY tenant_id, event_type
ORDER BY event_count DESC
LIMIT 100;
```

Funnel, retention, and safe parameterized-query patterns are in
[references/queries.md](https://skilld.dev/api/skills-raw/jeremylongshore/tons-of-skills-marketplace/clickhouse-core-workflow-b/references/queries.md).

### Step 3: Pre-aggregation and windowing

For dashboards, pre-aggregate on INSERT with a materialized view backed by an
`AggregatingMergeTree` target, then merge states at read time. Window functions
(`row_number()`, running totals via `OVER (PARTITION BY ...)`) and the full
function reference table are in
[references/advanced.md](https://skilld.dev/api/skills-raw/jeremylongshore/tons-of-skills-marketplace/clickhouse-core-workflow-b/references/advanced.md).

## Output

Applying this skill produces:

- **Insert code** — a batched `client.insert(...)` call (or file stream) that
  loads rows without triggering "too many parts".
- **Query results** — aggregation rows returned as JSON via `rs.json()`, ready
  to feed a dashboard or API response.
- **Materialized view + target table** — DDL that keeps a small pre-rolled table
  updated automatically on every source INSERT.

## Error Handling

| Error | Cause | Solution |
|-------|-------|----------|
| `Too many parts (300)` | Frequent small inserts | Batch inserts, increase `parts_to_throw_insert` |
| `Memory limit exceeded` | Large GROUP BY / JOIN | Add WHERE filters, increase `max_memory_usage` |
| `UNKNOWN_FUNCTION` | Wrong ClickHouse version | Check `SELECT version()` |
| `Cannot parse datetime` | Wrong format | Use `YYYY-MM-DD HH:MM:SS` format |

## Examples

- **Insert a batch of events** — Step 1 above; adapt `values` to your row shape.
- **Top events / funnel / retention / parameterized queries** — full runnable
  SQL and Node.js in [references/queries.md](https://skilld.dev/api/skills-raw/jeremylongshore/tons-of-skills-marketplace/clickhouse-core-workflow-b/references/queries.md).
- **Materialized view, window functions, function reference** — the pre-aggregation
  and windowing patterns plus the common-function cheat sheet in
  [references/advanced.md](https://skilld.dev/api/skills-raw/jeremylongshore/tons-of-skills-marketplace/clickhouse-core-workflow-b/references/advanced.md).

## Resources

- [SQL Reference](https://clickhouse.com/docs/sql-reference)
- [Aggregate Functions](https://clickhouse.com/docs/sql-reference/aggregate-functions)
- [Materialized Views Guide](https://clickhouse.com/blog/using-materialized-views-in-clickhouse)

## Next Steps

For error troubleshooting once queries are running, see `clickhouse-common-errors`.
For table and schema design, revisit `clickhouse-core-workflow-a`.
