关于pgsql 几个操作符的效率测试比较

Posted mywebnumber

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了关于pgsql 几个操作符的效率测试比较相关的知识,希望对你有一定的参考价值。

关于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,对于cpu来说, 1=2 多数据存储的动作。

以上是关于关于pgsql 几个操作符的效率测试比较的主要内容,如果未能解决你的问题,请参考以下文章

关于pgsql 的json 和jsonb 的数据查询操作笔记整理

关于postgresql——常用操作指令

关于INSERT和LOAD效率的简单测试

JDK 8 Stream 数据流效率怎么样?

JDK 8 Stream 数据流效率怎么样?

分享几个好用到爆的 Python 模块,建议收藏!