How to Schedule Queries in ClickHouse

Galaxy Glossary

How can I schedule recurring SQL queries directly inside ClickHouse?

CREATE TASK lets you run ClickHouse queries automatically on a cron-like schedule without external tools.

Sign up for the latest in SQL knowledge from the Galaxy Team!
Welcome to the Galaxy, Guardian!
You'll be receiving a confirmation email

Follow us on twitter :)
Oops! Something went wrong while submitting the form.

Description

Table of Contents

Why schedule queries in ClickHouse?

Automated jobs refresh roll-ups, clean logs, and feed dashboards without manual effort. Native scheduling avoids external cron servers and keeps logic close to data.

What is the CREATE TASK command?

CREATE TASK stores a query in system.tasks and executes it per a cron expression.Tasks survive restarts and can be ENABLED or DISABLED.

How do I create a daily sales summary task?

Use CREATE TASK with ON SCHEDULE to insert aggregated data into a reporting table every night.

CREATE TASK daily_sales_rollup
ON SCHEDULE '0 2 * * *'
AS INSERT INTO sales_daily (order_date, total_amount)
SELECT toDate(order_date), sum(total_amount)
FROM Orders
GROUP BY toDate(order_date);

How can I pause or resume a task?

ALTER TASK … DISABLE stops execution while keeping the definition.ALTER TASK … ENABLE resumes the schedule.

How do I change the schedule?

ALTER TASK with a new CRON string updates when the query runs. The task ID remains the same, so monitoring scripts stay intact.

How do I monitor task runs?

system.task_log records start time, duration, state, and errors. Query it to build alerts or dashboards.

Best practices for scheduling

1. Aggregate into new tables, not source tables. 2. Guard against duplicates with toStartOfInterval filters. 3.Log errors to Slack via Webhooks.

Common mistakes

Missing timezone: Cron strings run in server TZ. Set timezone parameter when servers span regions.

Heavy joins at peak: Schedule ETL during off-hours to prevent resource contention.

Need more complex workflows?

Chain tasks by appending INSERT … SELECT from yesterday’s roll-up, or orchestrate with Airflow when dependencies grow.

.

Why How to Schedule Queries in ClickHouse is important

How to Schedule Queries in ClickHouse Example Usage


-- Nightly inventory snapshot for ‘Products’ table
CREATE TASK nightly_inventory_snapshot
ON SCHEDULE '30 1 * * *'
AS INSERT INTO inventory_history
SELECT id, name, stock, now() AS snapshot_time
FROM Products;

How to Schedule Queries in ClickHouse Syntax


CREATE [OR REPLACE] TASK [IF NOT EXISTS] task_name
ON SCHEDULE '<cron_expression>' [SETTING schedule_timezone = 'UTC']
[ENABLE | DISABLE]
AS <query>;

ALTER TASK task_name
    ENABLE | DISABLE |
    ON SCHEDULE '<new_cron_expression>' |
    AS <new_query>;

DROP TASK [IF EXISTS] task_name;

Common Mistakes

Frequently Asked Questions (FAQs)

Does CREATE TASK support parameters?

No, the query body must be a fully formed statement. Use macros like today() for dynamic dates.

Can I backfill missed runs?

Tasks don’t rerun automatically for downtime gaps. Manually execute the query or adjust the WHERE clause to cover missed periods.

Is CREATE TASK production-ready?

Yes, since v21.12 it’s marked stable, but always monitor system.task_log and set memory limits in the query.

Want to learn about other SQL terms?

Trusted by top engineers on high-velocity teams
Aryeo Logo
Assort Health
Curri
Rubie Logo
Bauhealth Logo
Truvideo Logo
Welcome to the Galaxy, Guardian!
You'll be receiving a confirmation email

Follow us on twitter :)
Oops! Something went wrong while submitting the form.