I have some working code which pulls records from a forms table where '[email protected]' appears as a JSON value in one of the JSONB fields,column_a, column_b, column_c, or column_d.
SELECT *
FROM forms f
WHERE exists (SELECT *
FROM jsonb_each_text(f.column_a) as e(ky,val)
WHERE e.val = '[email protected]')
UNION
SELECT *
FROM forms f
WHERE exists (SELECT *
FROM jsonb_each_text(f.column_b) as e(ky,val)
WHERE e.val = '[email protected]')
UNION
SELECT *
FROM forms f
WHERE exists (SELECT *
FROM jsonb_each_text(f.column_c) as e(ky,val)
WHERE e.val = '[email protected]')
UNION
SELECT *
FROM forms f
WHERE exists (SELECT *
FROM jsonb_each_text(f.column_d) as e(ky,val)
WHERE e.val = '[email protected]');
The JSON in the columns is similar to:
{ "xyz":"[email protected]", "def":"[email protected]", "lmn":"[email protected]" }
Although the code works, it looks highly repetitive/long-winded. Given that I can't change the JSON structure, is there are more concise way of building this query, and what indexes should I be building for those columns for the best performance?