I create the following indexes on jsonb columns in my table:
CREATE INDEX idx_gin_accounts ON t1 USING GIN (accounts jsonb_path_ops);
CREATE INDEX idx_gin_stocks ON t1 USING GIN (stocks jsonb_path_ops);
CREATE INDEX idx_gin_stocks_value ON t1 USING GIN ((stocks-> 'value'));
CREATE INDEX idx_gin_stocks_type ON t1 USING GIN ((stocks-> 'type'));
My query is like this:
SELECT
t.accounts ->> 'name' as account_name
//other columns
FROM t1 t
left join lateral jsonb_array_elements(t.accounts) a(accounts)
on 1 = 1 and a.accounts @> '{"role": "ADVISOR"}'
left join lateral jsonb_array_elements(t1.stocks) s(stocks)
on 1 = 1 and s.stocks @> '{"type": "RIC"}'
WHERE (s.stocks -> 'value' ? 'XXX')
When I analyse with EXPLAIN ANALYSE I do not see these indexes being used in the query plan.
Should different indexes be created? Or How can I use these ones to speed up the search?
Say When I pass in (s.stocks-> 'value' ? 'XXX') in where condition, I would want the search to be optimal?
whereclause on the outer joined table turns the outer join back into an inner join