您的位置:首页 > 数据库

关于 pgsql 数据库json几个函数用法的效率测试

2016-08-31 17:12 330 查看
关于 pgsql 数据库json几个函数用法的效率测试

关于pgsql 几个操作符的效率测试比较
1. json::->> 和 ->>

测试方法:单次运行100次,运行10个单次取平均时间。
测试结果:->> 效率高 5% 左右

功能差异:
json::->> 在使用前需要对对象转换为jsonb 然后再执行 ->> 操作,所以比->>更耗时 。
所以如果我们需要对返回的对象进行jsonb操作,用jsonb_* 相关函数时,
建议用jsonb_* 而不用 jsonb_*_text ,后者会把结果的jsonb对象转换为text,相对于会多两次 jsonb <--> text 转换操作。

2. any 和 in
select * from table where column in('1','3','5','7');
select * from table where column any('{1,3,5,7}'::text[]);

测试方法:单次运行100次,运行10个单次取平均时间。
测试结果:in 效率高 5% 左右

功能差异:
如果参数是自己传入的,那么建议用in(),如果参数是jsonb对象转换的可使用any。

3. 查询条件判断是否有值的时候,用 exists 和 判断结果 is null
select * from table1 a where exists (select 1 from jsonb_array_elements(a.value) as b where b->>'goods_id' = '?' ) ;
select * from table1 a where (select 1 from jsonb_array_elements(a.value) as b where b->>'goods_id' = '?' limit 1) is not null;

测试方法:单次运行100次,运行10个单次取平均时间。
测试结果:exists 效率高 8-10% 左右 。
功能差异:exists只比较不会操作数据,is not null 有操作数据的动作。

4. false 和 1 = 2
select * from table where false ;
建议使用false,。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: