Re: Adjacency List & total item counts

From: Oleg Bartunov <oleg(at)sai(dot)msu(dot)su>
To: Ben <newreaders(at)gmail(dot)com>
Cc: PostgreSQL <pgsql-general(at)postgresql(dot)org>
Subject: Re: Adjacency List & total item counts
Date: 2005-08-09 07:03:37
Message-ID: Pine.GSO.4.63.0508091103230.599@ra.sai.msu.su
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

use contrib/ltree

Oleg
On Tue, 9 Aug 2005, Ben wrote:

> Hi
>
> This question is not specific to PostgreSQL but I would like to know
> what is the best way to count the number of items in each node from
> the leaf to the root? Something like this:
>
> Computers (100)
> /\
> / \
> CPU (15) Memory (85)
>
> I have the following SQL schema:
>
> Tree (
> treeId int,
> parentId int,
> name varchar(250),
> )
>
> Item (
> itemId int,
> treeId int,
> expiryDate date
> )
>
> Note that the count for the total number of items in each node depends
> on the item expiry date, i.e. ignore the item if the expiry date is
> older than now().
>
> I have come up with the following solutions but not happy with any one of them:
>
> 1) Do a batch count, i.e. count the number of items every 30 minutes.
> Using this method defeats the purpose of having the count next to each
> node since the number might not be the same as the actual count.
>
> 2) Use trigger but this can be slow since it has to recurse the tree
> and do the sum every time new item is added.
>
> Thanks
> Ben
>
> ---------------------------(end of broadcast)---------------------------
> TIP 9: In versions below 8.0, the planner will ignore your desire to
> choose an index scan if your joining column's datatypes do not
> match
>

Regards,
Oleg
_____________________________________________________________
Oleg Bartunov, sci.researcher, hostmaster of AstroNet,
Sternberg Astronomical Institute, Moscow University (Russia)
Internet: oleg(at)sai(dot)msu(dot)su, http://www.sai.msu.su/~megera/
phone: +007(095)939-16-83, +007(095)939-23-83

In response to

Browse pgsql-general by date

  From Date Subject
Next Message Frank Millman 2005-08-09 07:35:25 Re: Case sensitivity
Previous Message Ben 2005-08-09 06:39:25 Adjacency List & total item counts