更新时间:2026-11-23 GMT+08:00

物化视图设计

物化视图设计的核心原则:“相似归一,找公共集合”,减少重复计算。

此原则是指通过分析历史高频相似SQL(基于Unique SQL ID),提取归纳公共子查询并创建物化视图的过程,其步骤主要为:

  1. TopSQL分析
    1
    2
    3
    4
    5
    SELECT unique_sql_id, count(*) as freq
    FROM pgxc_wlm_session_info
    WHERE query LIKE '%table_name%'
    GROUP BY unique_sql_id
    ORDER BY freq DESC;
    
  2. 归纳公共计算。
    • 相同表上的相似聚合。
    • 相同表连接的不同查询。
    • 常用的过滤条件组合。
  3. 设计物化视图
    • 提取更基础、更简单的计算。
    • 复用频率越高越好。
    • 优先物化底层视图。

示例

  1. 建表并插入数据。

     1
     2
     3
     4
     5
     6
     7
     8
     9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    CREATE TABLE orders (
        order_id    int,
        region      varchar(20),
        product     varchar(20),
        amount      numeric(10,2),
        order_date  date
    ) 
    WITH (orientation=column)
    DISTRIBUTE BY hash(order_id);
    
    INSERT INTO orders VALUES
    (1, 'North', 'phones', 1000.00, '2024-01-05'),
    (2, 'North', 'Laptops', 3000.00, '2024-01-10'),
    (3, 'North', 'phones', 1500.00, '2024-01-15'),
    (4, 'East', 'phones', 2000.00, '2024-01-08'),
    (5, 'East', 'Laptops', 5000.00, '2024-01-12'),
    (6, 'East', 'Tablets', 800.00, '2024-01-20'),
    (7, 'South', 'Laptops', 4500.00, '2024-01-06'),
    (8, 'South', 'Tablets', 1200.00, '2024-01-18'),
    (9, 'South', 'phones', 1800.00, '2024-01-22'),
    (10,'North', 'Tablets', 600.00, '2024-01-25');
    

  2. 创建物化视图。

    创建物化视图mv_region_product,使用最细粒度(region+product),同时存储SUM和COUNT。

     1
     2
     3
     4
     5
     6
     7
     8
     9
    10
    11
    12
    13
    CREATE MATERIALIZED VIEW mv_region_product
    REFRESH COMPLETE
    ENABLE QUERY REWRITE
    WITH (orientation=column,enable_hstore_opt = ON)
    DISTRIBUTE BY hash(region)
    AS
    SELECT
        region,
        product,
        sum(amount)   AS total_amount,
        count(amount) AS cnt_amount
    FROM orders
    GROUP BY region, product;
    

  3. 刷新物化视图。

    1
    REFRESH MATERIALIZED VIEW mv_region_product;
    

  4. 查询按照region聚合。

    1
    2
    3
    SELECT region, sum(amount)
    FROM orders
    GROUP BY region;
    

  5. 查看执行计划,预期使用物化视图重写。

     1
     2
     3
     4
     5
     6
     7
     8
     9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    EXPLAIN (verbose on, costs off, nodes off)
    SELECT region, sum(amount)
    FROM orders
    GROUP BY region;
                             QUERY PLAN                          
    -------------------------------------------------------------
      id |                      operation                       
     ----+------------------------------------------------------
       1 | ->  Row Adapter                                      
       2 |    ->  Vector Streaming (type: GATHER)               
       3 |       ->  Vector Sonic Hash Aggregate                
       4 |          ->  CStore Scan on public.mv_region_product 
    
     Targetlist Information (identified by plan id)
     ----------------------------------------------
       1 --Row Adapter
             Output: region, (sum(total_amount))
       2 --Vector Streaming (type: GATHER)
             Output: region, (sum(total_amount))
       3 --Vector Sonic Hash Aggregate
             Output: region, sum(total_amount)
             Group By Key: mv_region_product.region
       4 --CStore Scan on public.mv_region_product
             Output: region, total_amount
             Distribute Key: region
    

物化视图设计原则

  • 物化视图的设计越基础,复用频率越高,收益越大。
  • 避免过度物化,增加系统负担。
  • 合理设计刷新策略,平衡性能与实时性。