2012-09-13 29 views
3

我有几个带有json列的表,并且想要在这些列上构建索引。但是,我得到一个默认操作员类(http://www.postgresql.org/docs/9.2/static/sql-createopclass.html)。有人已经做过或者给我一个替代方案吗?在PostgreSQL 9.2的json字段上创建索引

要重现该问题,请尝试:

>> create table foo (json json, id int); 
CREATE TABLE 
>> create index on foo (id); 
CREATE INDEX 
>> create index on foo (json); 
ERROR: data type json has no default operator class for access method "btree" 
HINT: You must specify an operator class for the index or define a default operator class for the data type. 

这同样适用于gistgil索引。

>> create type "nested" as (json json, extra text); 
CREATE TYPE 
>> create table bar (id int, json nested); 
CREATE TABLE 
>> create index on bar (json); 
CREATE INDEX 

是不是因为是为组件创建没有索引:

有趣的是,当我尝试以下方法我没有得到这个错误吗?

好的,主要问题是默认的操作符。任何帮助或共享经验,赞赏。 谢谢。

回答

2

最适合我的情况的解决方案如下:

我只是把JSON作为文本并在文本上创建索引。然后

>> create index on foo ((json::text)); 

查询必须被转换,因此它使用的索引。

解释显示索引是否被使用。

>> explain select * from foo where json::text = 'foo'; 
1

对于JSON或XML类型没有内部索引类型。这些字段可以保存一个值,但不能将它作为索引 - 您需要使用hstore列或类似的辅助列。

相关问题