索引jsonb数组和查询jsonb数组

时间:2017-05-09 15:40:55

标签: postgresql indexing jsonb postgresql-9.6

[{"id": "345", "categoryid": "2", "products": ["23", "45","34"]}]
[{"id": "2343", "categoryid": "3", "products":  ["123", "455"]}]
[{"id": "12", "categoryid": "12", "products":  ["234", "459","314","4563"]}, {"id": "512", "categoryid": "15", "products":  ["234"]}]
[{"id": "123", "categoryid": "3", "products":  ["293", "145","634"]}, {"id": "889", "categoryid": "18", "products":  ["123", "845","1234"]}]
[{"id": "2546", "categoryid": "2", "products":  ["2397", "452","434","234", "479","304","4563"]}]


SELECT *
FROM   products p,
       jsonb_array_elements(p.products_rec_jsonb) elem
WHERE  (elem ->> 'id')::bigint < 1234
       AND
       (
         (
           (elem -> 'products') ?| array(12,576,34)::text[]
           AND
           (elem -> 'categoryid') ?| array(12,5)::text[]
         )
         OR
         (elem -> 'categoryid') ?| array(12,5)::text[]
       )
ORDER BY (elem ->> 'id')::bigint DESC
LIMIT 20;

CREATE INDEX CONCURRENTLY idx_products_products ON products  USING GIN (products_rec_jsonb -> 'products')  

CREATE INDEX CONCURRENTLY idx_products_category ON products  USING GIN (products_rec_jsonb -> 'categoryid')  

CREATE INDEX CONCURRENTLY idx_products_id ON products  USING BTREE (products_rec_jsonb ->> 'id')  

我需要使这个查询使用索引(在jsonb列products_rec_jsonb上),如"products""categoryid""id"

我想将"products""category_id"索引为GIN,将"id"索引为BTREE DESC(作为bigint数据类型)...问题是当我运行查询时它根本不使用索引我怎么修理它?

0 个答案:

没有答案