2012-09-13 28 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列或類似的輔助列。

相關問題