postgres - jsonb 字段具有任意 json - 索引正常吗?
postgres - jsonb field with arbitrary json - indexing ok?
我有一个 postgres table,我需要在其中存储任意 key/value 对,其中一些值可能是数组。我看到 postgres 支持 JSONB 和 Array 字段类型,并且正在考虑使用 JSONB,并且已经 reviewing the JSONB docs on indexing
我想了解的是,如果字段中存储的 json 模式越来越不同,是否可以有效地索引该字段?
例如,假设我有一个 table 来存储插件数据,并且每个用户都将允许这些插件存储任意数量的密钥。
如果
,我能否在此字段中有效地存储数据,并使其索引性能良好?
- 插件数量会增加,但可能会<100
- json 字段值大部分是简单的 json 对象,深一层
- 如果有帮助,我可以完全控制每个插件的 json 模式的外观。
plugin_data
key
value(JSONB)
plugin_type
userId
plugin.one
{id:1, test:'two'}
one
100
plugin.one
{id:32, test:'my title'}
one
102
plugin.two
{schedule:52, count:5, increment:2}
two
200
plugin.three
{duration:10s}
three
200
是的,you can index 具有 GIN 索引的列。这将加快搜索带有 ?
、?&
和 ?|
的键以及包含运算符 @>
以搜索是否包含“子对象”。
我有一个 postgres table,我需要在其中存储任意 key/value 对,其中一些值可能是数组。我看到 postgres 支持 JSONB 和 Array 字段类型,并且正在考虑使用 JSONB,并且已经 reviewing the JSONB docs on indexing
我想了解的是,如果字段中存储的 json 模式越来越不同,是否可以有效地索引该字段?
例如,假设我有一个 table 来存储插件数据,并且每个用户都将允许这些插件存储任意数量的密钥。
如果
,我能否在此字段中有效地存储数据,并使其索引性能良好?- 插件数量会增加,但可能会<100
- json 字段值大部分是简单的 json 对象,深一层
- 如果有帮助,我可以完全控制每个插件的 json 模式的外观。
plugin_data
key | value(JSONB) | plugin_type | userId |
---|---|---|---|
plugin.one | {id:1, test:'two'} | one | 100 |
plugin.one | {id:32, test:'my title'} | one | 102 |
plugin.two | {schedule:52, count:5, increment:2} | two | 200 |
plugin.three | {duration:10s} | three | 200 |
是的,you can index 具有 GIN 索引的列。这将加快搜索带有 ?
、?&
和 ?|
的键以及包含运算符 @>
以搜索是否包含“子对象”。