Re: count(*) slow on large tables - Mailing list pgsql-performance

From Bruce Momjian
Subject Re: count(*) slow on large tables
Date
Msg-id 200310041748.h94Hmlc04602@candle.pha.pa.us
Whole thread Raw
In response to Re: count(*) slow on large tables  (Tom Lane <tgl@sss.pgh.pa.us>)
Responses Re: count(*) slow on large tables
List pgsql-performance
Tom Lane wrote:
> Bruce Momjian <pgman@candle.pha.pa.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.

The cache is an ordinary table, with xid's on every row.  I meant it
would require no index/heap scans of the large table --- it would still
require a scan of the "count" table.

--
  Bruce Momjian                        |  http://candle.pha.pa.us
  pgman@candle.pha.pa.us               |  (610) 359-1001
  +  If your life is a hard drive,     |  13 Roberts Road
  +  Christ can be your backup.        |  Newtown Square, Pennsylvania 19073

pgsql-performance by date:

Previous
From: Tom Lane
Date:
Subject: Re: count(*) slow on large tables
Next
From: Tom Lane
Date:
Subject: Re: count(*) slow on large tables