Updated on 2026-11-06 GMT+08:00

Syntax Reference

  • Create a regular materialized view.

    Create a materialized view based on a SQL query.

    CREATE MATERIALIZED VIEW [IF NOT EXISTS] view_name
    [(<col_name> [COMMENT <col_comment>], ...)]
    [COMMENT '...']
    [PARTITIONED BY col1, col2, ...]
    [ CLUSTERED BY (col_name3, col_name4, ...)
    [ SORTED BY (col_name [ ASC | DESC], ...)]
    INTO num_buckets BUCKETS]
    [TBLPROPERTIES ('key'='value', ...)]
    AS <SELECT query>;

    Examples:

    CREATE MATERIALIZED VIEW mv_partitioned
    PARTITIONED BY (region, product)
    AS SELECT region, product, SUM(amount) AS total
    FROM sales GROUP BY region, product
    CREATE MATERIALIZED VIEW mv_sorted_bucketed
    CLUSTERED BY (id) SORTED BY (amount ASC) INTO 2 BUCKETS
    AS SELECT * FROM sales
    CREATE MATERIALIZED VIEW mv_sales_with_product AS
    SELECT s.region, p.product_name, SUM(s.amount) AS total_amount
    FROM sales s
    INNER JOIN products p ON s.product_id = p.product_id
    GROUP BY s.region, p.product_name
  • Delete a materialized view.
    DROP MATERIALIZED VIEW [IF EXISTS] mv_revenue_per_customer;

    Example:

    DROP MATERIALIZED VIEW mv_sales
  • Query materialized views.

    Query all materialized views in the current database.

    SHOW MATERIALIZED VIEWS;

    Query all materialized views in a specific database.

    SHOW MATERIALIZED VIEWS FROM example_db;

    Query materialized views matching a specific pattern.

    SHOW MATERIALIZED VIEWS LIKE 'mv_*';
  • Refresh a materialized view.

    Recompute the data of the materialized view from the base tables.

    REFRESH MATERIALIZED VIEW mv_revenue_per_customer;
  • Alter materialized view properties.
    ALTER MATERIALIZED VIEW mv_name SET TBLPROPERTIES ('key1' = 'val1');
  • Drop materialized view properties.
    ALTER MATERIALIZED VIEW mv_name UNSET TBLPROPERTIES ('key1' = 'val1');

    The following properties cannot be altered using the ALTER command:

    • mv.archived: Whether the view is archived.
    • mv.base.table.valid: Whether the view can be used for SQL rewrite.
    • mv.base.tables: Base tables of the materialized view.
    • mv.base.table.names: Materialized view.
    • mv.select.statement.spark: SELECT statement corresponding to this materialized view in the Spark engine.
    • mv.base.tables.last.update.time: Last refresh time of the base tables of the materialized view.
    • mv.last.refresh.time: Last refresh time of the materialized view.
    • viewQuery: Query statement of the materialized view.
    • spark.mv.type: Type of the materialized view.

    The following property can be altered using the ALTER command:

    mv.refresh.policy: Refresh policy of the materialized view.

    Use the following syntax to alter the refresh policy:

    ALTER MATERIALIZED VIEW mv_altr ADD SCHEDULE REFRESH EVERY 3 DAYS
    ALTER MATERIALIZED VIEW mv_altr ALTER SCHEDULE REFRESH EVERY 2 DAYS
    ALTER MATERIALIZED VIEW mv_altr DROP SCHEDULE;