首页 > 数据库 >postgresql jsonb

postgresql jsonb

时间:2022-12-22 10:23:18浏览次数:43  
标签:postgresql jsonb ops 索引 json params tbl

准备测试数据

DROP TABLE IF EXISTS "tbl";
create table tbl(id int, js jsonb);  
create index idx_tbl_1 on tbl using gin (js jsonb_path_ops);  
create index idx_tbl_2 on tbl using gin (js); 
ALTER TABLE "public"."tbl" ADD CONSTRAINT "tbl_pkey" PRIMARY KEY ("id");

INSERT INTO "tbl" VALUES (1, '{"sex": "男", "city": "贡嘎", "ogran": "贡嘎中心医院", "office": "内科"}');
INSERT INTO "tbl" VALUES (2, '{"sex": "男", "city": "玉树", "ogran": "玉树中心医院", "office": "内科"}');
INSERT INTO "tbl" VALUES (3, '{"sex": "男", "city": "西藏", "ogran": "西藏中心医院", "office": "内科"}');
INSERT INTO "tbl" VALUES (4, '{"sex": "男", "city": "西藏", "ogran": "人民中心医院", "office": "内科"}');
INSERT INTO "tbl" VALUES (5, '{"sex": "男", "city": "西藏", "ogran": "林芝中心医院", "office": "内科"}');
INSERT INTO "tbl" ("id", "js") VALUES (6, '{"items": {"qty": 1, "product": "Toy Car"}, "customer": "Josh William"}');

explain select * from tbl where js  @> '{"city": "西藏"}';
PostGreSql支持json类型的字段和jsonb类型的字段 json和jsonb的区别
  • json类型把输入的数据原封不动的存放到数据库中。jsonb类型在存放时把JSON解析成二进制格式,读出再解析
  • jsonb写入比 json 慢,但检索较 json快
  • json存储占内存比jsonb小
一、json类型 1.添加json类型的字段
alter table t_message add params_json json;
2.创建索引 方法一:
create index idx_params_id on t_message using btree (json_extract_path_text(params_json, 'id'));
方法二:
create index idx_params_name on t_message using btree ((params_josn->>'name'));
3.执行 方法一走索引的查询
select * from test where json_extract_path_text( params_json,'id')='123';
方法二走索引的查询
select * from test where params_json->>'name'='123';
二、jsonb类型 josnb字段创建 GIN 索引, GIN 索引有两种模式, 默认模式支持  @>, ?, ?& 和 ?| 的索引查询
  • 使用默认的jsonb_ops操作符创建。
  • 使用jsonb_path_opsselect * from test where params_json->>'name'='123';
操作符创建。   两者的区别是:
  •   jsonb_ops的GIN索引中,JSONB数据中的每个key和value都是作为一个单独的索引项。
  •   jsonb_path_ops只为每一个value创建一个索引项。
    例如,一个项{“foo”:{"bar":"baz"}},jsonb_ops会分别为“foo”,"bar","baz"创建索引项,共创建了三个。jsonb_path_ops则是把“foo”,"bar","baz"组合成一个hash值作为索引项。因此,通常jsonb_path_ops的索引较小。  我们这里使用默认模式 1.添加jsonb类型
alter table t_message add params_jsonb jsonb;
  使用默认的jsonb_ops操作符创建索引。
create index idx_gin_params_jsonb on t_message using gin (params_jsonb);
  使用jsonb_path_ops操作符创建索引。
create index idx_gin_params_jsonb on t_message using gin(params_jsonb jsonb_path_ops);
3.执行 不走索引:
select * from t_message where params_jsonb->>'loanId' ='123';
走索引:
select * from t_message where params_jsonb @> '{"loanId": 123}';
走索引:
select * from t_message where params_jsonb->'loanId' ?'123'; 
结果:没走 ? 操作没走索引, 但 ? 操作支持索引检索,创建以下索引.
create index idx_gin_params_jsonb_loaId on t_message using gin((params_jsonb -> 'loanId'));

  


标签:postgresql,jsonb,ops,索引,json,params,tbl
From: https://www.cnblogs.com/liuxiaoji/p/16997773.html

相关文章

  • postgresql jsonb + 索引 + 分区 + 测试
    --创建表DROPTABLEIFEXISTStba;CREATETABLEtba(idserial,peaktempint,jsjsonb,logdatedatenotnull)PARTITIONBYRANGE(logdate); --设置主键......
  • Powerdesigner反向Postgresql14常见问题
    一、数据库连接powerdesigner需要安装32位jdk,同时到Tool->GeneralOptions中设置32位jdk的路径  配置数据库连接:选择菜单Database->UpdateModelFromDataba......
  • 德哥PostgreSQL学习资料汇总(转)
    德哥介绍:周正中,网名德哥(digoal),目前就职于阿里云。PostgreSQL中国社区发起人之一,PostgreSQL象牙塔发起人之一,DBA+社群联合发起人之一,DBGeeK社区专家组成员。德哥:看完......
  • springboot+postgresql集成anyline试水
    anyline是什么简单讲就是一个工具可以让你抛开常规的机械性建mapper、dao、sql,用通用的语句查询和操作数据库表。目前也在初步探索中,感受还不深。官网文档:http://doc.any......
  • postgreSQL常用命令
     --建表案例CREATETABLEgas_use_test(idserialPRIMARYKEYNOTNULL,tenantIdintegerNOTNULL,meterNovarchar(255)uniqueNOTNULL,......
  • 从Mariadb迁移到postgresql
    前言本文主要实验所用的环境,后端是golang,ginweb框架.mysqldriver:github.com/go-sql-driver/mysqlv1.4.1迁移后PostgreSQLdriver:github.com/jackc/pgx/v5v......
  • postgresql数据库插入和读取图片
    postgresql插入和读取图片postgresql存储图片需要二进制类型bytea,创建一张测试表:postgres=#createtabletest_image(imgbytea);CREATETABLE使用jdbc插入1.jpgtry......
  • PostgreSQL 常用操作记录
    常用命令行命令1,连接数据库#需要输入密码psql-hhost-Udbuser-ddbname免密登录方法:方法一:设定环境变量PGPASSWORD​方法二:配置.pgpass​touch~/......
  • postgresql 9.4引入的with ordinaly在lightdb中使用rownum替换
    pg9.4为表函数引入了自动生成行号的功能,如下:=#SELECT*FROMgenerate_series(4,1,-1)WITHORDINALITY;generate_series|ordinality-----------------+--------......
  • postgresql 时间格式 获取年初年末年末 ,月初月末日期,本日,去年等日期
      --获取本天yyyy-MM-dd/2022-12-13selectcurrent_date;--获取本天date/2022-12-13selectnow();--获取本天/13selectdate_part('day',curre......