0

下面是我在 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 字段上使用索引?

4

0 回答 0