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

Configuring Doris Materialized Views

Doris materialized views are entities that contain both computation logic and data. Unlike views, which only contain computation logic and do not store data themselves, materialized views do.

Doris materialized views mainly apply to the query acceleration, lightweight ETL (data modeling), and lakehouse integration scenarios. Materialized views can be classified into the following types based on data timeliness:

  • Synchronous materialized view: A synchronous materialized view stores pre-computed data sets in Doris based on defined SELECT statements. Doris automatically maintains the data in synchronous materialized views, ensuring that any new imports or deletions in the base table are reflected in the materialized view in real-time, maintaining data consistency. When querying, Doris automatically selects the optimal materialized view and retrieves data directly from it.

    The SQL definition of a synchronous materialized view can use only single-table. Data is refreshed in real-time. Synchronous materialized views need to maintain strong consistency with the base table data and do not support direct queries.

  • Asynchronous materialized views: When creating a materialized view, the system simultaneously registers a refresh task. This task will run when needed, executing an INSERT OVERWRITE statement to write the latest data into the materialized view.

    The SQL definition of an asynchronous materialized view can use both single-table and multi-table. Asynchronous materialized views support both full refresh and partition incremental refresh, and maintain eventual consistency with the base table data and may have some delay. Asynchronous materialized views are typically used in scenarios where data timeliness is not critical and support direct queries.

This section applies only to MRS 3.6.0-LTS and later versions.

Applicable Scenarios

  • Synchronous materialized views are applicable to the following scenarios:
    • Accelerating time-consuming aggregation operations
    • Queries requiring prefix index matching
    • Reducing the amount of data scanned by pre-filtering
    • Speeding up queries by pre-computing complex expressions
  • Asynchronous materialized views are applicable to the following scenarios:
    • Query acceleration and concurrency improvement

      Materialized views can significantly enhance query speed while boosting the system's concurrent processing capabilities, effectively reducing resource consumption.

    • Simplifying ETL processes

      During the Extract, Transform, Load (ETL) process, materialized views can streamline workflows, improve development efficiency, and make data processing smoother.

    • Accelerating external table queries in lakehouse architecture

      In a lakehouse architecture, materialized views can significantly enhance the query speed for external data sources, improving data access efficiency.

    • Improving write efficiency

      By reducing resource contention, materialized views can optimize the data writing process, enhance write efficiency, and ensure data consistency and integrity.

Constraints

  • Synchronous materialized view
    • Synchronous materialized views only support SELECT statements for a single table, including WHERE, GROUP BY, and ORDER BY clauses, but do not support JOIN, HAVING, LIMIT clauses, or LATERAL VIEW.
    • Synchronous materialized views cannot be queried directly.
    • The SELECT list for synchronous materialized views cannot include auto-increment columns, constants, duplicate expressions, or window functions.
    • If the SELECT list contains aggregation functions, the functions must be root expressions (for example, sum(a) + 1 is supported, but sum(a + 1) is not supported), and no non-aggregation function expressions can follow the aggregation function (for example, SELECT x, sum(a) is allowed, but SELECT sum(a), x is not allowed).
    • If the condition column for a DELETE statement exists in the materialized view, the DELETE operation cannot proceed. If data deletion is necessary, the materialized view must be deleted first.
    • Excessive materialized views on a single table can impact import efficiency. When importing data, both the materialized views and the base table are updated synchronously. Excessive materialized views on a table can slow down imports, similar to importing data into multiple tables simultaneously.
    • Materialized views on Unique Key data models can only reorder columns and do not support aggregation. Therefore, coarse-grained aggregation operations cannot be performed through materialized views on Unique Key models.
  • Asynchronous materialized view
    • Asynchronous materialized views will eventually be consistent with the base table data, but they cannot be synchronized in real-time, meaning that real-time consistency cannot be maintained.
    • Currently, if a query contains window functions, it is not supported to transparently rewrite that query to utilize materialized views for acceleration.
    • If the number of tables involved in the materialized view creation exceeds the number of tables involved in the query (for example, if the query only involves t1 and t2, while the materialized view includes t1, t2, and an additional t3), the system currently does not support transparently rewriting that query to utilize the materialized view for acceleration.
    • If the materialized view contains set operations such as UNION ALL, LIMIT, ORDER BY, or CROSS JOIN, the materialized view can be built normally, but it cannot be used for transparent rewriting.

Creating a Doris Synchronous Materialized View

  1. Log in to the node where MySQL is installed and run the following command to connect to the Doris database:

    If Kerberos authentication is enabled for the cluster (the cluster is in security mode), run the following command to connect the Doris database:

    export LIBMYSQL_ENABLE_CLEARTEXT_PLUGIN=1

    mysql -uDatabase login user -p -PConnection port for FE queries -hIP address of the Doris FE instance

    Enter the password for logging in to the database.

    • To obtain the query connection port for a Doris FE instance, log in to FusionInsight Manager and choose Cluster > Services > Doris > Configurations. Locate the query_port parameter to view its value.
    • To obtain the IP address of a Doris FE instance, log in to FusionInsight Manager of the MRS cluster, choose Cluster > Services > Doris > Instances, and view the service IP address for the target instance.
    • You can also use the MySQL connection software or Doris web UI to connect to the database.

  2. Run the following commands to create a database, in this example, called test_db:

    create database test_db;

    use test_db;

  3. Run the following commands to create a table, in this example, called sales_records:

    create table sales_records

    (

    record_id int,

    seller_id int,

    store_id int,

    sale_date date,

    sale_amt bigint

    )

    distributed by hash(record_id)

    properties("replication_num" = "2");

  4. Run the following command to insert data to the table:

    insert into sales_records values(1,1,1,'2020-02-02',1);

  5. Run the following command to create a materialized view:

    create materialized view store_amt as select store_id,sum(sale_amt) from sales_records group by store_id;

  6. Run the following command to query the materialized view:

    select store_id,sum(sale_amt) from sales_records group by store_id;

    +----------+---------------+
    | store_id | sum(sale_amt) |
    +----------+---------------+
    |        1 |             1 |
    +----------+---------------+

    You can add explain to the query SQL statement to view VOlapScanNode in the execution plan for determining whether the query hits the materialized view. If there is no data in the table, then the materialized view may not be hit.

    explain select store_id,sum(sale_amt) from sales_records group by store_id;

    ...
    0:VOlapScanNode(148)                                               
    	TABLE: test_db.sales_records(store_amt), PREAGGREGATION: ON     
    	partitions=1/1 (sales_records)
    ...

  7. Run the following command to check the materialized view:

    show alter table materialized view from test_db;

  8. Run the following command to view the structure of all materialized views:

    desc sales_records all;

  9. Run the following command to view the creation statement of the materialized view:

    show create materialized view store_amt on sales_records;

  10. Run the following command to delete the materialized view:

    drop materialized view store_amt on sales_records;

Creating an Asynchronous Materialized View

  1. Log in to the node where MySQL is installed and run the following command to connect to the Doris database:

    If Kerberos authentication is enabled for the cluster (the cluster is in security mode), run the following command to connect the Doris database:

    export LIBMYSQL_ENABLE_CLEARTEXT_PLUGIN=1

    mysql -uDatabase login user -p -PConnection port for FE queries -hIP address of the Doris FE instance

    Enter the password for logging in to the database.

    • To obtain the query connection port for a Doris FE instance, log in to FusionInsight Manager and choose Cluster > Services > Doris > Configurations. Locate the query_port parameter to view its value.
    • To obtain the IP address of a Doris FE instance, log in to FusionInsight Manager of the MRS cluster, choose Cluster > Services > Doris > Instances, and view the service IP address for the target instance.
    • You can also use the MySQL connection software or Doris web UI to connect to the database.

  2. Run the following commands to create an asynchronous materialized view:

    CREATE MATERIALIZED VIEW

    [ IF NOT EXISTS ] <materialized_view_name>

    [ (<columns_definition>) ]

    [ BUILD <build_mode> ]

    [ REFRESH <refresh_method> [refresh_trigger]]

    [ [DUPLICATE] KEY (<key_cols>) ]

    [ COMMENT '<table_comment>' ]

    [ PARTITION BY (

    { <partition_col>

    | DATE_TRUNC(<partition_col>, <partition_unit>) }

    )]

    [ DISTRIBUTED BY { HASH (<distribute_cols>) | RANDOM }

    [ BUCKETS { <bucket_count> | AUTO } ]

    ]

    [ PROPERTIES (

    -- Table property

    <table_property>

    -- Additional table properties

    [ , ... ])

    ]

    AS <query>;

    • build_mode: indicates the refresh timing and determines whether to refresh immediately after materialized view creation.
      • IMMEDIATE: Refresh immediately (default mode).
      • DEFERRED: Delayed refresh.
    • refresh_method: indicates the refresh method. When refreshing the materialized view, Doris can only perceive data changes in internal tables and Hive data source tables. Support for other data sources is being gradually implemented.
      • COMPLETE: Refresh all partitions.
      • AUTO: Attempt incremental refresh, only refreshing partitions with data changes since the last materialization. Fall back to full refresh of all partitions if data changes cannot be detected.
    • refresh_trigger: indicates the trigger method.
      • ON MANUAL: Manual trigger. For example:

        REFRESH MATERIALIZED VIEW mvName [refresh_method];

        REFRESH MATERIALIZED VIEW mvName partitions(partitionName1,partitionName2);

      • ON SCHEDULE: Scheduled trigger. This parameter is used to specify the data refresh interval.
      • ON COMMIT: Automatic trigger.

  3. Create a base table and import data to the table.

    1. Create a base table, in this example, called lineitem.

      CREATE TABLE IF NOT EXISTS lineitem (

      l_orderkey integer not null,

      l_partkey integer not null,

      l_suppkey integer not null,

      l_linenumber integer not null,

      l_quantity decimalv3(15,2) not null,

      l_extendedprice decimalv3(15,2) not null,

      l_discount decimalv3(15,2) not null,

      l_tax decimalv3(15,2) not null,

      l_returnflag char(1) not null,

      l_linestatus char(1) not null,

      l_shipdate date not null,

      l_commitdate date not null,

      l_receiptdate date not null,

      l_shipinstruct char(25) not null,

      l_shipmode char(10) not null,

      l_comment varchar(44) not null

      )

      DUPLICATE KEY(l_orderkey, l_partkey, l_suppkey, l_linenumber)

      PARTITION BY RANGE(l_shipdate)

      (FROM ('2023-10-17') TO ('2023-11-01') INTERVAL 1 DAY)

      DISTRIBUTED BY HASH(l_orderkey) BUCKETS 3;

    1. Insert data into the table.

      INSERT INTO lineitem VALUES

      (1, 2, 3, 4, 5.5, 6.5, 7.5, 8.5, 'o', 'k', '2023-10-17', '2023-10-17', '2023-10-17', 'a', 'b', 'yyyyyyyyy'),

      (2, 4, 3, 4, 5.5, 6.5, 7.5, 8.5, 'o', 'k', '2023-10-18', '2023-10-18', '2023-10-18', 'a', 'b', 'yyyyyyyyy'),

      (3, 2, 4, 4, 5.5, 6.5, 7.5, 8.5, 'o', 'k', '2023-10-19', '2023-10-19', '2023-10-19', 'a', 'b', 'yyyyyyyyy');

    2. Create a base table, in this example, called orders.

      CREATE TABLE IF NOT EXISTS orders (

      o_orderkey integer not null,

      o_custkey integer not null,

      o_orderstatus char(1) not null,

      o_totalprice decimalv3(15,2) not null,

      o_orderdate date not null,

      o_orderpriority char(15) not null,

      o_clerk char(15) not null,

      o_shippriority integer not null,

      o_comment varchar(79) not null

      )

      DUPLICATE KEY(o_orderkey, o_custkey)

      PARTITION BY RANGE(o_orderdate)(

      FROM ('2023-10-17') TO ('2023-11-01') INTERVAL 1 DAY)

      DISTRIBUTED BY HASH(o_orderkey) BUCKETS 3;

    3. Insert data into the table.

      INSERT INTO orders VALUES

      (1, 1, 'o', 9.5, '2023-10-17', 'a', 'b', 1, 'yy'),

      (1, 1, 'o', 10.5, '2023-10-18', 'a', 'b', 1, 'yy'),

      (2, 1, 'o', 11.5, '2023-10-19', 'a', 'b', 1, 'yy'),

      (3, 1, 'o', 12.5, '2023-10-19', 'a', 'b', 1, 'yy');

  4. Create a materialized view.

    • Example 1:

      CREATE MATERIALIZED VIEW mv_1_0

      BUILD IMMEDIATE

      REFRESH AUTO

      ON MANUAL

      DISTRIBUTED BY RANDOM BUCKETS 2

      AS

      SELECT

      l_linestatus,

      to_date(o_orderdate) as date_alias,

      o_shippriority

      FROM

      orders

      LEFT JOIN lineitem ON l_orderkey = o_orderkey;

    • Example 2

      CREATE MATERIALIZED VIEW mv_1_1

      BUILD DEFERRED

      REFRESH COMPLETE

      ON SCHEDULE EVERY 1 DAY STARTS '2025-12-01 20:30:00'

      DISTRIBUTED BY RANDOM

      AS

      SELECT

      l_linestatus,

      to_date(o_orderdate) as date_alias,

      o_shippriority

      FROM

      orders

      LEFT JOIN lineitem ON l_orderkey = o_orderkey;

    • Example 3

      CREATE MATERIALIZED VIEW mv_1_2

      BUILD IMMEDIATE

      REFRESH AUTO

      ON SCHEDULE EVERY 1 hour

      DISTRIBUTED BY RANDOM BUCKETS 3

      AS

      SELECT t1.l_linenumber,

      o_custkey,

      o_orderdate

      FROM (SELECT * FROM lineitem WHERE l_linenumber > 1) t1

      LEFT OUTER JOIN orders

      ON l_orderkey = o_orderkey;

  5. Query the materialized view (direct querying of a materialized view).

    Query the base table:

    SELECT t1.l_linenumber,

    o_custkey,

    o_orderdate

    FROM (SELECT * FROM lineitem WHERE l_linenumber > 1) t1

    LEFT OUTER JOIN orders

    ON l_orderkey = o_orderkey

    WHERE o_orderdate = '2023-10-18';

    After SQL execution, Doris will use transparent rewriting to perform equivalent direct query on the materialized view:

    SELECT l_linenumber,o_custkey,o_orderdate FROM mv_1_2 WHERE l_linenumber > 1 and o_orderdate = '2023-10-18';

    +--------------+-----------+-------------+
    | l_linenumber | o_custkey | o_orderdate |
    +--------------+-----------+-------------+
    |            4 |         1 | 2023-10-18  |
    +--------------+-----------+-------------+

  6. Run the following commands to modify the materialized view:

    ALTER MATERIALIZED VIEW mv_1

    SET(

    "grace_period" = "10"

    );

  7. Run the following command to view the creation statement of the materialized view:

    SHOW CREATE MATERIALIZED VIEW mv_1;

  8. Run the following command to delete the materialized view:

    DROP MATERIALIZED VIEW mv_1;

  9. Run the following command to pause the refresh task of a materialized view:

    PAUSE MATERIALIZED VIEW JOB ON <mv_name>

  10. Run the following command to resume a materialized view:

    RESUME MATERIALIZED VIEW JOB ON <mv_name>

  11. Run the following command to cancel the refresh task of a materialized view:

    CANCEL MATERIALIZED VIEW TASK <task_id> ON <mv_name>

  12. Doris supports transparent rewriting. For details, see Transparent Query Rewriting.

    Transparent rewriting means that, during query processing, users do not need to manually modify queries, as the system will automatically optimize and rewrite them. Doris provides rich and comprehensive transparent rewriting capabilities.

    • Condition compensation: Query and materialized view conditions do not need to be exactly the same. By compensating conditions on materialized views to express queries, materialized views can be reused to the maximum extent, avoiding the need to repeatedly build materialized views.
    • JOIN rewriting: The query and materialized view use the same tables. Conditions (where) can be written in the materialized view, JOIN inputs, or outside the JOIN. The optimizer will attempt transparent rewriting for queries in this pattern.
    • JOIN derivation: When the JOIN types in the query and materialized view are inconsistent, if the materialized view can provide all the data needed by the query, transparent rewriting can still be performed by compensating predicates outside the JOIN.
    • Aggregate rewriting: When the group dimensions in the query and materialized view definition are consistent, if the materialized view uses the same group by dimensions as the query, and the aggregate functions used in the query can be expressed using the materialized view's aggregate functions, transparent rewriting can be performed.
    • Partition compensation: When a partitioned materialized view cannot provide all the data needed by the query, a union all approach can be used, combining data from the original table and the materialized view as the final result.