Indexing 为什么我的时间戳索引没有被返回200条3亿条记录的查询使用

Indexing 为什么我的时间戳索引没有被返回200条3亿条记录的查询使用,indexing,postgresql-9.5,Indexing,Postgresql 9.5,我有一张桌子 它是一个简单的表,称为带有一些时间戳字段的事件 CREATE TABLE public.event ( id int8 NOT NULL DEFAULT nextval('event_id_seq'::regclass), email_id bpchar(36) NOT NULL, sending timestamp NULL, CONSTRAINT event_pkey PRIMARY KEY (id) ) WITH ( OIDS=FALSE ); CREATE INDE

我有一张桌子

它是一个简单的表,称为带有一些时间戳字段的事件

CREATE TABLE public.event (
id int8 NOT NULL DEFAULT nextval('event_id_seq'::regclass),
email_id bpchar(36) NOT NULL,
sending timestamp NULL,
CONSTRAINT event_pkey PRIMARY KEY (id)
)
WITH (
   OIDS=FALSE
);

CREATE INDEX email_idx ON public.event (email_id);

CREATE INDEX sending_idx ON public.event ((sending::date));
其中一个时间戳字段称为sending,它有一个日期索引

问题是postgres正在使用seqscan检索以下查询的结果:

select email_id from event where sending between  '2018-01-07 00:33:00'  and '2018-01-07 00:33:20' 
我进行了解释分析,结果如下:

Seq Scan on event  (cost=0.00..11391171.08 rows=139 width=37) (actual time=265885.080..503636.060 rows=257 loops=1)
Filter: ((sending >= '2018-01-07 00:33:00'::timestamp without time zone) AND (sending <= '2018-01-07 00:33:20'::timestamp without time zone))
 Rows Removed by Filter: 317633116
Planning time: 0.066 ms
Execution time: 503650.634 ms
为什么postgres要执行seqscan,通过索引字段从数百万条记录中检索数百条记录

谢谢

您没有索引时间戳,而是索引了日期。在where子句中,还需要将列与日期进行比较,以使索引可用

将其与日期进行比较:

where sending between date '2018-01-07'  and date '2018-01-07' 
或在时间戳值上创建索引:

CREATE INDEX sending_idx ON public.event (sending);
那么您的原始查询应该使用索引

别忘了分析你的表格,让你的统计数据保持最新