我有一个表,其中的架构如下:
CREATE TABLE table1(
account VARCHAR(64) NOT NULL,
json JSONB NOT NULL
);
Run Code Online (Sandbox Code Playgroud)
我想在其上创建一个索引,以允许我运行以下查询:
SELECT *
FROM table1
WHERE account = :account
AND json ... -- Some JSON search expression
Run Code Online (Sandbox Code Playgroud)
所以我尝试在这个表上添加一个索引,如下所示:
CREATE INDEX index1 ON table1 USING GIN (account, json);
Run Code Online (Sandbox Code Playgroud)
我得到:
ERROR: data type character varying 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.
Run Code Online (Sandbox Code Playgroud)
我试过添加_varchar_ops:
CREATE INDEX index1 ON table1 USING GIN (account _varchar_ops, json);
Run Code Online (Sandbox Code Playgroud)
然后我得到:
ERROR: operator class "_varchar_ops" does not accept data type character varying
Run Code Online (Sandbox Code Playgroud)
我意识到我可以简单地将帐户添加为每个 json 文档的字段,但我想避免这种重复性和潜在的陷阱或差异空间,并将帐户作为列。
如何解决这个问题并获得这两个字段的组合索引?
只需创建两个单独的索引。PostgreSQL 将在适当的情况下使用两者。
CREATE INDEX ON table1 (account);
CREATE INDEX ON table1 USING GIN (json);
Run Code Online (Sandbox Code Playgroud)
或者您可以使用btree_gin.
btree_gin 提供示例 GIN 运算符类,这些类实现了数据类型 int2、int4、int8、float4、float8、带时区的时间戳、不带时区的时间戳、带时区的时间、不带时区的时间、日期、间隔的 B 树等效行为, oid, money, "char", varchar, text, bytea, bit, varbit, macaddr, inet, and cidr。
看起来像这样,
CREATE EXTENSION btree_gin;
CREATE INDEX ON table1 USING gin (account,json);
Run Code Online (Sandbox Code Playgroud)
在正常情况下,我可能会使用两个索引。