Postgresql没有在没有时区列的时间戳上使用btree索引

时间:2018-04-11 07:11:58

标签: postgresql postgresql-9.4 postgresql-10

下面是我在postgresql中的表。

CREATE TABLE public.member_profile_events (
    id bigserial PRIMARY KEY,
    profileid varchar(80) NOT NULL,
    data jsonb,
    last_updated_at TIMESTAMP WITHOUT TIME ZONE NOT NULL DEFAULT now(),
    UNIQUE (profileid)
);
CREATE INDEX member_profile_events_last_updated_at ON public.member_profile_events USING (last_updated_at);

我正在每次插入和更新时更新last_updated_at = now()。

我使用下面的upsert查询为" profile1"," profile2",...等上传了多达350000个随机行。

INSERT INTO public.member_profile_events as e (profileid, data, last_updated_at)
    values (
        'profile1',
        '{"group1": {"score": 1,"recorddate": "2018-02-01 13:15:00"}}',
        now()
    )
ON CONFLICT (profileid)
    DO UPDATE SET data = '{"group1": {"score": 1,"recorddate": "2018-02-01 13:15:00","updated":"true"}}',
            last_updated_at = now();

但是,在运行以下查询之后:

explain analyze select * from member_profile_events where last_updated_at = '2018-04-11 12:49:35'::timestamp;

我发现postgres没有在last_updated_at列上使用索引。以下是输出:

 Seq Scan on member_profile_events  (cost=0.00..570.65 rows=1 width=130) (actual time=8.490..8.490 rows=0 loops=1)
   Filter: (last_updated_at = '2018-04-11 12:49:35'::timestamp without time zone)
   Rows Removed by Filter: 17963
 Planning time: 0.282 ms
 Execution time: 8.685 ms

有人可以告诉我这里出了什么问题。为什么postgres没有在last_updated_at字段上使用索引?

0 个答案:

没有答案