title: ClickHouse Materialized View generating too slow
body: <p>We collect raw events into a ClickHouse table. Table structure:</p>
  <pre><code>CREATE TABLE IF NOT EXISTS raw_events
  (
      owner_id                  UInt32,
      user_id                   UInt32,
      event_datetime            DateTime,
      event_type_id             UInt8,
      unique_id                 FixedString(18),
      data                      String,
      attr_1                    UInt32,
      attr_2                    UInt32
  )
      engine = MergeTree PARTITION BY toYYYYMMDD(event_datetime)
      ORDER BY (owner_id, user_id, event_type_id, event_datetime);
  </code></pre>
  <p>At the moment <em>raw_events</em> table contains <strong>120000000 (120M) rows</strong>.</p>
  <p>We aim to show our users some aggregate statistics for those events, thus we created a materialized view:</p>
  <pre><code>CREATE MATERIALIZED VIEW test
  ENGINE = AggregatingMergeTree() PARTITION BY (date)
  ORDER BY (owner_id, user_id, date)
  AS SELECT
         toYYYYMMDD(event_datetime) as date,
         owner_id,
         user_id,
         multiIf(
                     event_type_id == 1, 10,
                     event_type_id == 2, 20,
                     event_type_id == 3, 30,
                     event_type_id == 4, 40,
                     event_type_id == 32, 50,
                     event_type_id &gt;= 64, 60,
                     0
             ) as status,
         attr_1,
         attr_2,
         COUNT() as count,
         COUNT(DISTINCT unique_id) as unique_count
     FROM raw_events
     GROUP BY owner_id, user_id, date, status, attr_1, attr_2
     ORDER BY owner_id, user_id, date;
  </code></pre>
  <p>If we run the select query separately, it takes around 1 second to generate a response for a single owner_id. But creating a materialized view for the same select takes too much time. After executing <em>create materialized view</em> query it generated just <strong>200 records</strong> and it took it <strong>~10 minutes</strong>. So it looks like it will take days to completely build a view for the current 120M records table.</p>
  <p>What am I missing? Maybe there are some tricks for order/group clauses to make it run faster? At the moment it's much easier for me to just run select+group query instead of using a materialized view.</p>
  <p>Additional question: is there a way to check the progress for materialized view building for an existing table?</p>