pg.ddx.io  pgsql-sql@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: David Witham <davidw@unidial.com.au>
To: pgsql-sql@postgresql.org
Subject: Index question
Date: Fri, 13 Feb 2004 13:30:10 +1100
Message-ID: <CFA248776934FD43847E740E43C346D199DBE9@ozimelb03.ozicom.com> (raw)

Hi all,

I have a table with around 3M records in it and a few indexes on it. One of them is on the day column. I get 10-20K new records a day. After running ANALYSE in psql I tried the following queries:

buns=# explain select count(*) from cdr where day >= '20040127';
                            QUERY PLAN                            
------------------------------------------------------------------
 Aggregate  (cost=85596.50..85596.50 rows=1 width=0)
   ->  Seq Scan on cdr  (cost=0.00..85053.86 rows=217055 width=0)
         Filter: ("day" >= '2004-01-27'::date)
(3 rows)

buns=# explain select count(*) from cdr where day = '20040127'; 
                                   QUERY PLAN                                   
--------------------------------------------------------------------------------
 Aggregate  (cost=12950.10..12950.10 rows=1 width=0)
   ->  Index Scan using cdr_ix1 on cdr  (cost=0.00..12928.00 rows=8839 width=0)
         Index Cond: ("day" = '2004-01-27'::date)
(3 rows)

buns=# explain select count(*) from cdr where day between '20040127' and current_date;
                                       QUERY PLAN                                       
----------------------------------------------------------------------------------------
 Aggregate  (cost=20129.91..20129.91 rows=1 width=0)
   ->  Index Scan using cdr_ix1 on cdr  (cost=0.00..20095.66 rows=13699 width=0)
         Index Cond: (("day" >= '2004-01-27'::date) AND ("day" <= ('now'::text)::date))
(3 rows)

I understand that selecting count(*) will involve a scan at some stage, but I was surprised that the index wasn't used in the >= case, but was used in the between case.

Why is this so? Do I need to ANALYSE some more or is this just the way the query planner works?

Thanks,
David

David Witham
Telephony Platforms Architect
Unidial
Ph: 03 8628 3383
Fax: 03 8628 3399




view thread (5+ messages)  latest in thread

Message-ID: <CFA248776934FD43847E740E43C346D199DBE9@ozimelb03.ozicom.com>
Permalink:  ../CFA248776934FD43847E740E43C346D199DBE9@ozimelb03.ozicom.com/
Also on:    postgresql.org/message-id/CFA248776934FD43847E740E43C346D199DBE9@ozimelb03.ozicom.com

 · 

reply

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Reply to all the recipients from your mail client; the link fills in
  the headers below: reply via email
  Some clients, webmail especially, drop In-Reply-To and References from
  a link, and the reply then starts a new thread: use the mbox below.

  To: pgsql-sql@postgresql.org
  Cc: davidw@unidial.com.au
  Subject: Re: Index question
  In-Reply-To: <CFA248776934FD43847E740E43C346D199DBE9@ozimelb03.ozicom.com>
  References: <CFA248776934FD43847E740E43C346D199DBE9@ozimelb03.ozicom.com>

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox