一、缘起

慢sql分析,总行数80w+。

比较特殊的是:其中有个字段info是jsonb类型,写法:info::json->'length' as length

同样的查询条件查这个字段和不查这个字段相差3.3倍

那看来就是json取值拖垮了查询的性能。

取jsonb中的字段有多种取法(如下), 那他们有什么区别呢,对性能有啥影响呢?

  • info::json->'length'
  • info::jsonb->'length'
  • info::json->>'length'
  • info::jsonb->>'length'
  • info->'length'
  • info->'length'
  • info->>'length'
  • info->>'length'

二、对比

2.1 输出类型对比

查询不同写法的类型:

select
info::json->'length' AS "info::json->", pg_typeof(info::json->'length' ) ,
info::jsonb->'length' AS "info::jsonb->" , pg_typeof(info::jsonb->'length' ),
info::json->>'length' AS "info::json->>" , pg_typeof(info::json->>'length' ),
info::jsonb->>'length' AS "info::jsonb->>" , pg_typeof(info::jsonb->>'length'),
info->'length' AS "info->" , pg_typeof(info->'length' ),
info->'length' AS "info->" , pg_typeof(info->'length' ),
info->>'length' AS "info->>" , pg_typeof(info->>'length' ),
info->>'length' AS "info->>" , pg_typeof(info->>'length' )
from t_test_json limit 1;

结果

 info::json-> | pg_typeof | info::jsonb-> | pg_typeof | info::json->> | pg_typeof | info::jsonb->> | pg_typeof | info-> | pg_typeof | info-> | pg_typeof | info->> | pg_typeof | info->> | pg_typeof
--------------+-----------+---------------+-----------+---------------+-----------+----------------+-----------+--------+-----------+--------+-----------+---------+-----------+---------+-----------
123.9 | json | 123.9 | jsonb | 123.9 | text | 123.9 | text | 123.9 | jsonb | 123.9 | jsonb | 123.9 | text | 123.9 | textttui 

分析小结

  • ->> 输出类型为text
  • ->输出到底为何得看调用它的数据类型,比如:info类型是jsonb, 那么info->'length'为jsonb类型
  • ::json、::jsonb起到类型转换的作用。
  • info本来就是jsonb类型,info::jsonb算无效转换,是否对性能有影响,待会验证

2.2 性能对比

jihite=> EXPLAIN ANALYSE
jihite-> select
jihite-> info::json->'length' AS "info::json->", pg_typeof(info::json->'length' )
jihite-> from t_test_json limit 1;
QUERY PLAN
---------------------------------------------------------------------------------------------------------------
Limit (cost=0.00..0.04 rows=1 width=36) (actual time=0.028..0.028 rows=1 loops=1)
-> Seq Scan on t_test_json (cost=0.00..30.62 rows=750 width=36) (actual time=0.027..0.027 rows=1 loops=1)
Planning time: 0.056 ms
Execution time: 0.047 ms
(4 rows) jihite=> EXPLAIN ANALYSE
jihite-> select
jihite-> info::jsonb->'length' AS "info::jsonb->" , pg_typeof(info::jsonb->'length' )
jihite-> from t_test_json limit 1
jihite-> ;
QUERY PLAN
---------------------------------------------------------------------------------------------------------------
Limit (cost=0.00..0.03 rows=1 width=36) (actual time=0.017..0.017 rows=1 loops=1)
-> Seq Scan on t_test_json (cost=0.00..23.12 rows=750 width=36) (actual time=0.015..0.015 rows=1 loops=1)
Planning time: 0.053 ms
Execution time: 0.031 ms
(4 rows) jihite=> EXPLAIN ANALYSE
jihite-> select
jihite-> info::jsonb->'length' AS "info::jsonb->" , pg_typeof(info::jsonb->'length' )
jihite-> from t_test_json limit 1;
QUERY PLAN
---------------------------------------------------------------------------------------------------------------
Limit (cost=0.00..0.03 rows=1 width=36) (actual time=0.010..0.010 rows=1 loops=1)
-> Seq Scan on t_test_json (cost=0.00..23.12 rows=750 width=36) (actual time=0.009..0.009 rows=1 loops=1)
Planning time: 0.037 ms
Execution time: 0.022 ms
(4 rows) jihite=>
jihite=> EXPLAIN ANALYSE
jihite-> select
jihite-> info::json->>'length' AS "info::json->>" , pg_typeof(info::json->>'length' )
jihite-> from t_test_json limit 1;
QUERY PLAN
---------------------------------------------------------------------------------------------------------------
Limit (cost=0.00..0.04 rows=1 width=36) (actual time=0.026..0.027 rows=1 loops=1)
-> Seq Scan on t_test_json (cost=0.00..30.62 rows=750 width=36) (actual time=0.025..0.025 rows=1 loops=1)
Planning time: 0.056 ms
Execution time: 0.046 ms
(4 rows) jihite=>
jihite=> EXPLAIN ANALYSE
jihite-> select
jihite-> info::jsonb->>'length' AS "info::jsonb->>" , pg_typeof(info::jsonb->>'length')
jihite-> from t_test_json limit 1;
QUERY PLAN
---------------------------------------------------------------------------------------------------------------
Limit (cost=0.00..0.03 rows=1 width=36) (actual time=0.012..0.012 rows=1 loops=1)
-> Seq Scan on t_test_json (cost=0.00..23.12 rows=750 width=36) (actual time=0.011..0.011 rows=1 loops=1)
Planning time: 0.053 ms
Execution time: 0.029 ms
(4 rows) jihite=>
jihite=> EXPLAIN ANALYSE
jihite-> select
jihite-> info->'length' AS "info->" , pg_typeof(info->'length' )
jihite-> from t_test_json limit 1;
QUERY PLAN
---------------------------------------------------------------------------------------------------------------
Limit (cost=0.00..0.03 rows=1 width=36) (actual time=0.014..0.014 rows=1 loops=1)
-> Seq Scan on t_test_json (cost=0.00..23.12 rows=750 width=36) (actual time=0.013..0.013 rows=1 loops=1)
Planning time: 0.052 ms
Execution time: 0.030 ms
(4 rows) jihite=>
jihite=> EXPLAIN ANALYSE
jihite-> select
jihite-> info->'length' AS "info->" , pg_typeof(info->'length' )
jihite-> from t_test_json limit 1;
QUERY PLAN
---------------------------------------------------------------------------------------------------------------
Limit (cost=0.00..0.03 rows=1 width=36) (actual time=0.013..0.013 rows=1 loops=1)
-> Seq Scan on t_test_json (cost=0.00..23.12 rows=750 width=36) (actual time=0.012..0.012 rows=1 loops=1)
Planning time: 0.051 ms
Execution time: 0.029 ms
(4 rows) jihite=>
jihite=> EXPLAIN ANALYSE
jihite-> select
jihite-> info->>'length' AS "info->>" , pg_typeof(info->>'length' )
jihite-> from t_test_json limit 1;
QUERY PLAN
---------------------------------------------------------------------------------------------------------------
Limit (cost=0.00..0.03 rows=1 width=36) (actual time=0.012..0.013 rows=1 loops=1)
-> Seq Scan on t_test_json (cost=0.00..23.12 rows=750 width=36) (actual time=0.011..0.011 rows=1 loops=1)
Planning time: 0.053 ms
Execution time: 0.030 ms
(4 rows) jihite=>
jihite=> EXPLAIN ANALYSE
jihite-> select
jihite-> info->>'length' AS "info->>" , pg_typeof(info->>'length' )
jihite-> from t_test_json limit 1;
QUERY PLAN
---------------------------------------------------------------------------------------------------------------
Limit (cost=0.00..0.03 rows=1 width=36) (actual time=0.012..0.013 rows=1 loops=1)
-> Seq Scan on t_test_json (cost=0.00..23.12 rows=750 width=36) (actual time=0.011..0.011 rows=1 loops=1)
Planning time: 0.053 ms
Execution time: 0.029 ms
(4 rows)

从执行耗时(Execution time)分析小结

执行了类型转换 jsonb->json,转换性能(0.46ms)显然低出不转换(0.3ms)

三、优化

把查询字段:info::json->'length' 改为info->>'length',减少类型转换导致性能的损耗。

四、待调查

4.1 同类型转换是否影响性能

字段本身是jsonb, 进行强转::jsonb 是否对性能造成影响,还是在执行预编译时就已被优化

从大量数据的压测看,转换会对性能有影响,但是不大

4.2 如何分析函数的耗时

在explain analyze时,主要分析了索引对性能的影响,那函数的具体影响如何查看呢?

五、附

5.1 json、jsonb区别

  • jsonb 性能优于json
  • jsonb 支持索引
  • 【最大差异:效率】jsonb 写入时会处理写入数据,写入相对较慢,json会保留原始数据(包括无用的空格)

推荐把JSON 数据存储为jsonb

5.2 postgresql查看字段类型函数

pg_typeof()

5.3 性能分析指令

如果您有一条执行很慢的 SQL 语句,您想知道发生了什么以及如何优化它。
EXPLAIN ANALYSE 能够获取数据库执行 sql 语句,所经历的过程,以及耗费的时间,可以协助优化性能。

关键参数:

Execution time: *** ms 表明了实际的SQL 执行时间,其中不包括查询计划的生成时间

5.4 示例中的建表语句

# 建表语句

create table t_test_json
(
id bigserial not null PRIMARY KEY,
task character varying not null,
info jsonb not null,
create_time timestamp not null default current_timestamp
);

# 压测数据

insert into t_test_json(task, info) values('1', '{"length": 123.9, "avatar": "avatar_url", "tags": ["python", "golang", "db"]}');
insert into t_test_json(task, info) values('2', '{"length": 123.9, "avatar": "avatar_url", "tags": ["python", "golang", "db"]}');
insert into t_test_json(task, info) values('3', '{"length": 123.9, "avatar": "avatar_url", "tags": ["python", "golang", "db"]}');
insert into t_test_json(task, info) values('4', '{"length": 123.9, "avatar": "avatar_url", "tags": ["python", "golang", "db"]}');
insert into t_test_json(task, info) values('5', '{"length": 123.9, "avatar": "avatar_url", "tags": ["python", "golang", "db"]}');
insert into t_test_json(task, info) values('6', '{"length": 123.9, "avatar": "avatar_url", "tags": ["python", "golang", "db"]}');
insert into t_test_json(task, info) values('7', '{"length": 123.9, "avatar": "avatar_url", "tags": ["python", "golang", "db"]}');
insert into t_test_json(task, info) values('8', '{"length": 123.9, "avatar": "avatar_url", "tags": ["python", "golang", "db"]}');
insert into t_test_json(task, info) values('9', '{"length": 123.9, "avatar": "avatar_url", "tags": ["python", "golang", "db"]}');
insert into t_test_json(task, info) values('10', '{"length": 123.9, "avatar": "avatar_url", "tags": ["python", "golang", "db"]}');
insert into t_test_json(task, info) values('11', '{"length": 123.9, "avatar": "avatar_url", "tags": ["python", "golang", "db"]}');
insert into t_test_json(task, info) values('12', '{"length": 123.9, "avatar": "avatar_url", "tags": ["python", "golang", "db"]}');
insert into t_test_json(task, info) values('13', '{"length": 123.9, "avatar": "avatar_url", "tags": ["python", "golang", "db"]}');
insert into t_test_json(task, info) values('14', '{"length": 123.9, "avatar": "avatar_url", "tags": ["python", "golang", "db"]}');
insert into t_test_json(task, info) values('15', '{"length": 123.9, "avatar": "avatar_url", "tags": ["python", "golang", "db"]}');
insert into t_test_json(task, info) values('16', '{"length": 123.9, "avatar": "avatar_url", "tags": ["python", "golang", "db"]}');
insert into t_test_json(task, info) values('17', '{"length": 123.9, "avatar": "avatar_url", "tags": ["python", "golang", "db"]}');
insert into t_test_json(task, info) values('18', '{"length": 123.9, "avatar": "avatar_url", "tags": ["python", "golang", "db"]}');
insert into t_test_json(task, info) values('19', '{"length": 123.9, "avatar": "avatar_url", "tags": ["python", "golang", "db"]}');
insert into t_test_json(task, info) values('20', '{"length": 123.9, "avatar": "avatar_url", "tags": ["python", "golang", "db"]}');

5.5 示例中的压测脚本

import time
import psycopg dbname, user, pwd, ip, port = '', '', '', '', '5432'
connection = "dbname=%s user=%s password=%s host=%s port=%s" % (dbname, user, pwd, ip, port)
db = psycopg.connect(connection)
cur = db.cursor() ss = 0
lens = 20
for i in range(lens):
s = time.time()
sql = ''' select
task.id,
act.payload::json->'prod_type' as prod_type
from
t_test_json
order by id
offset %s limit 1000 ''' % (i * 1000)
#print("sql:", sql)
cur.execute(sql)
rev = cur.fetchall() e = time.time()
print("scan:", i, e - s)
ss += (e - s) print('avg', ss / lens)

postgresql json取值为何这么慢?的更多相关文章

  1. 选中没有选中的复选框,匹配含有某个字符串的正则,json取值的两种方法,把变量定义在外面跟里面的区别

    一.筛选没有选中的复选框:not("input:checked") 二.匹配有VARCHAR的字符串:".*VARCHAR.*?" 三.json取值的两种方法 ...

  2. JSON取值(key是中文或者数字)方式详解

    JSON取值(key是中文或者数字)方式详解 先准备一个json对象用于演示 var json = {'name':'zhangsan', '年龄':23, 404:'你可能迷路了'}; 使用JS中w ...

  3. 闲扯json取值,联想map取值。

    将list转json(list中的Bean的属性名称为变量,若为常量没必要采用此方式,直接转实体类即可) JSONArray json = JSONArray.fromObject(list); fo ...

  4. JSON取值前判断

    public static void main(String[] args)throws Exception{ String jsonStr1="{\"access_token\& ...

  5. javascript中json对象json数组json字符串互转及取值

    今天用到了json数组和json对象和json类型字符串之间互转及取值,记录一下: 1.json类型的字符串转换为json对象及取值 var jsonString = '{"bar" ...

  6. ZT: C#不建类直接Json解析与取值

    C#不建类直接Json解析与取值 2017年10月19日 15:58:22 圆圆娃哈哈 阅读数:701    版权声明:本文为博主原创文章,未经博主允许不得转载. https://blog.csdn. ...

  7. 【js jQuery】map集合 循环迭代取值---以及 map、json对象、list、array循环迭代的方法和区别

    后台给前台传来一个map @ResponseBody @RequestMapping(value = "getSys") public Map<Long,String> ...

  8. SNF快速开发平台MVC-EasyUI3.9之-WebApi和MVC-controller层接收的json字符串的取值方法和调用后台服务方法

    最近项目组很多人问我,从前台页面传到后台controller控制层或者WebApi 时如何取值和运算操作. 今天就都大家一个在框架内一个取值技巧 前台JS调用代码: 1.下面是选中一行数据后右键点击时 ...

  9. 实用ExtJS教程100例-011:ExtJS Form 使用JSON数据赋值和取值

    上一节中我们演示了ExtJS Form的异步加载和提交数据,本节中我们将演示如何使用JSON数据为ExtJS Form中的字段赋值和取值. 系列ExtJS教程持续更新中,点击查看>>最新E ...

  10. C# 后台解析json,简单方法 字符串序列化为对象,取值

    如果后台是一个JSON的字符串格式如下: string str = "{\"Success\":true,\"Msg\":\"成功!\&qu ...

随机推荐

  1. 超详细!新手如何创建一个Vue项目

    目录 一.在官网下载Vue.js 二.使用<script>标签直接引入本地的vue.js 三.使用CDN引入Vue.js 四.验证是否安装成功 五.安装Vue Devtools浏览器调试插 ...

  2. 部署lnmp环境,安装typecho博客

    安装nginx和PHP环境 root@cby:~# apt install nginx php7.4 php7.4-mysql php7.4-fpm 修改nginx配置文件 root@cby:~# v ...

  3. pyhon之编译成exe

    1安装pyinstaller pip install pyinstaller 2 编译 pyinstaller -F -w game.py  (-F表示打包单个文件,-w是为了打开exe时候不弹出黑框 ...

  4. [网络/Linux]处理安全报告/安全漏洞的一般流程与思路

    对近期工作中所经历的4次处理第三方网络安全公司的安全报告及其安全漏洞的经验做一点小结. 1 流程 Stage1 阅读/整理/分类:安全漏洞报告的安全漏洞 (目的:快速了解漏洞规模和分布) Stage2 ...

  5. [大数据]ETL之增量数据抽取(CDC)

    关于:转载/知识产权 本文遵循 GPL开源协议,如若转载: 1 请发邮件至博主,以作申请声明. 2 请于引用文章的显著处注明来源([大数据]ETL之增量数据抽取(CDC) - https://www. ...

  6. Yum安装svn及配置

    svn配置 1.安装svn服务器端 yum install subversion 从镜像下载安装svn服务器端 cd /usr/local/ //进入目录,准备创建svn目录 mkdir svn // ...

  7. Docker容器核心实践(操作容器)

    镜像和容器是docker中最基础的概念,镜像可以理解为包含应用程序以及其相关依赖的一个基础文件系统,在其启动过程中,以只读的方式被用于创建容器的运行环境,本质上是基于UnionFS文件系统的一组镜像层 ...

  8. Springboot3整合使用ja-captcha行为验证码解决方案

    截止到目前,Springboot最新稳定版本已经迭代到3.0.5,而我们项目中使用的行为验证码框架ja-captcha还没有适配Springboot3,码云上类似的请求也没有得到过回应,于是决定自己动 ...

  9. uniapp directive 在原生 wgt 包不生效 uniapp directive 不生效

    需求 根据权限编码禁用按钮 阻止当前 dom 绑定的点击事件,禁用状态(opacity 半透明?? 或者 display: none?? ) 尝试 开发环境用 Chrome 跑,一切正常,构建打包后去 ...

  10. 前端获取不到环境变量NODE_ENV

    有时候我们期望通过执行不同的 npm script 来区分诸如 dev.prod.uat.sit等多环境下使用的不同变量 今天我也在整环境变量,碰到一个小小的bug.装了 cross-env 但还是没 ...