Re: count * performance issue - Mailing list pgsql-performance

From Scott Marlowe
Subject Re: count * performance issue
Date
Msg-id dcc563d10803102011i1749c2ccy9df76c99a18b060b@mail.gmail.com
Whole thread Raw
In response to Re: count * performance issue  ("Robins Tharakan" <tharakan@gmail.com>)
Responses Re: count * performance issue  (Albert Cervera Areny <albert@sedifa.com>)
List pgsql-performance
On Mon, Mar 10, 2008 at 7:57 PM, Robins Tharakan <tharakan@gmail.com> wrote:
> Hi,
>
> I have been reading this conversation for a few days now and I just wanted
> to ask this. From the release notes, one of the new additions in 8.3 is
> (Allow col IS NULL to use an index (Teodor)).
>
> Sorry, if I am missing something here, but shouldn't something like this
> allow us to get a (fast) accurate count ?
>
> SELECT COUNT(*) from table WHERE indexed_field IS NULL
>  +
> SELECT COUNT(*) from table WHERE indexed_field IS NOT NULL

It really depends on the distribution of the null / not nulls in the
table.  If it's 50/50 there's no advantage to using the index, as you
still have to check visibility info in the table itself.

OTOH, if NULL (or converserly not null) are rare, then yes, the index
can help.  I.e. if 1% of the tuples are null, the select count(*) from
table where field is null can use the index efficiently.

pgsql-performance by date:

Previous
From: "Joshua D. Drake"
Date:
Subject: Re: count * performance issue
Next
From: Greg Smith
Date:
Subject: Re: UPDATE 66k rows too slow