Skip to content

In development. Expect rough edges and changes. The source code is not public yet.

ClickHouse 29 min read How-to mediumAdept

Accelerating ClickHouse queries on JSON data for faster Bluesky insights

Incremental materialized views feeding AggregatingMergeTree tables cut ClickHouse dashboard queries over 4+ billion Bluesky JSON documents (1.6 TiB) from 44–56 seconds to 3–7 ms. Partial aggregation states are computed only for newly inserted rows and merged in the background, so the pre-aggregated tables stay tiny (e.g. 892 rows, 11 KiB) and constant-size regardless of source growth. Query latency therefore stays flat as the source table grows, and memory drops from ~776 MiB to ~186 KiB per query. Three dashboard scenarios cover count, uniq-based unique-user counts and top-reposted posts, with DDL, materialized view definitions and execution statistics; the reposts dashboard runs with a 5-second delay.

Related posts

  • Harnessing the Power of Materialized Views and ClickHouse for High-Performance Analytics at Inigo

    ClickHouse mediumAdept

    Inigo stores analytics on billions of GraphQL API requests in ClickHouse after rejecting Snowflake as too slow and costly for customer-facing data and lacking a local emulator, and PostgreSQL because its indexes and costs blew up past roughly 100K–1M rows. Raw tables keyed on observed_at feed AggregatingMergeTree materialized views bucketed at 1-minute, 6-minute, 40-minute and 3-hour intervals, so most queries scan thousands of aggregated rows instead of millions of raw ones. Counts are stored as a calls column and read back with sum(calls), percentiles use quantilesState in the view and quantileMerge at query time, and high-cardinality fields like trace_id stay in raw tables only. The result is month-long retention in views versus a week of raw data, lower database load, and fast local integration tests, at the cost of setup complexity and no transactions.

  • Building a product analytics solution with ClickHouse

    ClickHouse mediumAdept

    ClickHouse's in-house product analytics platform, Galaxy, stores over 20 billion events (14 TB, roughly 14x compression) in a single denormalized MergeTree table keyed on created_at. A single sparse table avoids join overhead, since ClickHouse's sparse serialization makes empty columns cheap. Columns used for cohorts and filters should be top-level rather than buried in a JSON payload. Incremental materialized views speed up the few dozen recurring queries, and a custom SDK handles event capture.

  • Better Analytics at Scale with Trackingplan and ClickHouse

    ClickHouse mediumAdept

    Trackingplan, which tests web, mobile and backend analytics by sampling events sent to tools like Google Analytics and Segment, moved from DynamoDB with PySpark-precomputed statistics to ClickHouse Cloud so it could answer unforeseen ad hoc queries on raw data. After evaluating Snowflake, BigQuery, Redshift and Athena, the team found ClickHouse 20x faster than Athena and cheaper than the pay-per-scan options for a continuous query workload. Materialized views, backfilled via INSERT INTO SELECT, precompute the common dashboard statistics. Customer-ID ordering keys handle multi-tenancy and ad hoc queries across about 30 billion rows growing 400% per year, and Map/Array types hold dynamic properties.

  • ClickHouse Newsletter April 2022: JSON, JSON, JSON

    ClickHouse mediumAdept

    ClickHouse 22.3 LTS adds an experimental JSON data type that stores semi-structured documents in a single column, with dynamic subcolumns underneath, so you no longer need hand-built schemas of arrays and materialized columns. The worked examples cover querying nested fields, loading files with the JSONAsString format, and pulling a timestamp into its own column so MergeTree has a usable sorting key. They also show how to display field names with output_format_json_named_tuples_as_objects. The release also ships ARM Docker images, fully tested S3 disk storage (with known performance issues), and a Hive table function, and ClickHouse Cloud's serverless waitlist is now open.

  • ClickHouse Release 23.12

    ClickHouse mediumAdept

    ClickHouse 23.12 adds experimental Refreshable Materialized Views, which periodically re-run an arbitrary query and atomically replace the results in a target table. That makes complex multi-join aggregations practical where incremental insert-trigger views fall short, and they can also serve as cron-style import/export jobs chained with DEPENDS. In the IMDB example, a refreshable view cuts an actor-summary query from 1.2 s to 3 ms. The release also speeds up SELECT ... FINAL by splitting data into non-intersecting sorting-key ranges, which are processed in parallel without merge logic, so only intersecting ranges pay the merge cost and merges are skipped across partitions when the partition key is a prefix of the sorting key.

The quest for software engineering knowledge.

Look:
Layout: