PostgreSQL JSONB JAVA data type - inserts and joins

First I tried my hand in the JSONB data format (the discussion continued ( Attach tables using the value inside the JSONB column ), we recommend @Erwin, starting a new stream)

Two tables (obfuscated data and table names):

1. Discussion table { discussion_id int, contact_id, group_id, discussion_updates jsonb } [has around 600 thousand rows]
2. Authorization table {user_id varchar , auth_contacts jsonb, auth_groups jsonb} [has around 100 thousand rows]

auth_contacts jsonb data has key value pairs data (as example) 
- {"CC1": "rr", "CC2": "ro" }
auth_groups jsonb data has key value pairs data (as example)
- {"GRP1": "rr", "GRP2": "ro" }

1- Firstly, when inserting into the database via Java JDBC: I do this:

JSONObject authContacts = new JSONObject();

for(each record in data){
authContacts.put(contactKey, contactRight);
authGroups.put(groupKey, groupRight);
}

String insertSql = "INSERT INTO SSTA_AuthAll(employee_id, auth_contacts, auth_groups) VALUES(?,?::jsonb,?::jsonb)";
//---Connect to Db and prepare query
preparedStatement.setObject(2, authContacts.toJSONString());
preparedStatement.setObject(3, authGroups.toJSONString());
//INSERT into DB

Now toJSONString () takes time (up to 1 second sometimes - TIME FOR toJSON STRING LOOP: 17238ms), which is again inefficient. So again is this the right way to do this? Most google examples have a line that they insert.

If I directly insert the MAP into coolson jsonb, does it expect an HSTORE extension that I should not use if I am going to use jsonb?

2- : contact_id contact_id auth_contacts json datatype [ , ] join group_id auth_groups group_id

contact_id:

SELECT *
FROM discussion d 
JOIN 
(SELECT user_id, jsonb_object_keys(a.contacts) AS contacts FROM auth_contacts a WHERE user_id='XXX') AS c
ON (d.contact_id = c.contacts::text)
ORDER BY d.updated_date DESC

, 60 , 60 , - Obfuscated :

   "Sort  (cost=4194.02..4198.39 rows=1745 width=301) (actual time=50.791..51.042 rows=5590 loops=1)"
"  Sort Key: d.updated_date"
"  Sort Method: quicksort  Memory: 3061kB"
"  Buffers: shared hit=11601"
"  ->  Nested Loop  (cost=0.84..4100.06 rows=1745 width=301) (actual time=0.481..44.437 rows=5590 loops=1)"
"        Buffers: shared hit=11598"
"        ->  Index Scan using auth_contacts_pkey on auth_contacts a  (cost=0.42..8.93 rows=100 width=888) (actual time=0.437..1.074 rows=1987 loops=1)"
"              Index Cond: ((user_id)::text = '105037'::text)"
"              Buffers: shared hit=25"
"        ->  Index Scan using discussion_contact_id on discussion d  (cost=0.42..40.73 rows=17 width=310) (actual time=0.016..0.020 rows=3 loops=1987)"
"              Index Cond: ((contact_id)::text = (jsonb_object_keys(a.contacts)))"
"              Buffers: shared hit=11573"
"Planning time: 17.866 ms"
"Execution time: 52.192 ms"

- group_id. , jsonb_object_keys, userid vs authcontacts . , 60 60 (, ). , auth_groups ( 60 1000 , .

, jsonb- ?

+4

Source: https://habr.com/ru/post/1598423/


All Articles