By using db<>fiddle, you agree to license everything you submit by Creative Commons CC0.
Help with an interesting Postgres question: Why isn't an Index Only Scan used on a partition accessed via the parent table?.
create table it220626 (
id int not null primary key
, code text not null
, name text not null
, parent int
);
insert into it220626 values
(1, 'M0', '系統', NULL),
(2, 'M00', '權限', 1),
(3, 'M00-00', '角色權限', 2),
(4, 'M00-01', '角色審核', 2);
select *
from it220626;
4 rows affected
id | code | name | parent |
---|---|---|---|
1 | M0 | 系統 | null |
2 | M00 | 權限 | 1 |
3 | M00-00 | 角色權限 | 2 |
4 | M00-01 | 角色審核 | 2 |
with recursive c as (
select id
, '[' || code || ',' || name || ']' as path
from it220626
where parent is null
union all
select i.id
, c.path || ' -> ' || '[' || i.code || ',' || i.name || ']'
from it220626 i
join c
on i.parent = c.id
)
select id
, path
from c;
id | path |
---|---|
1 | [M0,系統] |
2 | [M0,系統] -> [M00,權限] |
3 | [M0,系統] -> [M00,權限] -> [M00-00,角色權限] |
4 | [M0,系統] -> [M00,權限] -> [M00-01,角色審核] |