Query Plan choice with timestamps

From: Giorgio Valoti <giorgio_v(at)mac(dot)com>
To: pgsql-performance(at)postgresql(dot)org
Subject: Query Plan choice with timestamps
Date: 2008-08-07 07:50:04
Message-ID: 321506D5-4400-470C-909E-C7700D49A540@mac.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-performance

Hi, I have a timestamptz field that I want to use with a query, but I
don’t need the full timestamp resolution, so I’ve created a
day_trunc(timestamptz) immutable function which I’ll use with the
query and with a new index:

logs=> create index test_idx on blackbox (day_trunc(ts));

However, the query plan doesn’t use the index:

logs=>explain select count(*) from blackbox group by day_trunc(ts)
order by day_trunc(ts);
QUERY PLAN
------------------------------------------------------------------------------------------
GroupAggregate (cost=98431.58..119773.92 rows=74226 width=8)
-> Sort (cost=98431.58..99050.92 rows=247736 width=8)
Sort Key: (day_trunc(ts))
-> Seq Scan on blackbox (cost=0.00..72848.36 rows=247736
width=8)
(4 rows)

while with this index:

logs=>create index test_2_idx on blackbox (ts);

the query plan is the expected one:

logs=>explain select count(*) from blackbox group by ts order by ts;
QUERY PLAN
------------------------------------------------------------------------------------------
GroupAggregate (cost=0.00..19109.66 rows=74226 width=8)
-> Index Scan using test_2_idx on blackbox (cost=0.00..16943.16
rows=247736 width=8)

But I fail to see why. Any hints?

Thank you in advance
--
Giorgio Valoti

Responses

Browse pgsql-performance by date

  From Date Subject
Next Message Richard Huxton 2008-08-07 07:58:00 Re: Unexpectedly Long DELETE Wait
Previous Message dforum 2008-08-07 07:35:17 Re: Plz Heeeelp! performance settings