0

I have a jsonb column which have following rows

ROW1:

 [
        {
            "cpe23Uri": "cpe:2.3:a:sgi:irix:3.55:*:*:*:*:*:*:*",
            "active": true
        },
        {
            "cpe23Uri": "cpe:2.3:a:university_of_washington:imap:10.234:*:*:*:*:*:*:*",
            "active": true
        }
    ]

ROW 2:

 []

ROW 3:

[
    {
        "cpe23Uri": "cpe:2.3:o:sgi:irix:*:*:*:*:*:*:*:*",
        "active": true
    }
]

I want to find the rows which contain sgi:irix in the key cpe23Uri

Which query should i use for best performance?

1 Answer 1

1

You could use an exists condition with a correlated subquery that uses jsonb_array_element() to unnest and search the array:

select *
from mytable t
where exists (
    select 1
    from jsonb_array_elements(t.js) x
    where  x->>'cpe23Uri' like '%sgi:irix%'
);

Demo on DB Fiddle

2
  • It works great! Is there any way to make it faster? because it need about 3 seconds to get the results. I have to run this query for a big analyse 15M times which can take 1 month so.. any idea? Commented Feb 3, 2020 at 14:50
  • select * from t WHERE t.products @>'[{"cpe23Uri":"cpe:2.3:a:sgi:irix:0.1.0:*:*:*:*:*:*:*"}]'::jsonb; This query takes only 1 second but without LIKE operator.. Commented Feb 3, 2020 at 14:53

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.