Postgresql 如何索引jsonb整数值

Postgresql 如何索引jsonb整数值,postgresql,indexing,gwt-gin,jsonb,Postgresql,Indexing,Gwt Gin,Jsonb,我正在尝试使用新的JSONB类型 我有一个带有属性jsonb字段的documents表,其中有一个字段publication\u year。我想查找一年范围内的所有文件记录,例如2013-2015年。[编辑:这里的主要挑战是查询一系列值,尽管我在下面使用了一个精确匹配的示例。所要求的方法也适用于,例如美元范围价格>20美元和价格>'price' as int)); explain analyze select * from tt where cast(jb->>'price' as int)

我正在尝试使用新的JSONB类型

我有一个带有属性jsonb字段的documents表,其中有一个字段publication\u year。我想查找一年范围内的所有文件记录,例如2013-2015年。[编辑:这里的主要挑战是查询一系列值,尽管我在下面使用了一个精确匹配的示例。所要求的方法也适用于,例如美元范围价格>20美元和价格<40美元或时间戳范围。]

我试过:

create index test1 on documents using gin ((cast(properties->'announced_on_year' as integer)));

ERROR:  cannot cast type jsonb to integer
以及:

create index test1 on documents using gin (cast(properties->>'publication_year' as integer));

ERROR:  data type integer has no default operator class for access method "gin"
HINT:  You must specify an operator class for the index or define a default operator class for the data type.`
我在这篇文章中看到,这应该是可能的,但我想不出正确的语法

当我只做一个简单的索引时:

create index test1 on documents using gin ((properties->'publication_year'));
“创建了一个索引,但我不能用整数值来查询它,以得到一个范围,”它说

select count(*) from documents where properties->>'publication_year' = 2015;
ERROR:  operator does not exist: text = integer
LINE 1: ...*) from documents where properties->>'publication_year' = 2015;
                              ^
HINT:  No operator matches the given name and argument type(s). You might need to add explicit type casts.

非常感谢任何提示和提示。我相信其他人也会受益。TIA

1整数没有GIN索引,至少不是现成的,请使用btree

create index test1 on documents using btree (cast (properties->>'announced_on_year' as int));
2该错误很容易解释,将整数转换为文本或使用文本进行比较:

select count(*) from documents where properties->>'publication_year' = '2015';

为什么不为整个jsonb字段定义一个索引呢


您可以强制转换为整数并使用contrib/btree\u-gin扩展名

create extension btree_gin; create index tt_jb_int_idx on tt using gin( cast (jb->>'price' as int)); explain analyze select * from tt where cast(jb->>'price' as int) > 3 and cast(jb->>'price' as int) > 'price'::text))::integer > 3) AND (((jb ->> 'price'::text))::integer Bitmap Index Scan on tt_jb_int_idx (cost=0.00..28.06 rows=6 width=0) (actual time=0.016..0.016 rows=1 loops=1) Index Cond: ((((jb ->> 'price'::text))::integer > 3) AND (((jb ->> 'price'::text))::integer
根据我的经验,在JSONB列上使用GIN索引并没有更快。您可以通过将其强制转换为整数来创建普通索引

CREATE INDEX test1 ON documents ((properties->>'publication_year')::int);
此外,杜松子酒还有一些在创建之前应该考虑的因素。即使对整个JSONB列进行索引,也会产生大量表大小的索引


这是基于我的经验和对Postgres文档的浏览。

谢谢,这很有帮助,但它并没有解决我的主要问题:范围查询也许我应该更明确一些。最终目标类似于查找出版年份大于2012年且小于2016年的所有记录。@Willkesler您可以使用b树索引来实现这一点。你试过了吗?我试过了,但问得不对。但是现在在你和穆里森的帮助下,我明白了,请看上面对穆里森回应的评论。不过,谢谢你的帮助。谢谢你——我想这就是我所寻求的答案。即使文档没有指定,您也可以使用此解决方案使用CAST获取范围结果,如:`EXPLAIN ANALYSE SELECT COUNT*,其中castproperties->“publication\u year”为整数>2012,castproperties->“publication\u year”为整数<2016;我不确定,如果以这种方式使用它,是否会对性能产生影响,我猜这就是目标。根据doc,这种类型的索引不支持->>运算符。此外,您还可以将x>A和xCREATE INDEX test1 ON documents ((properties->>'publication_year')::int);