Creating Materialized Views

Creating Materialized Views

Viewing typescript

switch to python

Materialized views help summarize and transform large datasets into easily queryable tables, precomputing results for efficient data retrieval.

Setting up Materialized Views with createAggregation()

Ensure Block Initialization

Before setting up your aggregation, make sure you have already initialized a new block.

The createAggregation() function in Moose makes it easy to define a materialized view by specifying a query and a destination table for your aggregated data.

Example

/blocks/example-aggregation.ts
import {
  createAggregation,
  AggregationCreateOptions,
  Blocks,
  ClickHouseEngines,
} from "@514labs/moose-lib";
 
const destinationTable = "UserActivitySummary";
const materializedView = "UserActivitySummaryMV";
 
// Define the table options
const tableCreateOptions = {
  name: destinationTable, 
  columns: {
    activity: "String",
    unique_user_count: "AggregateFunction(uniq, String)",
    activity_count: "AggregateFunction(count, String)",
  },
  orderBy: "activity",
  engine: ClickHouseEngines.AggregatingMergeTree,
} as TableCreateOptions;
 
// Define the query to aggregate the data
const selectQuery = `
  SELECT 
    activity,
    uniqState(userId) as unique_user_count, 
    countState(activity) AS activity_count 
  FROM 
    ParsedActivity_0_0 
  GROUP BY 
    activity
`;
 
// Create the aggregation
export default {
  setup: createAggregation({
    tableCreateOptions: tableCreateOptions,
    materializedViewName: materializedView, 
    select: selectQuery,
  }),
} as Blocks;

Example Explanation

  • tableCreateOptions: Defines the destination table schema
    • name: Table name for aggregated data
    • columns: Column names and data types
    • orderBy: Column for table ordering
    • engine: ClickHouse engine (e.g., AggregatingMergeTree for efficient aggregations)
  • materializedViewName: Name of the materialized view
  • select: SQL query for data aggregation from the source table
Workflow Tip

Test your query in a SQL explorer for a more efficient workflow. Once satisfied, copy and paste it into the aggregation file. Be sure to validate your query using the Clickhouse SQL Reference (opens in a new tab) to ensure it adheres to ClickHouse's syntax.

Materialized View Creation Process

When you save the file, Moose:

  1. Creates a new table using the defined structure in tableCreateOptions
  2. Executes the select query to aggregate data
  3. Populates the destination table with the results
Automatic Updates

ClickHouse Materialized Views automatically refresh your target table when new data is added to source tables. Learn more (opens in a new tab)


API Reference

Blocks Object

  • setup (string[]): SQL statements to set up the aggregation
  • teardown (string[]): SQL statements to tear down the aggregation

createAggregation() Function

Sets up a materialized view based on a SQL query.

Parameters

  • options (AggregationCreateOptions):
    • tableCreateOptions: Destination table creation options
    • materializedViewName: Materialized view name
    • select: Aggregation SQL query

Returns

string[]: SQL statements to set up the materialized view and destination table

dropAggregation() Function

Removes an existing aggregation.

Parameters

  • options (AggregationDropOptions):
    • viewName: Materialized view name to drop
    • tableName: Destination table name to drop

Returns

string[]: SQL statements to drop the specified materialized view and table

ClickHouseEngines

Enum defining available ClickHouse storage engines in Moose.

enum ClickHouseEngines {
  MergeTree,
  ReplacingMergeTree,
  SummingMergeTree,
  AggregatingMergeTree,
  CollapsingMergeTree,
  VersionedCollapsingMergeTree,
  GraphiteMergeTree,
}
ClickHouse Table Engines

Learn more about ClickHouse table engine types and use cases in the ClickHouse documentation (opens in a new tab).