我的 postgres 数据库中有以下数据结构 - 一个名为的 jsonb 列customer
{
"RequestId": "00000000-0000-0000-0000-000000000000",
"Customer": {
"Status": "A",
"AccountId": 14603582,
"ProfileId": 172,
"ReferralTypeId": 15
}
"Contact": {
"Telephone": "",
"Email": ""
}
}
我想在上面创建一个索引ProfileId
字段,它是一个整数。
我一直无法找到如何在嵌套字段上创建索引的示例。
我正在执行的查询(大约需要 300 秒)是:
select id, customer from where customer @> '{"Customer":{"ProfileId": 172}}'
运算符类jsonb_path_ops
and jsonb_ops
对于 GIN 索引支持@>操作员 https://www.postgresql.org/docs/current/static/gin-builtin-opclasses.html#GIN-BUILTIN-OPCLASSES-TABLE.
所以你的查询应该能够使用以下索引
create index on the_table using gin (customer);
它使用默认的jsonb_ops
操作员。
根据手册jsonb_path_ops
运算符速度更快,但仅支持@>
操作员。因此,如果这是您拥有的唯一条件类型(对于该列),请使用jsonb_path_ops
可能会更有效:
create index on the_table using gin (customer jsonb_path_ops);
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)