Adjacency List & total item counts

Поиск
Список
Период
Сортировка
От Ben
Тема Adjacency List & total item counts
Дата
Msg-id 5f4a64610508082339f632987@mail.gmail.com
обсуждение исходный текст
Ответы Re: Adjacency List & total item counts  (Oleg Bartunov <oleg@sai.msu.su>)
Список pgsql-general
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

В списке pgsql-general по дате отправления:

Предыдущее
От: Tom Lane
Дата:
Сообщение: Re: plpythonu and return void
Следующее
От: Oleg Bartunov
Дата:
Сообщение: Re: Adjacency List & total item counts