Re: []performance issues

From: "Christopher Kings-Lynne" <chriskl(at)familyhealth(dot)com(dot)au>
To: "Yaroslav Dmitriev" <yar(at)warlock(dot)ru>, "PostgreSQL-development" <pgsql-hackers(at)postgresql(dot)org>
Subject: Re: []performance issues
Date: 2002-08-02 09:15:38
Message-ID: GNELIHDDFBOCMGBFGEFOIEHOCDAA.chriskl@familyhealth.com.au
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general pgsql-hackers

> Here we have table "stats" with something over one millon records.
> Obvious "SELECT COUNT(*) FROM stats " takes over 40 seconds to execute,
> and this amount of time does not shorten considerably in subsequent
> similar requests. All the databases are vacuumed nightly.

Doing a row count requires a sequential scan in Postgres.

Try creating another summary table that just has one row and one column and
is an integer.

Then, create a trigger on your stats table that fires whenever a new row is
added or deleted and updates the tally of rows in the summary table.

Then, just select from the summary table to get an instantaneous count. Of
course, insert and deletes will be marginally slowed down.

Refer to the docs for CREATE TRIGGER, CREATE FUNCTION and PL/PGSQL for more
info on how to do this.

Regards,

Chris

In response to

Responses

Browse pgsql-general by date

  From Date Subject
Next Message Markus Wollny 2002-08-02 09:30:31 Questions regarding contrib/tsearch
Previous Message Yaroslav Dmitriev 2002-08-02 08:59:19 []performance issues

Browse pgsql-hackers by date

  From Date Subject
Next Message Karel Zak 2002-08-02 09:51:39 Re: getpid() function
Previous Message Yaroslav Dmitriev 2002-08-02 08:59:19 []performance issues