索引扫描不适用于 postgres 中的 json 数据集

Index scan is not working on json data set in postgres

我正在尝试了解如何使用某些过滤条件从 PostgreSQL 中的 JSON 文件中提取数据。

这是我的查询,

创建如下索引,

CREATE INDEX idx_startTimeL_n
    ON mytable USING btree
    (((data -> 'info'::text) ->> 'startTimeL'::text) ) 

如果我运行解释select查询

EXPLAIN   SELECT * FROM mytable
      WHERE (((data -> 'info'::text) ->> 'startTimeL'::text)::double precision) <= (date_part('epoch'::text, now()) * 1000::double precision)
      AND ((data -> 'info'::text) ->> 'startTimeL'::text)::double precision) >= (date_part('epoch'::text, now()) * 1000::double precision - 3600000::double precision)
     LIMIT 400000;

查询规划器结果是,

"Limit  (cost=0.00..36371.90 rows=220700 width=1568)"
"  ->  Seq Scan on mytable  (cost=0.00..36371.90 rows=220700 width=1568)"
"        Filter: (((((data -> 'info'::text) ->> 'startTimeL'::text))::double precision <= (date_part('epoch'::text, now()) * '1000'::double precision)) AND ((((data -> 'info'::text) ->> 'startTimeL'::text))::double precision >= ((date_part('epoch'::text, now()) * '1000'::double precision) - '3600000'::double precision)))"

所以,我的问题是,为什么 seq scan 发生而不是 index scan,即使 table 使用过滤条件索引?

您的示例查询有语法错误,不平衡的括号。

如果要查找双精度索引,则必须这样定义索引。

CREATE INDEX ON mytable USING btree
    (((data -> 'info' ->> 'startTimeL')::double precision))