您如何基于JSONB字段对象array和适当的索引来创建PostgreSQL视图?下面的示例。
CREATE TABLE "ProductLists"
(
id uuid NOT NULL DEFAULT gen_random_uuid(),
listName text NOT NULL
productIds jsonb NOT NULL DEFAULT '[{ productId:0 }]'::jsonb,
)
具有以下视图:
SELECT "ProductLists".id AS listId,
jsonb_array_elements("ProductLists".productIds) ->> 'productId'::text AS productId
FROM "ProductLists";
感谢您的输入!
jsonb列上的GIN索引将支持several JSON operators。其中之一是@>
运算符,它也可用于JSON数组。
以下索引:
create index on product_list using gin (product_ids);
可能会利用上述索引的查询看起来像这样:
select *
from product_list
where product_ids @> '[{"productId": 42}]'::jsonb;
您的建议视图无法利用索引,因为JSONB列不属于该视图,因此必须向下推JSON列的条件。
您可以在视图中使用索引的唯一方法是在其中包含JSON列:
create view normalized_list
as
SELECT pl.id AS list_id,
t.product_id,
pl.product_ids
FROM product_list pl
CROSS JOIN jsonb_array_elements(pl.product_ids) ->> 'id' AS t(product_id)
;
这样的查询:
select id, product_Id
from normalized_list
where product_ids @> '[{"id":42}]'::jsonb;
将利用GIN索引。
请注意,如果only要以非规范化方式存储ID,则本机整数数组(product_ids int[]
)会更高效,并使表大大变小]