Re: Slow COUNT

From: Jan Wieck <JanWieck(at)Yahoo(dot)com>
To: Jaime Casanova <systemguards(at)gmail(dot)com>
Cc: Poul Møller Hansen <freebsd(at)pbnet(dot)dk>, pgsql-general(at)postgresql(dot)org
Subject: Re: Slow COUNT
Date: 2005-12-02 19:28:53
Message-ID: 4390A075.5070000@Yahoo.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

On 12/2/2005 2:02 PM, Jaime Casanova wrote:
> so the way to do it is create a trigger that record in a table the
> number of rows...

Neither, because now you have to update one single row in that new
table, which causes a row exclusive lock. That is worse than an
exclusive lock on the original table because it has the same
serialization of writers but the additional work to update the count
table as well as vacuum it.

What you need is a separate table where your trigger will insert delta
rows with +1 or -1 for insert and delete. A view will sum() over that
and tell you the true number of rows. Periodically you condense the
table by replacing all current rows with one that represents the sum().

Jan

--
#======================================================================#
# It's easier to get forgiveness for being wrong than for being right. #
# Let's break this rule - forgive me. #
#================================================== JanWieck(at)Yahoo(dot)com #

In response to

Browse pgsql-general by date

  From Date Subject
Next Message P G 2005-12-02 19:29:38 Re: How: single db, multiple users
Previous Message Bruce Momjian 2005-12-02 19:09:08 Re: Numeric 508 datatype