如何过滤一个包含多个元素的JSON阵列
这是示例表:
create table leadtime.test (
id serial primary key,
name jsonb
)
数据测试:
insert into leadtime.test (name)
values ('["abc", "def", "ghi"]');
我想检查名称是否包含此数组中的任何值[“ ABC”,“ 132”,“ 456”]'
我必须执行此代码:
select * from leadtime.test
where (name ? 'abc') or (name ? '132') or (name ? '456');
我被告知多个或
'ED过滤器或不是最佳性能。
有更好的方法吗?
This is the sample table:
create table leadtime.test (
id serial primary key,
name jsonb
)
Data test:
insert into leadtime.test (name)
values ('["abc", "def", "ghi"]');
I want to check if name contains any value in this array '["abc", "132", "456"]'
I have to do this code:
select * from leadtime.test
where (name ? 'abc') or (name ? '132') or (name ? '456');
I was told that multiple OR
'ed filters or not optimal for performance.
Is there a better way?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
将您的搜索术语数量传递为实际的postgres数组,并使用
|?
操作员:手册:
可以用普通的 gin Index on
> (名称)
也是。Pass your array of search terms as actual Postgres array and use the
|?
operator:The manual:
Can be supported with a plain GIN index on
(name)
, too.