pg.ddx.io  pgsql-general@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Rob Sargent <robjsargent@gmail.com>
To: Francisco Olarte <folarte@peoplecall.com>
Cc: pabloa98 <pabloa98@gmail.com>
Cc: pgsql-general@postgresql.org <pgsql-general@postgresql.org>
Subject: Re: SELECT all the rows where id is children of other node.
Date: Wed, 21 Aug 2019 11:54:14 -0600
Message-ID: <1A6490A9-87AB-41E0-AAE1-74403C6E2131@gmail.com> (raw)
In-Reply-To: <CA+bJJbzW8F8F_A3cYypk6qiu3tp34f2nXU2uBS3enf1jJ5cjCw@mail.gmail.com>
References: <CAEjudX6P5NaBPKGDV6f=KwEKGGG0-_R_PruqaY0gM5SWU-xA6A@mail.gmail.com>
	<60F3F05A-84AF-4944-8EA2-5484A5D5B0C3@gmail.com>
	<CAEjudX6KqLePkP6zAyiuFHx=+ow4GvtPUm8sA9-MBat97NB8BA@mail.gmail.com>
	<CA+bJJbzW8F8F_A3cYypk6qiu3tp34f2nXU2uBS3enf1jJ5cjCw@mail.gmail.com>



> On Aug 21, 2019, at 3:35 AM, Francisco Olarte <folarte@peoplecall.com> wrote:
> 
> Pablo:
> 
> On Tue, Aug 20, 2019 at 6:49 PM pabloa98 <pabloa98@gmail.com> wrote:
>> Thank you for your responses Rob. Appreciated. The problem with recursive queries is that they are executed several times and it has and impact in performance.
>> I need a subset of those rows and I want them in one pass.
>> I discovered that ltree extension could be useful. I will play with it today. I am sure there's a way to find al the nodes in O(n) time with n = size of the resulset ...
> 
> Unless you have some extra conditions in a table ( like
> "autoincremented immutable primary key and parents are always created
> before childs" ) I think your problem of "get all the descendant ( i
> do not like to call them children ) nodes of a given id" can not be
> solved in one pass.
> 
> I mean, if you are getting descendants of the node N1, you need to
> read the last node, NL, of the table to know if it is a child of N1.
> But then you have to read the table again to find childs of NL.
> 
> Of course, if you have something like "hierarchical ids" you can
> traverse ordering by it and know NL MUST be childless, and build the
> tree rooted on node N1 as you go, but without some of this conditions
> I do not think it can be done in an "ideal" db ( which lets you scan
> in any order you can define from just a row without cost ) in one scan
> ( storing and prunning the whole tree as you go is cheating ).
> 
> Also, if your node ids come from a serial and are immutables, or you
> take a little care when mutating them, you can do it traversing by id,
> but you need a full scan, a recursive query with several index scans
> may easily be faster in wide trees.
> 
> 
> Francisco Olarte.
If you accept Francisco’s thesis then you may be interested in this
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 a.last, array_agg(distinct(a.kids))as clan from (select last, unnest(array_remove(children, null)) as kids from descendants where children[1] is not null) as a group by last order by last
 last |           clan           
------+--------------------------
    1 | {2,3,4,21,22,23,221,222}
    2 | {21,22,23,221,222}
   22 | {221,222}
(3 rows)
No comment on performance other than to say that if you are interested in the result for a given seed parent then performance would likely correlate with the average depth of your lineages.

I believe the ascending order of the members of each clan is completely fortuitous unless it’s a consequence of distinct?

view thread (8+ messages)

Message-ID: <1A6490A9-87AB-41E0-AAE1-74403C6E2131@gmail.com>
Permalink:  ../1A6490A9-87AB-41E0-AAE1-74403C6E2131@gmail.com/
Also on:    postgresql.org/message-id/1A6490A9-87AB-41E0-AAE1-74403C6E2131@gmail.com

 · 

reply

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Reply to all the recipients from your mail client; the link fills in
  the headers below: reply via email
  Some clients, webmail especially, drop In-Reply-To and References from
  a link, and the reply then starts a new thread: use the mbox below.

  To: pgsql-general@postgresql.org
  Cc: robjsargent@gmail.com, folarte@peoplecall.com, pabloa98@gmail.com
  Subject: Re: SELECT all the rows where id is children of other node.
  In-Reply-To: <1A6490A9-87AB-41E0-AAE1-74403C6E2131@gmail.com>
  References: <CAEjudX6P5NaBPKGDV6f=KwEKGGG0-_R_PruqaY0gM5SWU-xA6A@mail.gmail.com> <60F3F05A-84AF-4944-8EA2-5484A5D5B0C3@gmail.com> <CAEjudX6KqLePkP6zAyiuFHx=+ow4GvtPUm8sA9-MBat97NB8BA@mail.gmail.com> <CA+bJJbzW8F8F_A3cYypk6qiu3tp34f2nXU2uBS3enf1jJ5cjCw@mail.gmail.com> <1A6490A9-87AB-41E0-AAE1-74403C6E2131@gmail.com>

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox