I have a 9.4 Postgres database which has a table containing a jsonb field.
The structure of the json looks similar to this:
{
name: 'customer1',
age: 28,
products: [{
name: 'product1',
price: 100
}, {
name: 'product2',
price: 200
}]
}
The following query returns the aforementioned json just fine:
SELECT jdoc
FROM customers, jsonb_array_elements(jdoc->'products') as products
WHERE (products->>'price')::numeric > 150
The problem is that the performance suffers quite a bit on bigger databases.
What index can I use to speed up this query?
What I've tried to far:
GINindices (bothjsonb_opsandjsonb_path_ops). They only seem to work on existence operators like@>though.CREATE INDEX ON persons(((jsonb_array_elements(jdoc->'products')->>'price')::numeric)). Which give me give me datatype mismatch error.- Note that
CREATE INDEX ON persons(((jdoc->'age')::numeric))works and allows me to query(jdoc->>'age')::numeric < 30fast.
- Note that