我定义了下表和索引:
CREATE TABLE ticket (
wid bigint NOT NULL DEFAULT nextval('tickets_id_seq'::regclass),
eid bigint,
created timestamp with time zone NOT NULL DEFAULT now(),
status integer NOT NULL DEFAULT 0,
argsxml text,
moduleid character varying(255),
source_id bigint,
file_type_id bigint,
file_name character varying(255),
status_reason character varying(255),
...
)
我在时间戳上创建了一个索引,created
如下所示:
CREATE INDEX ticket_1_idx
ON ticket
USING btree
(created );
这是我的查询:
select * from ticket
where created between '2012-12-19 00:00:00' and '2012-12-20 00:00:00'
在记录数量开始增长(大约 500 万条)之前,这一直运行良好,现在它需要永远返回。
解释分析揭示了这一点:
Index Scan using ticket_1_idx on ticket (cost=0.00..10202.64 rows=52543 width=1297) (actual time=0.109..125.704 rows=53340 loops=1)
Index Cond: ((created >= '2012-12-19 00:00:00+00'::timestamp with time zone) AND (created <= '2012-12-20 00:00:00+00'::timestamp with time zone))
Total runtime: 175.853 ms
到目前为止,我已经尝试过设置:
random_page_cost = 1.75
effective_cache_size = 3
还创建了:
create CLUSTER ticket USING ticket_1_idx;
没有任何效果。我究竟做错了什么?为什么选择顺序扫描?索引应该使查询快速。有什么可以优化的吗?