Re: count(*) slow on large tables

From: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>
To: Bruce Momjian <pgman(at)candle(dot)pha(dot)pa(dot)us>
Cc: Christopher Browne <cbbrowne(at)libertyrms(dot)info>, pgsql-performance(at)postgresql(dot)org
Subject: Re: count(*) slow on large tables
Date: 2003-10-04 16:49:33
Message-ID: 6743.1065286173@sss.pgh.pa.us
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-hackers pgsql-performance

Bruce Momjian <pgman(at)candle(dot)pha(dot)pa(dot)us> writes:
> We do have a TODO item:
> * Consider using MVCC to cache count(*) queries with no WHERE clause

> The idea is to cache a recent count of the table, then have
> insert/delete add +/- records to the count. A COUNT(*) would get the
> main cached record plus any visible +/- records. This would allow the
> count to return the proper value depending on the visibility of the
> requesting transaction, and it would require _no_ heap or index scan.

... and it would give the wrong answers. Unless the cache is somehow
snapshot-aware, so that it can know which other transactions should be
included in your count.

regards, tom lane

In response to

Responses

Browse pgsql-hackers by date

  From Date Subject
Next Message Tom Lane 2003-10-04 16:53:42 Re: Beta4 Tag'd and Bundled ...
Previous Message Adam Witney 2003-10-04 16:30:17 Re: Beta4 Tag'd and Bundled ...

Browse pgsql-performance by date

  From Date Subject
Next Message Bruce Momjian 2003-10-04 17:48:47 Re: count(*) slow on large tables
Previous Message Tom Lane 2003-10-04 16:07:53 COUNT(*) again (was Re: [HACKERS] Index/Function organized table layout)