查看是否使用上索引

最近更新时间: 2025-02-18 16:02:00

  • 复制
    复制成功
postgres=# create index tbase_2_f2_idx on tbase_2(f2); 
CREATE INDEX
postgres=# explain select * from tbase_2 where f2=1;
                                     QUERY PLAN                                      
-------------------------------------------------------------------------------------
 Remote Fast Query Execution  (cost=0.00..0.00 rows=0 width=0)
   Node/s: dn001, dn002
   ->  Index Scan using tbase_2_f2_idx on tbase_2  (cost=0.42..4.44 rows=1 width=14)
         Index Cond: (f2 = 1)
(4 rows)
postgres=# explain select * from tbase_2 where f3='1';
                                   QUERY PLAN                                   
--------------------------------------------------------------------------------
 Remote Fast Query Execution  (cost=0.00..0.00 rows=0 width=0)
   Node/s: dn001, dn002
   ->  Gather  (cost=1000.00..7827.20 rows=1 width=14)
         Workers Planned: 2
         ->  Parallel Seq Scan on tbase_2  (cost=0.00..6827.10 rows=1 width=14)
               Filter: (f3 = '1'::text)
(6 rows)
postgres=# 

第一个查询使用了索引,第二个没有使用索引,通常情况下,使用索引可以加速查询速度,但要记住索引也会增加更新的开销。