clickhouse-best-practices: Constraining ClickHouse Reviews and Recommendations with 31 Rules

Introduction

When asking an Agent about ClickHouse table creation, query modifications, or configuration adjustments, the model often defaults to general OLTP intuition: freely using Nullable, setting large partitions, or filtering after JOINs. However, the mechanics of columnar storage, sparse indexing, and MergeTree are different; such advice can waste storage or severely degrade query performance. This article introduces the ClickHouse officially maintained Agent Skill, clickhouse-best-practices: before providing suggestions regarding schemas, queries, or configurations, the Agent must verify against built-in rules and cite the specific rule names in its response.

What is this?

clickhouse-best-practices is a Skill within the ClickHouse Agent Skills repository, maintained by ClickHouse Inc. It is licensed under Apache-2.0, with the current metadata version at 0.4.0. The Skill description mandates its use when reviewing ClickHouse schemas, queries, or configurations. It contains 31 rules that must be checked before offering advice, covering four categories: schema design, query optimization, data insertion, and Agent connectivity. These rules are sorted by impact priority. Aligned with the official documentation ClickHouse Best Practices, it serves as an “executable checklist + citation format,” rather than a replacement for the ClickHouse documentation itself.

The repository also offers other Skills for architecture consulting, Node client troubleshooting, chdb, and infrastructure; this article focuses solely on the best-practices Skill.

Core Features

Rule-Prioritized Response Flow

Before answering any ClickHouse-related questions, the Skill requires the following processing order:

  1. Search for applicable rules in the rules/ directory;
  2. If applicable rules exist: Follow the rules and cite them in the response using the format Per \rule-name`…`;
  3. If no corresponding rules exist: Use the model’s built-in ClickHouse knowledge or consult documentation;
  4. If still uncertain: Use Web search to find current best practices;
  5. Always cite sources: Rule names, “general ClickHouse guidance,” or URLs.

The reason for this approach is that behaviors like columnar storage, sparse indexing, and MergeTree merges are inconsistent with common database intuition. The rules encode verified, ClickHouse-specific guidance.

Four Review Categories & Agent Workflow

Schema Review (CREATE TABLE / ALTER TABLE): Read rule files in a fixed order covering Primary Key planning, Cardinality sorting, Filtering columns, Types, LowCardinality, Nullable, and Partitioning. Check items include verifying ORDER BY immutability, partition cardinality (approx. 100–1,000), and whether ReplacingMergeTree includes a version column.

Query Review (SELECT / JOIN / Aggregation): Covers JOIN algorithm selection, pre-JOIN filtering, ANY JOIN, skipping indices, and alignment between WHERE and ORDER BY.

Insert Strategy Review: Batch size (recommend approx. 10K–100K rows per batch), avoiding frequent ALTER TABLE UPDATE/DELETE, async small-batch inserts, and caution regarding OPTIMIZE TABLE.

Agent Connectivity & Querying: Connection (MCP + CLI), 7-step schema discovery, and safe querying with LIMIT and timeouts. The recommended session sequence is Connect → Discover → Plan → Execute → Recover.

Rule Classification & Priority (Excerpt)

Priority Category Impact Rule Prefix Count
1 Primary Key Selection CRITICAL schema-pk- 4
2 Data Type Selection CRITICAL schema-types- 5
3 JOIN Optimization CRITICAL query-join- 5
4 Insert Batching CRITICAL insert-batch- 1
5 Mutation Avoidance CRITICAL insert-mutation- 2
6 Partitioning Strategy HIGH schema-partition- 4
7 Skipping Indices HIGH query-index- 1
8 Materialized Views HIGH query-mv- 2
9 Async Inserts HIGH insert-async- 2
10 OPTIMIZE Avoidance HIGH insert-optimize- 1
11 JSON Usage MEDIUM schema-json- 1
12 Agent Schema Discovery CRITICAL agent-discovery- 1
13 Agent Query Safety CRITICAL agent-query- 1
14 Agent Connectivity + Formats HIGH agent-connect- 1

Rule file naming examples: schema-pk-plan-before-creation (plan ORDER BY before creation, as it is immutable), query-join-filter-before (filter before JOIN), insert-batch-size (batch insert size), etc. For the complete list, see SKILL.md and rules/ in the repository.

Mandatory Output Structure

For review-type responses, the Skill requires the Agent to organize results according to a template to facilitate compliance and violation checks:

## Rules Checked
- `rule-name-1` - Compliant / Violation found
...

## Findings
### Violations
- **`rule-name`**: ...
### Compliant
- `rule-name`: ...

## Recommendations
...

Installation & Activation

The Skill is distributed as part of the clickhouse/agent-skills package. It can be installed into supported Agent environments using the two methods described in the official documentation:

npx skills add clickhouse/agent-skills

npx skills will detect locally installed Agents and prompt for installation targets. If clickhousectl is already installed, you can also run:

clickhousectl skills

The Skill follows the open standard from agentskills.io. Once loaded, the Agent should automatically activate relevant guidance when handling ClickHouse table creation, query writing, or data pipeline design. A human-readable overview is available at skills/clickhouse-best-practices/SKILL.md in the repository, while a fully compiled version is in AGENTS.md in the same directory.

For mounting paths and trigger methods of Skills on platforms like Cursor, Claude Code, and Copilot, please refer to the respective product documentations; for installation commands, refer to the README of the ClickHouse repository.

Typical Usage

Scenario 1: Reviewing a New Table’s DDL

Provide a CREATE TABLE statement to an Agent loaded with this Skill, requesting it to follow the Schema Review process by reading files such as rules/schema-pk-*, rules/schema-types-*, and rules/schema-partition-* in order. Output should follow the “Rules Checked / Findings / Recommendations” format. You will see specific rule citations indicating whether “ORDER BY columns are sorted from low to high cardinality,” “if filter columns are in the sorting key prefix,” or “if partition count is out of control” are violated.

Scenario 2: Optimizing Slow Queries

Provide the SELECT statement and table structure context. The Agent should prioritize consulting query-join-*, query-index-skipping-indices, and schema-pk-filter-on-orderby to check for pre-JOIN filtering, whether the algorithm matches table scale, and if non-sorting key filters consider skipping indices.

Scenario 3: Agent Directly Connecting to ClickHouse for Data Exploration

Per Skill requirements, first follow rules/agent-connect-mcp.md to establish a connection, then execute the seven-step discovery in rules/agent-discovery-schema.md (Database → Table → Columns/Comments → Sorting Key → Skipping Indices → Sampling → EXPLAIN). Finally, execute queries with LIMIT and timeouts under the constraints of rules/agent-query-safety.md; if timeouts or memory errors occur, narrow the filter scope and retry.

Applicable Scenarios & Caveats

Who is it for?

  • Teams using Agents like Cursor, Claude Code, or Copilot for ClickHouse schema design, SQL review, or insertion strategy discussions;
  • Readers who want AI advice to align with clickhouse.com/docs/best-practices and be traceable to “which specific rule”;
  • Users expanding ClickHouse usage in analytics or AI data pipelines who want to prevent Agents from modifying keys or partitions based on generalized database experience.

Limitations & Notes

  • The Skill provides review and process instructions; it does not replace empirical verification like connecting to a cluster for EXPLAIN or checking system.parts. The Agent rules also state to check documentation or search only if no applicable rules exist.
  • There is a slight hierarchical difference between the repository README’s description of “business rule count” and the Skill metadata (31 rules, including Agent categories); defer to the actual files in the SKILL.md and rules/ within the clickhouse-best-practices directory.
  • For full-scale schema design requiring “when to partition, how to select architecture,” combine with the clickhouse-architecture-advisor from the same repository. best-practices focuses on the checkable “what,” while the architecture Skill focuses on “when/why/how.”

Conclusion

clickhouse-best-practices breaks down official ClickHouse best practices into 31 mandatory rules and a fixed review process. This ensures the Agent aligns with the real constraints of columnar storage and MergeTree before modifying tables, SQL, or discussing insertion strategies, and explicitly names the rules in its output. After installing the full package, simply load this Skill when handling ClickHouse-related tasks.

Official Address: https://github.com/clickhouse/agent-skills/tree/main/skills/clickhouse-best-practices

羽毛球分组比赛记分
小程序二维码

欢迎使用《羽毛球分组比赛记分》微信小程序

Xiaoye