Re: COUNT(*) and index-only scans - Mailing list pgsql-hackers

From Kevin Grittner
Subject Re: COUNT(*) and index-only scans
Date
Msg-id 4E92E6CA0200002500041CD0@gw.wicourts.gov
Whole thread Raw
In response to COUNT(*) and index-only scans  (Bruce Momjian <bruce@momjian.us>)
Responses Re: COUNT(*) and index-only scans  (Robert Haas <robertmhaas@gmail.com>)
Re: COUNT(*) and index-only scans  (Tom Lane <tgl@sss.pgh.pa.us>)
Re: COUNT(*) and index-only scans  (Jeff Janes <jeff.janes@gmail.com>)
List pgsql-hackers
Bruce Momjian <bruce@momjian.us> wrote:
> I talked to Robert Haas and he said that index-only scans do not
> optimize COUNT(*).  Is this something we can do for PG 9.2?  Is
> anyone working on this?
Well, it's not that it doesn't optimize COUNT(*) -- it's that it
doesn't yet cost the index scan as cheaper than a table scan when
you're accessing every row.
create table t (id int not null primary key);
insert into t select generate_series(1, 1000000);
vacuum freeze analyze;
explain analyze select count(*) from t where id between 500000 and 500010;
That gives you an index-only scan; but without the WHERE clause it
uses a seq scan.  I think it's mainly a matter of doing enough
benchmarks to figure out how best to model the costs of the index
scan so that it can be picked for that case.
-Kevin


pgsql-hackers by date:

Previous
From: Greg Stark
Date:
Subject: Re: COUNT(*) and index-only scans
Next
From: Thom Brown
Date:
Subject: Re: Range Types - typo + NULL string constructor