加入收藏 | 设为首页 | 会员中心 | 我要投稿 淮安站长网 (https://www.0517zz.cn/)- 运营、云管理、经验、智能边缘、云硬盘!
当前位置: 首页 > 建站 > 正文

PostgreSQL何以支持丰富的NoSQL特性?

发布时间:2018-09-30 09:06:53 所属栏目:建站 来源:DBAplus社群
导读:副标题#e# 【新品产上线啦】51CTO播客,随时随地,碎片化学习 作者介绍 谭峰,网名francs,中国开源软件推进联盟PostgreSQL分会特聘专家,《PostgreSQL实战》作者之一,《PostgreSQL 9 Administration Cookbook》译者之一。现就职于浙江移动负责应用上云架

查询tbl_user_json表的一条测试数据,如下:

  1. mydb=> SELECT * FROM tbl_user_json LIMIT 1;  
  2. id | user_info  
  3. ---------+------------------------------------------------------------------------------------  
  4. 2000001 | {"id":1,"user_id":1182883,"user_name":"1_francs","create_time":"2017-08-03T20:59:27.42741+08:00"}  
  5. (1 row) 

3、JSON与JSONB表读性能测试

对于json、jsonb读性能测试我们选择基于json、jsonb键值查询的场景,例如,根据user_info字段的user_name键的值查询,如下所示:

  1. mydb=> EXPLAIN ANALYZE SELECT * FROM tbl_user_jsonb WHERE user_info->>'user_name'='1_francs';  
  2. QUERY PLAN  
  3. -------------------------------------------------------------------------------------  
  4. Seq Scan on tbl_user_jsonb (cost=0.00..72859.90 rows=10042 width=143) (actual time=0.023..524.843 rows=1 loops=1)  
  5. Filter: ((user_info ->> 'user_name'::text) = '1_francs'::text)  
  6. Rows Removed by Filter: 1999999  
  7. Planning time: 0.091 ms  
  8. Execution time: 524.876 ms  
  9. (5 rows) 

上述SQL执行时间为524毫秒左右,基于user_info字段的user_name键值创建btree索引如下:

  1. mydb=> CREATE INDEX idx_jsonb ON tbl_user_jsonb USING btree ((user_info->>'user_name')); 

再次执行上述查询,如下所示:

  1. Bitmap Heap Scan on tbl_user_jsonb (cost=155.93..14113.93 rows=10000 width=143) (actual time=0.027..0.027 rows=1 loops=1)  
  2. Recheck Cond: ((user_info ->> 'user_name'::text) = '1_francs'::text)  
  3. Heap Blocks: exact=1  
  4. -> Bitmap Index Scan on idx_jsonb (cost=0.00..153.43 rows=10000 width=0) (actual time=0.021..0.021 rows=1 loops=1)  
  5. Index Cond: ((user_info ->> 'user_name'::text) = '1_francs'::text)  
  6. Planning time: 0.091 ms  
  7. Execution time: 0.060 ms  
  8. (7 rows) 

根据上述执行计划看出走了索引,并且SQL时间下降到0.060ms。为更好地对比tbl_user_json、tbl_user_jsonb表基于键值查询的效率,计划根据user_info字段id键进行范围扫描对比性能,创建索引如下:

  1. mydb=> CREATE INDEX idx_gin_user_info_id ON tbl_user_json USING btree  
  2. (((user_info ->> 'id')::integer));  
  3. CREATE INDEX  
  4. mydb=> CREATE INDEX idx_gin_user_infob_id ON tbl_user_jsonb USING btree 

(编辑:淮安站长网)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

推荐文章
    热点阅读