栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

检查Postgres JSON数组是否包含字符串

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

检查Postgres JSON数组是否包含字符串

从PostgreSQL
9.4开始,您可以使用

?
operator:

select info->>'name' from rabbits where (info->'food')::jsonb ? 'carrots';

如果改用 jsonb 类型,甚至可以

?
"food"
键上为查询建立索引: __

alter table rabbits alter info type jsonb using info::jsonb;create index on rabbits using gin ((info->'food'));select info->>'name' from rabbits where info->'food' ? 'carrots';

当然,作为专职兔子饲养员,您可能没有时间这样做。

更新: 这是在一张1,000,000只兔子的桌子上性能改进的演示,其中每只兔子喜欢两种食物,其中10%像胡萝卜:

d=# -- Postgres 9.3 solutiond=# explain analyze select info->>'name' from rabbits where exists (d(# select 1 from json_array_elements(info->'food') as foodd(#   where food::text = '"carrots"'d(# ); Execution time: 3084.927 msd=# -- Postgres 9.4+ solutiond=# explain analyze select info->'name' from rabbits where (info->'food')::jsonb ? 'carrots'; Execution time: 1255.501 msd=# alter table rabbits alter info type jsonb using info::jsonb;d=# explain analyze select info->'name' from rabbits where info->'food' ? 'carrots'; Execution time: 465.919 msd=# create index on rabbits using gin ((info->'food'));d=# explain analyze select info->'name' from rabbits where info->'food' ? 'carrots'; Execution time: 256.478 ms


转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/391231.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号