There are two tables:
Authorized Contacts (auth_contacts
):
(
userid varchar
contacts jsonb
)
contacts
contains an array of contacts with attributes {contact_id, type}
discussion
:
(
contact_id varchar
discussion_id varchar
discussion_details jsonb
)
The table auth_contacts
has at least 100k records making it non JSONB type is not appropriate according as it would double or triple the amount of records.
Sample data for auth_contacts
:
userid | contacts
'11111' | '{"contact": [{"type": "type_a", "contact_id": "1-A-12"}
, {"type": "type_b", "contact_id": "1-A-13"}]}'
discussion
table has 5 million odd records.
I want to join on discussion.contact_id
(relational column) with contact id which a json object inside array of json objects in auth_contacts.contacts
.
One very crude way is:
SELECT *
FROM discussion d
JOIN (SELECT userid, JSONB_OBJECT_KEYS(a.contacts) AS auth_contact
FROM auth_contacts a) AS contacts
ON (d.contact_id = contacts.auth_contact::text)
What this does is actually at runtime create (inner sql) userid vs contact id table (Which is what I was avoiding and hence went for JSONB data type This query for a user with large records takes 26 + seconds which is not all good. Tried a few other ways: PostgreSQL 9.4: Aggregate / Join table on JSON field id inside array
But there should be a cleaner and better way which would be as simple as
JOIN d.contact_id = contacts -> contact -> contact_id?
When I try this, it doesn't yield any results.
When searching the net this seems to be a pretty cumbersome task?