Re: count * performance issue

From: paul rivers <rivers(dot)paul(at)gmail(dot)com>
To: Craig James <craig_james(at)emolecules(dot)com>
Cc: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>, pgsql-performance(at)postgresql(dot)org
Subject: Re: count * performance issue
Date: 2008-03-07 05:38:48
Message-ID: 47D0D4E8.6040808@gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-performance

Craig James wrote:
> Tom Lane wrote:
>> Craig James <craig_james(at)emolecules(dot)com> writes:
>>> Count() on Oracle and MySQL is almost instantaneous, even for very
>>> large tables. So why can't Postgres do what they do?
>>
>> AFAIK the above claim is false for Oracle. They have the same
>> transactional issues we do.
>
> My experience doesn't match this claim. When I ported my application
> from Oracle to Postgres, this was the single biggest performance
> problem. count() in Oracle was always very fast. We're not talking
> about a 20% or 50% difference, we're talking about a small fraction of
> a second (Oracle) versus a minute (Postgres) -- something like two or
> three orders of magnitude.
>
> It may be that Oracle has a way to detect when there's no transaction
> and use a faster method. If so, this was a clever optimization -- in
> my experience, that represents the vast majority of the times you want
> to use count(). It's not very useful to count the rows of a table
> that many apps are actively modifying since the result may change the
> moment your transaction completes. Most of the time when you use
> count(), it's because you're the only one modifying the table, so the
> count will be meaningful.
>
> Craig
>
>

Oracle will use a btree index on a not null set of columns to do a fast
full index scan, which can be an order of magnitude or faster compared
to a table scan. Also, Oracle can use a bitmap index (in cases where a
bitmap index isn't otherwise silly) for a bitmap fast index scan/bitmap
conversion for similar dramatic results.

For "large" tables, Oracle is not going to be as fast as MyISAM tables
in MySQL, even with these optimizations, since MyISAM doesn't have to
scan even index pages to get a count(*) answer against the full table.

Paul

In response to

Browse pgsql-performance by date

  From Date Subject
Next Message Tom Lane 2008-03-07 05:40:13 Re: Improve Full text rank in a query
Previous Message shilpa.raghavendra 2008-03-07 04:59:00 database design for large data.