{"id":119550,"date":"2026-07-06T15:16:15","date_gmt":"2026-07-06T09:46:15","guid":{"rendered":"https:\/\/www.guvi.in\/blog\/?p=119550"},"modified":"2026-07-06T15:16:16","modified_gmt":"2026-07-06T09:46:16","slug":"clickhouse-tutorial","status":"publish","type":"post","link":"https:\/\/www.guvi.in\/blog\/clickhouse-tutorial\/","title":{"rendered":"ClickHouse Tutorial: Real-Time Analytics at Scale\u00a0"},"content":{"rendered":"\n<p>Traditional databases slow to a crawl when you ask them to aggregate billions of rows. ClickHouse was built specifically so that question takes milliseconds instead of minutes. This ClickHouse tutorial shows you how to get it running with Docker, create your first table with the MergeTree engine, insert data, run analytical queries, and connect from Python. No prior experience with columnar databases required.&nbsp;<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>TL;DR Summary<\/strong><\/h2>\n\n\n\n<ul>\n<li>ClickHouse is a free, open-source columnar database built for analytical queries on billions of rows in milliseconds.<\/li>\n\n\n\n<li>Setup takes under 5 minutes with a single Docker command.<\/li>\n\n\n\n<li>ClickHouse uses SQL you already know, with the MergeTree engine as the core table type for analytics.<\/li>\n\n\n\n<li>It is 100 to 1000x faster than traditional row-based databases for aggregation queries.<\/li>\n\n\n\n<li>Used in production at Cloudflare, Spotify, Uber, and eBay for real-time analytics at petabyte scale.<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>What is ClickHouse?<\/strong><\/h2>\n\n\n\n<p>ClickHouse is an open-source columnar database management system designed for Online Analytical Processing (OLAP). Instead of storing data row by row like PostgreSQL or MySQL, ClickHouse stores data column by column. When you run a query that only touches three columns out of thirty, ClickHouse reads only those three columns from disk. This is why it is so fast for analytical workloads.<\/p>\n\n\n\n<p>ClickHouse was originally developed at Yandex to power web analytics handling over 600 petabytes of data. It has since become one of the most popular choices for real-time dashboards, log analytics, time-series data, and event pipelines at companies like Cloudflare, Uber, and eBay.<\/p>\n\n\n\n<p>ClickHouse is one of the core components of modern data engineering stacks alongside stream processing tools and orchestration frameworks. HCL GUVI&#8217;s<a href=\"https:\/\/www.guvi.in\/zen-class\/ai-software-development-course\/?utm_source=blog&amp;utm_medium=hyperlink&amp;utm_campaign=clickhouse-tutorial\" target=\"_blank\" rel=\"noreferrer noopener\"> AI Software Development Course<\/a> is IITM Pravartak certified and builds the backend engineering depth that makes working with high-performance databases like ClickHouse practical and production-ready.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>ClickHouse Tutorial: Installation with Docker<\/strong><\/h2>\n\n\n\n<p>Run ClickHouse locally with one command:<\/p>\n\n\n\n<p><strong>docker run -d &#8211;name clickhouse-server &#8211;ulimit nofile=262144:262144 -p 8123:8123 -p 9000:9000 clickhouse\/clickhouse-server<\/strong><\/p>\n\n\n\n<p>ClickHouse listens on port 9000 for the native protocol and port 8123 for HTTP queries. The <strong>&#8211;ulimit<\/strong> flag sets the maximum number of open files, which ClickHouse needs to perform well under load.<\/p>\n\n\n\n<p>Verify it is running:<\/p>\n\n\n\n<p><strong>docker ps<\/strong><\/p>\n\n\n\n<p>You should see the clickhouse-server container listed with status Up.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Connecting with clickhouse-client<\/strong><\/h2>\n\n\n\n<p>The interactive SQL shell for ClickHouse is clickhouse-client. Open it directly inside the running container:<\/p>\n\n\n\n<p><strong>docker exec -it clickhouse-server clickhouse-client<\/strong><\/p>\n\n\n\n<p>You will see a <strong>clickhouse-server \ud83d\ude42<\/strong> prompt. You are now connected and ready to run SQL queries directly against ClickHouse.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Creating a Database and Table<\/strong><\/h2>\n\n\n\n<p>Create a database first:<\/p>\n\n\n\n<p><strong>Sql<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>CREATE DATABASE IF NOT EXISTS demo;\nUSE demo;\n<\/code><\/pre>\n\n\n\n<p>Now create a table using the MergeTree engine, the workhorse of ClickHouse:<\/p>\n\n\n\n<p><strong>sql<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>CREATE TABLE IF NOT EXISTS events\n(\n    event_date Date,\n    event_time DateTime,\n    user_id UInt32,\n    event_type LowCardinality(String),\n    page_url String,\n    duration_ms UInt32\n)\nENGINE = MergeTree()\nPARTITION BY toYYYYMM(event_date)\nORDER BY (event_type, event_time)\nTTL event_date + INTERVAL 90 DAY;\n<\/code><\/pre>\n\n\n\n<p>A few things worth understanding here. <strong>PARTITION BY toYYYYMM<\/strong> splits data by month so you can drop old months instantly without scanning the whole table. <strong>ORDER BY<\/strong> defines the sparse index and is the most important clause for query performance. <strong>LowCardinality(String)<\/strong> is a special type for columns with few unique values like event types or country codes.&nbsp;<\/p>\n\n\n\n<p>It dramatically reduces storage and speeds up filtering. <strong>TTL<\/strong> automatically deletes rows older than 90 days, useful for log data where old events are irrelevant.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Inserting and Querying Data<\/strong><\/h2>\n\n\n\n<p>Insert some sample rows:<\/p>\n\n\n\n<p><strong>Sql<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>INSERT INTO demo.events VALUES\n    (today(), now(), 1001, 'page_view', '\/home', 320),\n    (today(), now(), 1002, 'click', '\/pricing', 150),\n    (today(), now(), 1001, 'page_view', '\/docs', 540);\n<\/code><\/pre>\n\n\n\n<p>Run an analytical query to see ClickHouse in action:<\/p>\n\n\n\n<p><strong>sql<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>SELECT\n    event_type,\n    count() AS total_events,\n    avg(duration_ms) AS avg_duration\nFROM demo.events\nWHERE event_date &gt;= today() - 7\nGROUP BY event_type\nORDER BY total_events DESC;\n<\/code><\/pre>\n\n\n\n<p>ClickHouse returns this aggregation in milliseconds even when the table contains billions of rows, because it only reads the <strong>event_type<\/strong>, <strong>event_date<\/strong>, and <strong>duration_ms<\/strong> columns from disk.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Connecting with Python<\/strong><\/h2>\n\n\n\n<p>Install the official ClickHouse Python client:<\/p>\n\n\n\n<p><strong>pip install clickhouse-connect<\/strong><\/p>\n\n\n\n<p>Connect and query:<\/p>\n\n\n\n<p><strong>Python<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>import clickhouse_connect\n\nclient = clickhouse_connect.get_client(\n    host='localhost',\n    port=8123,\n    username='default',\n    password=''\n)\n\nresult = client.query(\"\"\"\n    SELECT event_type, count() as cnt, avg(duration_ms) as avg_ms\n    FROM demo.events\n    GROUP BY event_type\n    ORDER BY cnt DESC\n\"\"\")\n\nfor row in result.result_rows:\n    print(row)\n\nclient.close()\n<\/code><\/pre>\n\n\n\n<p>The <strong>clickhouse-connect<\/strong> package is the officially supported Python driver for ClickHouse in 2026, replacing older alternatives.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>When to Use ClickHouse<\/strong><\/h2>\n\n\n\n<figure class=\"wp-block-table\"><table><tbody><tr><td><strong>Use Case<\/strong><\/td><td><strong>Why ClickHouse<\/strong><\/td><\/tr><tr><td>Real-time dashboards<\/td><td>Sub-second aggregations on billions of events<\/td><\/tr><tr><td>Log and event analytics<\/td><td>Handles high ingestion rates with columnar compression<\/td><\/tr><tr><td>Time-series workloads<\/td><td>Partitioning by time makes range queries extremely fast<\/td><\/tr><tr><td>User behaviour funnels<\/td><td>GROUP BY and window functions perform at scale<\/td><\/tr><tr><td>Application performance monitoring<\/td><td>Replaces Elasticsearch for metrics at a fraction of the cost<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<div style=\"background-color: #099f4e; border: 3px solid #110053; border-radius: 12px; padding: 18px 22px; color: #FFFFFF; font-size: 18px; font-family: Montserrat, Helvetica, sans-serif; line-height: 1.6; box-shadow: 0 4px 12px rgba(0, 0, 0, 0.15); max-width: 750px; margin: 22px auto;\">\n  <h3 style=\"margin-top: 0; font-size: 22px; font-weight: 700; color: #ffffff;\">\ud83d\udca1 Did You Know?<\/h3>\n  <ul style=\"padding-left: 20px; margin: 10px 0;\">\n    <li>ClickHouse processes hundreds of millions to over a billion rows and tens of gigabytes of data per server per second. At Cloudflare, ClickHouse handles over 6 million queries per second across its global network monitoring infrastructure. The same query that takes minutes on PostgreSQL returns in milliseconds on ClickHouse at that scale.<\/li>\n  <\/ul>\n<\/div>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Common Mistakes to Avoid<\/strong><\/h2>\n\n\n\n<ul>\n<li><strong>Using ClickHouse as a transactional database.<\/strong> ClickHouse lacks full ACID compliance and updates are expensive. It is purpose-built for fast reads on large datasets, not frequent single-row writes and updates. Use PostgreSQL for transactional data and ClickHouse for analytics on top of it.<\/li>\n\n\n\n<li><strong>Choosing a poor ORDER BY clause.<\/strong> The ORDER BY columns in MergeTree are your sparse index. If you filter most queries by event_type and event_date, those should be first in the ORDER BY. A mismatched ORDER BY means full table scans on every query and eliminates ClickHouse&#8217;s primary performance advantage.<\/li>\n\n\n\n<li><strong>Not using LowCardinality for string columns with few unique values.<\/strong> Columns like country, event_type, status, or device_type have hundreds of distinct values at most. Wrapping them in LowCardinality cuts storage by up to 10x and significantly speeds up GROUP BY and WHERE filters on those columns.<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Conclusion<\/strong><\/h2>\n\n\n\n<p>This ClickHouse tutorial covered the full beginner workflow: running ClickHouse with Docker, connecting with clickhouse-client, creating a database and MergeTree table with partitioning and TTL, inserting and querying data, and connecting from Python. ClickHouse&#8217;s columnar architecture, SQL compatibility, and free open-source license make it one of the most powerful choices for real-time analytics in 2026.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>FAQs<\/strong><\/h2>\n\n\n<div id=\"rank-math-faq\" class=\"rank-math-block\">\n<div class=\"rank-math-list \">\n<div id=\"faq-question-1782740251482\" class=\"rank-math-list-item\">\n<h3 class=\"rank-math-question \"><strong>1. What is ClickHouse?<\/strong>\u00a0<\/h3>\n<div class=\"rank-math-answer \">\n\n<p>ClickHouse is a free, open-source columnar database designed for real-time OLAP queries. It stores data by column rather than by row, enabling aggregation queries on billions of rows to return in milliseconds.<\/p>\n\n<\/div>\n<\/div>\n<div id=\"faq-question-1782740268927\" class=\"rank-math-list-item\">\n<h3 class=\"rank-math-question \"><strong>2. Is ClickHouse free?<\/strong>\u00a0<\/h3>\n<div class=\"rank-math-answer \">\n\n<p>Yes. ClickHouse is open-source under the Apache 2.0 license. ClickHouse Cloud offers a managed service with a free trial for teams that prefer not to self-host.<\/p>\n\n<\/div>\n<\/div>\n<div id=\"faq-question-1782740288392\" class=\"rank-math-list-item\">\n<h3 class=\"rank-math-question \"><strong>3. What is the MergeTree engine in ClickHouse?<\/strong><\/h3>\n<div class=\"rank-math-answer \">\n\n<p>MergeTree is the primary table engine in ClickHouse for analytical workloads. It stores data sorted by the ORDER BY key, creates a sparse index for fast lookups, and supports partitioning, TTL, and data replication.<\/p>\n\n<\/div>\n<\/div>\n<div id=\"faq-question-1782740306864\" class=\"rank-math-list-item\">\n<h3 class=\"rank-math-question \"><strong>4. Can I use SQL with ClickHouse?<\/strong><\/h3>\n<div class=\"rank-math-answer \">\n\n<p>Yes. ClickHouse supports standard SQL for SELECT, INSERT, CREATE, and most common operations. Some advanced SQL features differ slightly from PostgreSQL, but most analytical queries work without modification.<\/p>\n\n<\/div>\n<\/div>\n<div id=\"faq-question-1782740326094\" class=\"rank-math-list-item\">\n<h3 class=\"rank-math-question \"><strong>5. Is ClickHouse suitable for transactional workloads?<\/strong><\/h3>\n<div class=\"rank-math-answer \">\n\n<p>No. ClickHouse is designed for analytical reads, not frequent single-row updates and deletes. Use a row-based database like PostgreSQL for transactional data and feed ClickHouse for analytics.<\/p>\n\n<\/div>\n<\/div>\n<\/div>\n<\/div>","protected":false},"excerpt":{"rendered":"<p>Traditional databases slow to a crawl when you ask them to aggregate billions of rows. ClickHouse was built specifically so that question takes milliseconds instead of minutes. This ClickHouse tutorial shows you how to get it running with Docker, create your first table with the MergeTree engine, insert data, run analytical queries, and connect from [&hellip;]<\/p>\n","protected":false},"author":65,"featured_media":121193,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[325,959],"tags":[],"views":"37","authorinfo":{"name":"Jebasta","url":"https:\/\/www.guvi.in\/blog\/author\/jebasta\/"},"thumbnailURL":"https:\/\/www.guvi.in\/blog\/wp-content\/uploads\/2026\/07\/ClickHouse-300x116.webp","_links":{"self":[{"href":"https:\/\/www.guvi.in\/blog\/wp-json\/wp\/v2\/posts\/119550"}],"collection":[{"href":"https:\/\/www.guvi.in\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.guvi.in\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.guvi.in\/blog\/wp-json\/wp\/v2\/users\/65"}],"replies":[{"embeddable":true,"href":"https:\/\/www.guvi.in\/blog\/wp-json\/wp\/v2\/comments?post=119550"}],"version-history":[{"count":3,"href":"https:\/\/www.guvi.in\/blog\/wp-json\/wp\/v2\/posts\/119550\/revisions"}],"predecessor-version":[{"id":121196,"href":"https:\/\/www.guvi.in\/blog\/wp-json\/wp\/v2\/posts\/119550\/revisions\/121196"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.guvi.in\/blog\/wp-json\/wp\/v2\/media\/121193"}],"wp:attachment":[{"href":"https:\/\/www.guvi.in\/blog\/wp-json\/wp\/v2\/media?parent=119550"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.guvi.in\/blog\/wp-json\/wp\/v2\/categories?post=119550"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.guvi.in\/blog\/wp-json\/wp\/v2\/tags?post=119550"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}