amrdb=> \d+ t_index
Table "XXXXXX_shm.t_index"
Column | Type | Modifiers | Storage | Stats target | Description
--------+-----------------------------+-----------+----------+--------------+--------------
sjid | numeric(13,0) | not null | main | |
col1 | character varying(20) | | extended | |
col2 | character varying(20) | | extended | |
col3 | character varying(32) | | extended | |
col4 | character varying(32) | | extended | |
col5 | numeric(5,1) | default 1 | main | |
col6 | numeric(1,0) | | main | |
col7 | timestamp without time zone | | plain | |
Indexes:
"t_index_pkey" PRIMARY KEY, btree (sjid)
"t_index_col2_idx" btree (col2)
db=> insert into t_index(sjid,col2)select id,id::varchar from generate_series(1,10000000) as id;
INSERT 0 10000000
#用到索引(正常)
db=> explain select * from t_index where hh='1111';
QUERY PLAN
----------------------------------------------------------------------------------------
Gather Motion 8:1 (slice1; segments: 8) (cost=0.18..200.20 rows=1 width=258)
-> Index Scan using t_index_col2_idx on t_index (cost=0.18..200.20 rows=1 width=258)
Index Cond: ((col2)::text = '1111'::text)
Optimizer: Postgres query optimizer
#删除数据后,仍然走索引(有问题)
db=> delete from t_index;
DELETE 10000000
db=> explain select * from t_index where hh='1111';
QUERY PLAN
----------------------------------------------------------------------------------------
Gather Motion 8:1 (slice1; segments: 8) (cost=0.18..200.20 rows=1 width=258)
-> Index Scan using t_index_col2_idx on t_index (cost=0.18..200.20 rows=1 width=258)
Index Cond: ((col2)::text = '1111'::text)
Optimizer: Postgres query optimizer
#vacuum完成后,不走索引(正常)
amrdb=> vacuum t_index;
VACUUM
amrdb=> explain select * from t_index where hh='1111';
QUERY PLAN
------------------------------------------------------------------------------
Gather Motion 8:1 (slice1; segments: 8) (cost=0.00..1.00 rows=1 width=258)
-> Seq Scan on t_index (cost=0.00..1.00 rows=1 width=258)
Filter: ((col2)::text = '1111'::text)
Optimizer: Postgres query optimizer
(4 rows)