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

Use Cases

Materialized views physically store the results of a SQL query, allowing future queries to read the precomputed results directly instead of recomputing the same logic each time. This can significantly improve query performance, especially for frequently run queries, particularly those involving joins and aggregate operations.

When materialized views are enabled, the optimizer automatically rewrites eligible queries to use the materialized view instead of scanning the base tables.

Spark materialized views support the following use cases:

  • Aggregation scenarios: Materialized views precompute and store aggregation results, such as SUM, COUNT, and AVG.

    Example of creating a materialized view:

    CREATE MATERIALIZED VIEW mv AS SELECT deptno, SUM(salary) AS s FROM emps WHERE deptno > 5 GROUP BY deptno;

    Original query statement:

    SELECT deptno FROM emps
    WHERE deptno > 10 GROUP BY deptno

    Rewritten query statement:

    SELECT deptno FROM mv
    WHERE deptno > 10
    GROUP BY deptno
  • Join scenarios: The query's table join method matches the materialized view, or the materialized view can compensate for the join method.

    Example of creating a materialized view:

    CREATE MATERIALIZED VIEW mv AS
    SELECT o.order_id, c.customer_id
    FROM orders o INNER JOIN customers c ON o.cust_id = c.customer_id;

    Original query statement:

    SELECT order_id FROM orders INNER JOIN customers
    ON orders.cust_id = customers.customer_id;

    Rewritten query statement:

    SELECT order_id FROM mv
  • Predicate/Filter scenarios: If a query contains filter conditions that can be pushed down, the materialized view can precompute and store the results matching those conditions.

    Example of creating a materialized view:

    CREATE MATERIALIZED VIEW mv AS
    SELECT * FROM sales WHERE region = 'ASIA';

    Original query statement:

    SELECT customer_id
    FROM sales WHERE region = 'ASIA' AND revenue > 1000;

    Rewritten query statement:

    SELECT customer_id from mv WHERE revenue > 1000;

    The optimizer can use the materialized view and apply additional filter conditions to filter data where revenue > 1000.

  • Aggregate Rollup scenarios: When a materialized view contains finer-grained pre-aggregated data, the query can perform a roll up.

    Original query statement:

    SELECT deptno, COUNT(*) AS c, SUM(salary) AS s
    FROM emps
    GROUP BY deptno

    Example of creating a materialized view:

    CREATE MATERIALIZED VIEW mv AS
    SELECT empid, deptno, COUNT(*) AS c, SUM(salary) AS s
    FROM emps
    GROUP BY empid, deptno

    Rewritten query statement:

    SELECT deptno, SUM(c), SUM(s)
    FROM mv
    GROUP BY deptno
  • Partial join scenarios: The joined tables in the query are a superset of the joined tables in the materialized view.

    Example 1

    Example of creating a materialized view:
    CREATE MATERIALIZED VIEW join_mv1 AS
    SELECT lo_orderkey, lo_linenumber, lo_revenue, lo_partkey, c_name, c_address
    FROM lineorder INNER JOIN customer
    ON lo_custkey = c_custkey;

    Original query statement:

    SELECT lo_orderkey, lo_linenumber, lo_revenue, c_name, c_address, p_name
    FROM lineorder INNER JOIN customer ON lo_custkey = c_custkey
    INNER JOIN part ON lo_partkey = p_partkey;

    Rewritten query statement:

    SELECT lo_orderkey, lo_linenumber, lo_revenue, c_name, c_address, p_name
    FROM join_mv1 INNER JOIN part ON lo_partkey = p_partkey;

    Example 2

    Example of creating a materialized view:

    CREATE MATERIALIZED VIEW mv AS
    SELECT empid, deptno, state, SUM(salary) AS s
    FROM emps
    JOIN locations ON emps.locationid = locations.locationid
    GROUP BY empid, deptno, state

    Original query statement:

    SELECT deptname, state, SUM(salary) AS s
    FROM emps
    JOIN depts ON emps.deptno = depts.deptno
    JOIN locations ON emps.locationid = locations.locationid
    GROUP BY deptname, state

    Rewritten query statement:

    SELECT deptname, state, SUM(s)
    FROM mv
    JOIN depts ON mv.deptno = depts.deptno
    GROUP BY deptname, state;
  • Expression scenarios: Rewrite join queries with complex expressions (e.g., arithmetic operations, string functions, and OR predicates).

    Example of creating a materialized view:

    CREATE MATERIALIZED VIEW join_mv1
    AS
    SELECT lo_orderkey, lo_linenumber, lo_revenue, lo_partkey, c_name, c_address
    FROM lineorder INNER JOIN customer
    ON lo_custkey = c_custkey;

    Original query statement:

    SELECT
    lo_orderkey,
    lo_linenumber,
    (2 * lo_revenue + 1) * lo_linenumber,
    upper(c_name),
    substr(c_address, 3)
    FROM lineorder INNER JOIN customer
    ON lo_custkey = c_custkey;
  • Join transitivity scenarios: This refers to a scenario where the join type in the materialized view differs from the join type in the query, but the join result of the materialized view contains the join result of the query.

    Example 1

    Original query statement:

    SELECT lo_orderkey, lo_linenumber, lo_revenue, c_custkey, c_address
    FROM lineorder LEFT OUTER JOIN customer
    ON lo_custkey = c_custkey
    WHERE customer.c_address = 'Sb4gxKs7' and customer.c_address is not null;

    Example of creating a materialized view:

    CREATE MATERIALIZED VIEW join_mv4
    AS
    SELECT lo_orderkey, lo_linenumber, lo_revenue, c_custkey, c_address
    FROM lineorder INNER JOIN customer
    ON lo_custkey = c_custkey;

    Example 2

    Original query statement:

    SELECT lo_orderkey, lo_linenumber, lo_revenue, c_custkey, c_address
    FROM lineorder INNER JOIN customer
    ON lo_custkey = c_custkey WHERE  c_custkey IS NOT NULL;

    Example of creating a materialized view:

    CREATE MATERIALIZED VIEW join_mv3
    AS
    SELECT lo_orderkey, lo_linenumber, lo_revenue, c_custkey, c_address
    FROM lineorder LEFT OUTER JOIN customer
    ON lo_custkey = c_custkey WHERE c_custkey IS NOT NULL;

    For exact rewrite matching, the following string transformation functions, aggregation functions, and join types are supported:

    • Aggregation functions: SUM, COUNT, MIN, MAX, AVG, STDDEV_SAMP, and STDDEV_POP
    • String transformation functions: LOWER, UPPER, SUBSTR, CONCAT, and CONCAT_WS
    • Join Types: INNER, LEFT, RIGHT, and FULL