在jsonb字段上添加索引 [英] add index on jsonb field

查看:555
本文介绍了在jsonb字段上添加索引的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要搜索Postgres中jsonb字段的数组键的值.

I need to search over the values of an array key of jsonb field in Postgres.

field: {'array_key' : [1, 2, 3, 4]}

是否可以在array_key上添加索引,或者是否有任何优化的方法来搜索值?

Is it possible to add index on array_key or is there any optimized method to search over the values ?

搜索查询将类似于

select * where field['array_key'].include?(2)

推荐答案

您可以在jsonb键上创建索引,

You can create index on jsonb keys as,

add_index :table_name, :field, :using => :gin, :expression => "(field->'array_key')", :name => 'index_table_name_on_field_array_keys'

然后,您可以搜索索引键为

Then, you can search over indexed keys as,

where("table_name.field->'array_keys' @> ?", Array(2))

这篇关于在jsonb字段上添加索引的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆