Skip site navigation (1) Skip section navigation (2)

Re: Select max(foo) and select count(*) optimization

From: Christopher Browne <cbbrowne(at)acm(dot)org>
To: pgsql-performance(at)postgresql(dot)org
Subject: Re: Select max(foo) and select count(*) optimization
Date: 2004-01-06 01:46:00
Message-ID: (view raw, whole thread or download thread mbox)
Lists: pgsql-performance
Martha Stewart called it a Good Thing when paul(at)tuckfield(dot)com (Paul Tuckfield) wrote:
> Not that I'm offering to do the porgramming mind you, :) but . . 
> In the case of select count(*), one optimization is to do  a scan of the
> primary key, not the table itself, if the table has a primary key. In a
> certain commercial, lesser database, this is called an "index fast full
> scan".  It would be important to scan the index in physical order
> (sequential physical IO) and not in key order (random physical IO)

The problem is that this "optimization" does not actually work.  The
index does not contain transaction visibility information, so you have
to go to the pages of tuples in order to determine if any given tuple
is visible.

> I'm guessing the payoff as well as real-world-utility of a max(xxx)
> optimization are much higher than a count(*) optimization tho

That's probably so.

In many cases, approximations, such as page counts, may be good
enough, and pray consider, that ("an approximation") is probably all
you were getting from the database systems that had an "optimization"
to store the count in a counter.
let name="cbbrowne" and tld="" in name ^ "@" ^ tld;;
"No, you  misunderstand. Microsoft asked  some hackers how  they could
make their system secure - the hackers replied "Turn it off.". So they
did." -- Anthony Ord

In response to


pgsql-performance by date

Next:From: Christopher BrowneDate: 2004-01-06 02:31:29
Subject: Re: Select max(foo) and select count(*) optimization
Previous:From: Doug McNaughtDate: 2004-01-06 00:29:56
Subject: Re: Select max(foo) and select count(*) optimization

Privacy Policy | About PostgreSQL
Copyright © 1996-2017 The PostgreSQL Global Development Group