<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Joe Martin</title>
    <description>The latest articles on DEV Community by Joe Martin (@joethreepwood).</description>
    <link>https://dev.to/joethreepwood</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F750591%2Fdb095177-2c0a-4117-aba9-a2507f0e793d.png</url>
      <title>DEV Community: Joe Martin</title>
      <link>https://dev.to/joethreepwood</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/joethreepwood"/>
    <language>en</language>
    <item>
      <title>How to speed up ClickHouse queries using materialized columns</title>
      <dc:creator>Joe Martin</dc:creator>
      <pubDate>Thu, 11 Nov 2021 14:48:26 +0000</pubDate>
      <link>https://dev.to/joethreepwood/how-to-speed-up-clickhouse-queries-using-materialized-columns-2bo8</link>
      <guid>https://dev.to/joethreepwood/how-to-speed-up-clickhouse-queries-using-materialized-columns-2bo8</guid>
      <description>&lt;p&gt;ClickHouse supports speeding up queries using materialized columns to create new columns on the fly from existing data. In this post by Karl-Aksel Puulmann - a version of which originally appeared on &lt;a href="https://posthog.com/blog/clickhouse-materialized-columns"&gt;the PostHog blog&lt;/a&gt; - I’ll walk through a query optimization example that's well-suited to this rarely-used feature. &lt;/p&gt;

&lt;p&gt;Consider the following schema:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight sql"&gt;&lt;code&gt;&lt;span class="k"&gt;CREATE&lt;/span&gt; &lt;span class="k"&gt;TABLE&lt;/span&gt; &lt;span class="n"&gt;events&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;
    &lt;span class="n"&gt;uuid&lt;/span&gt; &lt;span class="n"&gt;UUID&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="n"&gt;event&lt;/span&gt; &lt;span class="nb"&gt;VARCHAR&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="nb"&gt;timestamp&lt;/span&gt; &lt;span class="n"&gt;DateTime64&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;6&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s1"&gt;'UTC'&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt;
    &lt;span class="n"&gt;properties_json&lt;/span&gt; &lt;span class="nb"&gt;VARCHAR&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="n"&gt;ENGINE&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;MergeTree&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
&lt;span class="k"&gt;ORDER&lt;/span&gt; &lt;span class="k"&gt;BY&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;toDate&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nb"&gt;timestamp&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt; &lt;span class="n"&gt;event&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;uuid&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="k"&gt;PARTITION&lt;/span&gt; &lt;span class="k"&gt;BY&lt;/span&gt; &lt;span class="n"&gt;toYYYYMM&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nb"&gt;timestamp&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Each event has an ID, event type, timestamp, and a JSON representation of event properties. The properties can include the current URL and any other user-defined properties that describe the event (e.g. NPS survey results, person properties, timing data, etc.).&lt;/p&gt;

&lt;p&gt;This table can be used to store a lot of analytics data and is similar to what we use at PostHog.&lt;/p&gt;

&lt;p&gt;If we wanted to query login page pageviews in August, the query would look like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight sql"&gt;&lt;code&gt;&lt;span class="k"&gt;SELECT&lt;/span&gt; &lt;span class="k"&gt;count&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="k"&gt;FROM&lt;/span&gt; &lt;span class="n"&gt;events&lt;/span&gt;
&lt;span class="k"&gt;WHERE&lt;/span&gt; &lt;span class="n"&gt;event&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s1"&gt;'$pageview'&lt;/span&gt;
  &lt;span class="k"&gt;AND&lt;/span&gt; &lt;span class="n"&gt;JSONExtractString&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;properties_json&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s1"&gt;'$current_url'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s1"&gt;'https://app.posthog.com/login'&lt;/span&gt;
  &lt;span class="k"&gt;AND&lt;/span&gt; &lt;span class="nb"&gt;timestamp&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;=&lt;/span&gt; &lt;span class="s1"&gt;'2021-08-01'&lt;/span&gt;
  &lt;span class="k"&gt;AND&lt;/span&gt; &lt;span class="nb"&gt;timestamp&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="s1"&gt;'2021-09-01'&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This query takes a while complete on a large test dataset, but without the URL filter the query is almost instant. Adding even more filters just slows down the query. Let's dig in to understand why.&lt;/p&gt;

&lt;h2&gt;
  
  
  Looking at flamegraphs
&lt;/h2&gt;

&lt;blockquote&gt;
&lt;p&gt;ClickHouse has great tools for introspecting queries, which we discuss in &lt;a href="https://posthog.com/blog/clickhouse-materialized-columns"&gt;a version of this article on the PostHog blog&lt;/a&gt;. For now, let's focus on the soluion...&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h2&gt;
  
  
  Enter materialized columns
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight sql"&gt;&lt;code&gt;&lt;span class="k"&gt;ALTER&lt;/span&gt; &lt;span class="k"&gt;TABLE&lt;/span&gt; &lt;span class="n"&gt;events&lt;/span&gt;
&lt;span class="k"&gt;ADD&lt;/span&gt; &lt;span class="k"&gt;COLUMN&lt;/span&gt; &lt;span class="n"&gt;mat_&lt;/span&gt;&lt;span class="err"&gt;$&lt;/span&gt;&lt;span class="n"&gt;current_url&lt;/span&gt;
&lt;span class="nb"&gt;VARCHAR&lt;/span&gt; &lt;span class="n"&gt;MATERIALIZED&lt;/span&gt; &lt;span class="n"&gt;JSONExtractString&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;properties_json&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s1"&gt;'$current_url'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The above query creates a new column that is automatically filled for incoming data, creating a new file on disk. The data is automatically filled during &lt;code&gt;INSERT&lt;/code&gt; statements, so data ingestion doesn't need to change.&lt;/p&gt;

&lt;p&gt;The trade-off is more data being stored on disk. In practice, ClickHouse compresses data well, making this a worthwhile trade-off. On our test dataset, &lt;code&gt;mat_$current_url&lt;/code&gt; is only 1.5% the size of &lt;code&gt;properties_json&lt;/code&gt; on disk with a 10x compression ratio. Other properties which have lower cardinality can achieve even better compression (we’ve seen up to 100x)!&lt;/p&gt;

&lt;p&gt;Just creating the column is not enough though, since old data queries would still resort to using a &lt;code&gt;JSONExtract&lt;/code&gt;. For this reason, you want to backfill data. The easiest way currently is to run the &lt;a href="https://clickhouse.tech/docs/en/sql-reference/statements/optimize/"&gt;OPTIMIZE&lt;/a&gt; command:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight sql"&gt;&lt;code&gt;&lt;span class="n"&gt;OPTIMIZE&lt;/span&gt; &lt;span class="k"&gt;TABLE&lt;/span&gt; &lt;span class="n"&gt;events&lt;/span&gt; &lt;span class="k"&gt;FINAL&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;After backfilling, running the updated query speeds things up significantly:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight sql"&gt;&lt;code&gt;&lt;span class="k"&gt;SELECT&lt;/span&gt; &lt;span class="k"&gt;count&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="k"&gt;FROM&lt;/span&gt; &lt;span class="n"&gt;events&lt;/span&gt;
&lt;span class="k"&gt;WHERE&lt;/span&gt; &lt;span class="n"&gt;event&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s1"&gt;'$pageview'&lt;/span&gt;
  &lt;span class="k"&gt;AND&lt;/span&gt; &lt;span class="n"&gt;mat_&lt;/span&gt;&lt;span class="err"&gt;$&lt;/span&gt;&lt;span class="n"&gt;current_url&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s1"&gt;'https://app.posthog.com/login'&lt;/span&gt;
  &lt;span class="k"&gt;AND&lt;/span&gt; &lt;span class="nb"&gt;timestamp&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;=&lt;/span&gt; &lt;span class="s1"&gt;'2021-08-01'&lt;/span&gt;
  &lt;span class="k"&gt;AND&lt;/span&gt; &lt;span class="nb"&gt;timestamp&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="s1"&gt;'2021-09-01'&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Looking at &lt;code&gt;system.query_log&lt;/code&gt;, the new query:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Took 980ms (&lt;strong&gt;71%/3.4x improvement&lt;/strong&gt;)&lt;/li&gt;
&lt;li&gt;Read 14.36 GiB from disk (&lt;strong&gt;81%/5x improvement improvement&lt;/strong&gt;)&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The wins are even more magnified if more than one property filter is used at a time.&lt;/p&gt;

&lt;h2&gt;
  
  
  Backfilling efficiently
&lt;/h2&gt;

&lt;p&gt;Using &lt;code&gt;OPTIMIZE TABLE&lt;/code&gt; after adding columns is often not a good idea, since it will involve a lot of I/O as the whole table gets rewritten.&lt;/p&gt;

&lt;p&gt;As of writing, there's a feature request on &lt;a href="https://github.com/ClickHouse/ClickHouse/issues/27730"&gt;Github&lt;/a&gt; for adding specific commands for materializing specific columns on ClickHouse data parts.&lt;/p&gt;

&lt;p&gt;Here's how you can use &lt;code&gt;DEFAULT&lt;/code&gt; type columns to backfill more efficiently:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight sql"&gt;&lt;code&gt;&lt;span class="k"&gt;ALTER&lt;/span&gt; &lt;span class="k"&gt;TABLE&lt;/span&gt; &lt;span class="n"&gt;events&lt;/span&gt;
&lt;span class="k"&gt;ALTER&lt;/span&gt; &lt;span class="k"&gt;COLUMN&lt;/span&gt; &lt;span class="n"&gt;mat_&lt;/span&gt;&lt;span class="err"&gt;$&lt;/span&gt;&lt;span class="n"&gt;current_url&lt;/span&gt;
&lt;span class="nb"&gt;VARCHAR&lt;/span&gt; &lt;span class="k"&gt;DEFAULT&lt;/span&gt; &lt;span class="n"&gt;JSONExtractString&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;properties_json&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s1"&gt;'$current_url'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

&lt;span class="k"&gt;ALTER&lt;/span&gt; &lt;span class="k"&gt;TABLE&lt;/span&gt; &lt;span class="n"&gt;events&lt;/span&gt; &lt;span class="k"&gt;UPDATE&lt;/span&gt; &lt;span class="n"&gt;mat_&lt;/span&gt;&lt;span class="err"&gt;$&lt;/span&gt;&lt;span class="n"&gt;current_url&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;mat_&lt;/span&gt;&lt;span class="err"&gt;$&lt;/span&gt;&lt;span class="n"&gt;current_url&lt;/span&gt; &lt;span class="k"&gt;WHERE&lt;/span&gt; &lt;span class="nb"&gt;timestamp&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;=&lt;/span&gt; &lt;span class="s1"&gt;'2021-08-01'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="c1"&gt;-- Wait for mutations to finish before running this&lt;/span&gt;
&lt;span class="k"&gt;ALTER&lt;/span&gt; &lt;span class="k"&gt;TABLE&lt;/span&gt; &lt;span class="n"&gt;events&lt;/span&gt;
&lt;span class="k"&gt;ALTER&lt;/span&gt; &lt;span class="k"&gt;COLUMN&lt;/span&gt; &lt;span class="n"&gt;mat_&lt;/span&gt;&lt;span class="err"&gt;$&lt;/span&gt;&lt;span class="n"&gt;current_url&lt;/span&gt;
&lt;span class="nb"&gt;VARCHAR&lt;/span&gt; &lt;span class="n"&gt;MATERIALIZED&lt;/span&gt; &lt;span class="n"&gt;JSONExtractString&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;properties_json&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s1"&gt;'$current_url'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This will compute and store only the &lt;code&gt;mat_$current_url&lt;/code&gt; in our time range and is much more efficient than &lt;code&gt;OPTIMIZE TABLE&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Be aware though that this will:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Break your &lt;code&gt;INSERT&lt;/code&gt; statements if you don't specify column names explicitly&lt;/li&gt;
&lt;li&gt;Alter the behavior of &lt;code&gt;SELECT *&lt;/code&gt; queries&lt;/li&gt;
&lt;/ol&gt;

&lt;h2&gt;
  
  
  Usage at PostHog
&lt;/h2&gt;

&lt;p&gt;&lt;a href="https://posthog.com/blog/clickhouse-materialized-columns"&gt;PostHog&lt;/a&gt; as an analytics tool allows users to slice and dice their data in many ways across huge time ranges and datasets. This also means that performance is key when investigating things - but also that we currently do nearly no preaggregation.&lt;/p&gt;

&lt;p&gt;Rather than materialize all columns, we built a solution that looks at recent slow queries using &lt;code&gt;system.query_log&lt;/code&gt;, determines which properties need materializing from there, and backfills the data on a weekend. This works well because not every query needs optimizing and a relatively small subset of properties make up most of what’s being filtered on by our users.&lt;/p&gt;

&lt;p&gt;You can find the code for this &lt;a href="https://github.com/PostHog/posthog/blob/c23704b3909ae8ebb827e6a43453e32b3d3487bd/ee/clickhouse/materialized_columns/analyze.py#L42-L119"&gt;here&lt;/a&gt; and &lt;a href="https://github.com/PostHog/posthog/blob/c23704b3909ae8ebb827e6a43453e32b3d3487bd/ee/clickhouse/materialized_columns/columns.py#L37-L130"&gt;here&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;After materializing our top 100 properties and updating our queries, we analyzed slow queries (&amp;gt;3 seconds long). &lt;strong&gt;The average improvement in our query times was 55%, with 99th percentile improvement being 25x.&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;As a product, we're only scratching the surface of what ClickHouse can do to power product analytics. If you're interested in helping us with these kinds of problems, &lt;a href="https://posthog.com/careers"&gt;we're hiring&lt;/a&gt;!&lt;/p&gt;

</description>
      <category>opensource</category>
      <category>database</category>
      <category>datascience</category>
      <category>analytics</category>
    </item>
  </channel>
</rss>
