Re: Why Select Count(*) from table - took over 20 minutes?

From: Jasen Betts <jasen(at)xnet(dot)co(dot)nz>
To: pgsql-general(at)postgresql(dot)org
Subject: Re: Why Select Count(*) from table - took over 20 minutes?
Date: 2010-11-03 10:15:23
Message-ID: iarcnr$sp4$1@reversiblemaps.ath.cx
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

On 2010-10-26, John R Pierce <pierce(at)hogranch(dot)com> wrote:
>
> count(*) has to read the whole table to get the accurate count. The
> reason for this is that different clients can see different versions of
> that table, for instance, if client A is already in a transaction, and
> client B then does an INSERT, the two clients will see different values
> for the count.

They may or may not. the default transaction isolation level "read commited"
allows a session to see most changes that were committed externally after the
start of the transaction.

Tlso the index may include deleted rows.
which is another reason count(*) does a table scan.

--
ɹǝpun uʍop ɯoɹɟ sƃuıʇǝǝɹ⅁

In response to

Browse pgsql-general by date

  From Date Subject
Next Message Gabriele Bulfon 2010-11-03 10:29:25 Running postgres with a different user/group
Previous Message Dimitri Fontaine 2010-11-03 09:03:12 Re: Group by and limit