PW-773 Convert v_categorytree into a materialized view
This commit is contained in:
@@ -0,0 +1,63 @@
|
||||
-- Materialized View: public.v_categorytree
|
||||
DO $$
|
||||
BEGIN
|
||||
DROP MATERIALIZED VIEW IF EXISTS public.v_categorytree;
|
||||
EXCEPTION WHEN others THEN
|
||||
RAISE NOTICE 'Materialized view does not exist';
|
||||
END;
|
||||
$$;
|
||||
|
||||
DO $$
|
||||
BEGIN
|
||||
DROP VIEW IF EXISTS public.v_categorytree;
|
||||
EXCEPTION WHEN others THEN
|
||||
RAISE NOTICE 'View does not exist';
|
||||
END;
|
||||
$$;
|
||||
|
||||
CREATE MATERIALIZED VIEW public.v_categorytree AS
|
||||
SELECT c.id,
|
||||
c.name,
|
||||
( SELECT array_to_string(array_agg(main.parent_name), '/'::text) AS path
|
||||
FROM ( SELECT node.id,
|
||||
parent.name AS parent_name
|
||||
FROM categories node,
|
||||
categories parent
|
||||
WHERE node.lft >= parent.lft AND node.lft <= parent.rgt AND node.id = c.id
|
||||
ORDER BY parent.lft) main
|
||||
GROUP BY main.id) AS path,
|
||||
( SELECT array_to_string(array_agg(main.parent_id), '/'::text) AS path
|
||||
FROM ( SELECT node.id,
|
||||
parent.id AS parent_id
|
||||
FROM categories node,
|
||||
categories parent
|
||||
WHERE node.lft >= parent.lft AND node.lft <= parent.rgt AND node.id = c.id
|
||||
ORDER BY parent.lft) main
|
||||
GROUP BY main.id) AS path_numeric,
|
||||
CASE c.rgt - c.lft
|
||||
WHEN 1 THEN 1
|
||||
ELSE 0
|
||||
END AS is_leaf,
|
||||
( SELECT count(*) - 1
|
||||
FROM categories node,
|
||||
categories parent
|
||||
WHERE node.lft >= parent.lft AND node.lft <= parent.rgt AND node.id = c.id) AS depth,
|
||||
( SELECT count(*) - 1
|
||||
FROM categories c1
|
||||
WHERE c1.lft >= c.lft AND c1.lft <= c.rgt) AS child_count,
|
||||
c.lft,
|
||||
c.rgt
|
||||
FROM categories c
|
||||
ORDER BY c.lft
|
||||
WITH DATA;
|
||||
|
||||
GRANT ALL ON TABLE v_categorytree TO photowall;
|
||||
|
||||
-- Index: public.v_categorytree_key
|
||||
|
||||
-- DROP INDEX public.v_categorytree_key;
|
||||
|
||||
CREATE UNIQUE INDEX v_categorytree_key
|
||||
ON public.v_categorytree
|
||||
USING btree
|
||||
(id);
|
||||
Reference in New Issue
Block a user