Re: COUNT(*) and index-only scans

From: jesper(at)krogh(dot)cc
To: "Kevin Grittner" <Kevin(dot)Grittner(at)wicourts(dot)gov>
Cc: jeff(dot)janes(at)gmail(dot)com, bruce(at)momjian(dot)us, pgsql-hackers(at)postgresql(dot)org
Subject: Re: COUNT(*) and index-only scans
Date: 2011-10-11 06:42:02
Message-ID: 6e3fedf529086c83ed890d34c5569448.squirrel@shrek.krogh.cc
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-hackers

>> Jeff Janes wrote:
>> Kevin Grittner wrote:
>
>>> 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.
>>
>> If you convert the where clause to "where id is not null" it uses
>> the index only scan again, but only if you nudge it too with
>> enable_seqscan=off.
>
> Clever way to get a full-table test.
>
> It turns out that for the above, with your trick to use the index
> only scan, it comes out 12% faster to do a seqscan, even when the
> table and index are fully cached (based on the average time of ten
> runs each way). There's very little overlap, so the difference looks
> real. But that's on a very narrow record, having just the one column
> used in the index. I added one wide column like this:
>
> alter table t add column x text;
> update t set x = (repeat(random()::text, (random() * 100)::int));
> cluster t USING t_pkey;
> vacuum freeze analyze;
>
> With that change the index-only scan time remained unchanged, while
> the seqscan time grew to about 2.6 times the index only scan time.
> That was mildly surprising for me, considering it was all still
> cached.

Moving data around in memory isn't that cheap, and you have probably made
the row size 5 times larger by the above thing. However if you added 4000
bytes instead of 100, you should be back to the previous results since
data then ends up (most likely,otherwise add a bit more) in a toast table.

Jesper

In response to

Browse pgsql-hackers by date

  From Date Subject
Next Message Magnus Hagander 2011-10-11 07:21:07 Re: Bug in walsender when calling out to do_pg_stop_backup (and others?)
Previous Message Jeff Davis 2011-10-11 05:25:18 Re: Range Types - typo + NULL string constructor