Re: Improving count(*) - Mailing list pgsql-hackers

From Kevin Grittner
Subject Re: Improving count(*)
Date
Msg-id 437CB028020000250000081E@gwmta.wicourts.gov
Whole thread Raw
In response to Improving count(*)  (Simon Riggs <simon@2ndquadrant.com>)
Responses Re: Improving count(*)  (Simon Riggs <simon@2ndquadrant.com>)
List pgsql-hackers
In Sybase ASE (and I'm pretty sure the same is true in Microsoft SQL
Server) the leaf level of the narrowest index on the table is scanned,
following a linked list of leaf pages.  Leaf pages can be pretty dense
under Sybase, because they do use prefix compression.  A count(*)
on a table with 100 million rows is going to take a few minutes, but it
is going to be at least an order of magnitude faster than a data page
scan -- maybe two orders of magnitude faster.

What I don't understand is why people need to do such things so
frequently that it's a major issue, rather than an occassional
annoyance.  A solution which not only helped the count(*) issue
but also allowed index scans to skip the trip to the data page to
see if it's an active version seems like it would boost performance
overall.  As pointed out elsewhere, it could also allow new
techniques for vacuum which could be beneficial.

My view is that when tables get so big that a count(*) takes that
much time, you don't typiclally need an EXACT count anyway --
you could normally check the statistics from your nightly analyze.

-Kevin


>>> Tom Lane <tgl@sss.pgh.pa.us>  >>>
Simon Riggs <simon@2ndquadrant.com> writes:
> Bearing in mind other RDBMS' approach is to count the number of rows
in
> an index, their cost is probably about the same as scanning table
> blocks/10 very roughly - so the cost is far from zero for them.

Really?  The impression I get is that people who ask for this expect the
answer to be instantaneous, ie they think the system will maintain a
running net total for each table.  (In a non-MVCC system that isn't
necessarily an unreasonable thing to do.)

I really can't get excited about adding this level of complexity and
overhead to the system just to support COUNT(*)-with-no-WHERE slightly
better than we do now.

The triggers-and-deltas approach previously proposed seems considerably
more attractive to me, because (1) it's not invasive and (2) you only
have to pay the overhead on tables where you want it.
        regards, tom lane



pgsql-hackers by date:

Previous
From: Philip Yarra
Date:
Subject: Re: Optional postgres database not so optional in 8.1
Next
From: Tom Lane
Date:
Subject: Re: Improving count(*)