我有一個intarray
列feature_value_ids
物化視圖(~100k行)。我想要的是根據物化視圖上的某些條件從該列中選擇所有唯一標識DISTINCT over huge unnest
此查詢運行正常,aprox。 30毫秒,讓〜1萬行:
SELECT unnest(feature_value_ids) FROM dematerialized_products
WHERE is_private = 'f' AND product_category_ids && ARRAY [38]
不過,如果我添加DISTINCT,查詢降至〜400毫秒,從而〜5K行
SELECT DISTINCT unnest(feature_value_ids) FROM dematerialized_products
WHERE is_private = 'f' AND product_category_ids && ARRAY [38]
我試圖做一個遞歸查詢,沒有運氣(〜35秒),像這樣:
WITH RECURSIVE t AS (
(SELECT min(value_id) AS value_id FROM z)
UNION ALL
SELECT (SELECT min(value_id) FROM z WHERE value_id > t.value_id) AS value_id
FROM t
WHERE t.value_id IS NOT NULL
), z as (
SELECT unnest(feature_value_ids) as value_id
FROM dematerialized_products a
WHERE is_private = 'f' AND product_category_ids && ARRAY [38]
)
SELECT * FROM t WHERE t.value_id IS NOT NULL
我想這是因爲z
是,在每次評估遞歸查詢並