jsonb

Selecting from JSONB field slow

被刻印的时光 ゝ 提交于 2020-06-16 07:25:52
问题 I have a relatively small table (~50k rows). When I select all records, it takes a ~40s. The table has 3 JSONB columns. When I select every column except for the JSONBs, the query takes ~700ms. If I add in just one of the JSONB fields, the query time jumps to nearly 10s. I'm never using a where clause referencing something inside the JSONB, just selecting *. Even so, I tried adding GIN indexes because I saw them frequently mentioned as a performance booster for JSONB. I've ran a full vacuum.

How can I do less than, greater than in JSON Postgres fields?

Deadly 提交于 2020-05-26 03:49:39
问题 If I have some json: id = 1, json = {'key':95} id = 2, json = {'key':90} id = 3, json = {'key':50} Is there a way I can use Postgres fields to query for key greater than >= 90? 回答1: Use the operator ->> (Get JSON object field as text), e.g. with my_table(id, json) as ( values (1, '{"key":95}'::json), (2, '{"key":90}'), (3, '{"key":50}') ) select * from my_table where (json->>'key')::int >= 90; id | json ----+------------ 1 | {"key":95} 2 | {"key":90} (2 rows) 回答2: If you use postgres version

How to convert a XML into JSONB within Postgres

谁说胖子不能爱 提交于 2020-05-24 04:03:12
问题 I have a table, Table1 that contains a XML data type column, ColumnA and another JSONB column ColumnB How do I convert the data from ColumnA into ColumnB , presumably using an SQL UPDATE statement. Is there a built-in function in Postgres that does this? 回答1: There is no out-of-box functions as I know. Try: create or replace function xml_to_json(p_xml xml) returns jsonb as $$ declare result json; root text; childs jsonb; attr jsonb; txt text; begin -- Get root element name select (xpath('name

Django - postgres: How to create an index on a JsonB field

霸气de小男生 提交于 2020-05-13 08:59:08
问题 I want to allow indexing on JsonB field on an ID which is a few levels deep into the json data in our Django project. Here's what the JSONB data looks like: "foreign_data":{ "some_key": val "src_data": { "VEHICLE": { "title": "615", "is_working": true, "upc": "85121212121", "dealer_name": "CryptoDealer", "id": 1222551 } } } I want to index on the field id using Django views but not sure how to achieve that. Happy to post my Django ViewSet if it helps. 回答1: t=# create table d(i bigserial, j

Django - postgres: How to create an index on a JsonB field

倾然丶 夕夏残阳落幕 提交于 2020-05-13 08:56:29
问题 I want to allow indexing on JsonB field on an ID which is a few levels deep into the json data in our Django project. Here's what the JSONB data looks like: "foreign_data":{ "some_key": val "src_data": { "VEHICLE": { "title": "615", "is_working": true, "upc": "85121212121", "dealer_name": "CryptoDealer", "id": 1222551 } } } I want to index on the field id using Django views but not sure how to achieve that. Happy to post my Django ViewSet if it helps. 回答1: t=# create table d(i bigserial, j

Django - postgres: How to create an index on a JsonB field

百般思念 提交于 2020-05-13 08:56:13
问题 I want to allow indexing on JsonB field on an ID which is a few levels deep into the json data in our Django project. Here's what the JSONB data looks like: "foreign_data":{ "some_key": val "src_data": { "VEHICLE": { "title": "615", "is_working": true, "upc": "85121212121", "dealer_name": "CryptoDealer", "id": 1222551 } } } I want to index on the field id using Django views but not sure how to achieve that. Happy to post my Django ViewSet if it helps. 回答1: t=# create table d(i bigserial, j

MySQL与PostgreSQL相比哪个更好?

≯℡__Kan透↙ 提交于 2020-04-28 02:03:18
网上已经有很多拿PostgreSQL与MySQL比较的文章了,这篇文章只是对一些重要的信息进行下梳理。在开始分析前,先来看下这两张图: MySQL MySQL声称自己是最流行的开源数据库。LAMP中的M指的就是MySQL。构建在LAMP上的应用都会使用MySQL,如WordPress、Drupal等大多数php开源程序。MySQL最初是由MySQL AB开发的,然后在2008年以10亿美金的价格卖给了Sun公司,Sun公司又在2010年被Oracle收购。Oracle支持MySQL的多个版本:Standard、Enterprise、Classic、Cluster、Embedded与Community。其中有一些是免费下载的,另外一些则是收费的。其核心代码基于GPL许可,由于MySQL被控制在Oracle,社区担心会对MySQL的开源会有影响,所以开发了一些分支,比如: MariaDB和Percona。 PostgreSQL PostgreSQL标榜自己是世界上最先进的开源数据库。PostgreSQL的一些粉丝说它能与Oracle相媲美,而且没有那么昂贵的价格和傲慢的客服。最初是1985年在加利福尼亚大学伯克利分校开发的,作为Ingres数据库的后继。PostgreSQL是完全由社区驱动的开源项目。它提供了单个完整功能的版本,而不像MySQL那样提供了多个不同的社区版、商业版与企业版

PostgreSQL各数据类型的内置函数

怎甘沉沦 提交于 2020-04-21 07:01:06
参考《PostgreSQL实战》 3.1.2 数字类型操作符和数学函数 PostgreSQL 支持数字类型操作符和丰富的数学函数 例如支持 加、减、乘、除、模取取余 操作符 SELECT 1+2, 2*3, 4/2, 8%3; 按模取余 SELECT mod(8,3); 结果:2 四舍五入 函数: SELECT round(10.4) , round(10.5); 结果:10, 11 返回 大于或等于 给出参数的 最小整数 SELECT ceil(3.6) , ceil(-3.6); 结果:4, -3 返回 小于或等于 给出参数的 最小整数 floor(3.6) 结果:3 3.2.2 字符类型函数 PostgreSQL 支持丰富 的字符函数, 下面举例说明 计算字符串中的 字符数 select char_length('abcd'); 结果: 4 计算字符串占用的 字节数 select octet_length('abcd'); 结果: 4 指定字符在字符串中的 位置 (首次出现) select position('a' in 'abcda') 结果: 1 select position('x' in 'abcda') 结果: 0 提取字符串中的 子串 select substring('hello' from 3 for 4) 结果: llo select substring(

Postgres JSONB date condition true for all entries of an array

自作多情 提交于 2020-03-25 21:54:31
问题 I have a JSONB that looks something like this [{ "foo":"bar", "date":"2020-01-01" }, { "foo":"bar", "date":"2020-02-03" }, { "foo":"bar", "date":"2020-01-02" }] I need a query to return true if ALL of the "date"s are less than 1 year ago. I have looked at the postgres JBON documentation and the only thing sort of fitting I found was using ?& but I'm not just trying to compare strings but dates that are strings so I am kind of lost here 回答1: You will need to iterate over all elements and then

Add objects inside NESTED JSONB arrays with PostgreSQL

杀马特。学长 韩版系。学妹 提交于 2020-03-25 12:35:15
问题 Json Request INSERT INTO test.demotbl (data) VALUES ('{ "x1": "Americas", "x2": "West", "x3": [{ "x_id": "sam" }], "x4": { "a1": true, "a2": false, "a3": [ "xx", "xx" ], "a4": [ "Josh" ], "y1": [{ "id": "RW", "z2": true, "z3": "USER" }, { "id": "RO", "z2": false, "z3": "SELECT" } ] } }'::jsonb) I want to update a new filed z4 based on id condition "id": "RO".Eample "z4": [{ "name": "john" }, { "name": "Steve" } expected output : { "x1": "Americas", "x2": "West", "x3": [{ "x_id": "sam" }], "x4