index全表扫描

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

示例:/+index(table_name)/对于指定表的查询强制选择索引

  • 复制
    复制成功
postgres=# create table hint_t2(f1 integer,f2 integer) ;
CREATE TABLE
postgres=# create index hint_t2_f1_idx on hint_t2(f1);
CREATE INDEX
postgres=# insert into hint_t2 select t as f1,t as f2 from generate_series(1,1000) as t;    
INSERT 0 1000
postgres=# vacuum ANALYZE hint_t2;
VACUUM
postgres=#
postgres=# explain select /*+index(hint_t2) */ * from hint_t2 where f1>1; 
                                       QUERY PLAN                                       
----------------------------------------------------------------------------------------
 Remote Subquery Scan on all (dn001)  (cost=0.15..28.65 rows=1000 width=8)
   ->  Index Scan using hint_t2_f1_idx on hint_t2  (cost=0.15..28.65 rows=1000 width=8)
         Index Cond: (f1 > 1)
(3 rows) 
postgres=# explain select /*+index(hint_t2 hint_t2_f1_idx1) */ * from hint_t2 where f1>1; 
                                          QUERY PLAN                                          
----------------------------------------------------------------------------------------------
 Remote Subquery Scan on all (dn001)  (cost=10000000000.00..10000000020.50 rows=1000 width=8)
   ->  Seq Scan on hint_t2  (cost=10000000000.00..10000000020.50 rows=1000 width=8)
         Filter: (f1 > 1)
(3 rows)