物化视图和索引

the*_*he1 5 postgresql performance optimization query-performance

有没有办法强制 PostgreSQL 使用我在物化视图表上创建的索引?我尝试了移动列属性的所有可能组合,以使 Postgres 使用我的索引......

我有这张表:

=> explain select * from flight3mv1;
                         QUERY PLAN                          
-------------------------------------------------------------
 Seq Scan on flight3mv1  (cost=0.00..3.50 rows=150 width=26)
(1 row)
Run Code Online (Sandbox Code Playgroud)

当我在物化视图上创建索引时,我的查询计划没有任何变化。

=> create index flight3mv1index on flight3mv1(c_nation,s_nation);
=> analyze flight3mv1;
=> explain select * from flight3mv1 where c_nation='CHINA' and s_nation='CHINA';

                                      QUERY PLAN                                       
---------------------------------------------------------------------------------------
 Seq Scan on flight3mv1  (cost=0.00..4.25 rows=6 width=26)
   Filter: (((c_nation)::text = 'CHINA'::text) AND ((s_nation)::text = 'CHINA'::text))
(2 rows)
Run Code Online (Sandbox Code Playgroud)

这是我的 Flight3mv1 表:

=> \d flight3mv1
    Materialized view "public.flight3mv1"
  Column  |         Type          | Modifiers 
----------+-----------------------+-----------
 c_nation | character varying(15) | 
 s_nation | character varying(15) | 
 d_year   | integer               | 
 revenue  | bigint                | 
Indexes:
    "flight3mv1index" btree (c_nation, s_nation) CLUSTER
Run Code Online (Sandbox Code Playgroud)

war*_*yen 0

-- Step 1: Index the underlying table
CREATE INDEX flight3mv1_index ON flight3mv1 (c_nation, s_nation);

-- Step 2: Refresh the materialized view
REFRESH MATERIALIZED VIEW flight3mv1;

-- Step 3: Query the materialized view
EXPLAIN SELECT * FROM flight3mv1 WHERE c_nation = 'CHINA' AND s_nation = 'CHINA';
Run Code Online (Sandbox Code Playgroud)