Re: SELECT all the rows where id is children of other node.
От | Rob Sargent |
---|---|
Тема | Re: SELECT all the rows where id is children of other node. |
Дата | |
Msg-id | 60F3F05A-84AF-4944-8EA2-5484A5D5B0C3@gmail.com обсуждение исходный текст |
Ответ на | SELECT all the rows where id is children of other node. (pabloa98 <pabloa98@gmail.com>) |
Ответы |
Re: SELECT all the rows where id is children of other node.
|
Список | pgsql-general |
On Aug 19, 2019, at 7:42 PM, pabloa98 <pabloa98@gmail.com> wrote:Hello,I have a huge table (100 million rows) of relations between nodes by id in a Postgresql 11 server. Like this:CREATE TABLE relations (pid INTEGER NOT NULL,cid INTEGER NOT NULL,)This table has parent-child relations references between nodes by id. Like:pid -> cidn1 -> n2n1 -> n3n1 -> n4n2 -> n21n2 -> n22n2 -> n23n22 -> n221n22 -> n222I would like to get a list of all the nodes being children (direct or indirect) of any other node.Example. The children of:1) n3: [] (n3 has not children)
2) n22: [n221, n222] (n22 has 2 children: n221 and n222)3) n1: [n2, n21, n22, n23, n221, n222] (n1 has 6 children including indirect children).this pseudo SQL:SELECT *FROM relationsWHERE has_parent(myId)It can be solved with a recursive function or stored procedure. But that requires several passes. Is it possible to solve it in one pass? Perhaps using some low-level function or join or some index expression or auxiliary columns?It is OK to create an index or similar using recursive expressions. However, the SELECT expressions should be solved in one pass because of speed.Pablo
with recursive descendants (last, children) as
(select c.c, array[null::int] from kids c where not exists (select 1 from kids p where c.c = p.p)
union all
select k.p, array[k.c] || l.children from kids k, descendants l where k.c = l.last)
select last, children from descendants where children[1] is not null order by last
last | children
------+-----------------
1 | {2,22,222,NULL}
1 | {4,NULL}
1 | {2,21,NULL}
1 | {2,23,NULL}
1 | {2,22,221,NULL}
1 | {3,NULL}
2 | {22,221,NULL}
2 | {22,222,NULL}
2 | {21,NULL}
2 | {23,NULL}
22 | {221,NULL}
22 | {222,NULL}
(12 rows)
I’ll throw in the towel now
В списке pgsql-general по дате отправления: