From: Scott Marlowe <smarlowe@qwest.net>
To: Jack Kerkhof <jack.kerkhof@guest-tek.com>
Cc: pgsql-performance@postgresql.org
Subject: Re: Why does a simple query not use an obvious index?
Date: Sun, 29 Aug 2004 12:28:48 -0600
Message-ID: <1093804128.5493.22.camel@localhost.localdomain> (raw)
In-Reply-To: <ENEIKKAEJOFHOIHMMOIDIEBMJCAA.jack.kerkhof@guest-tek.com>
References: <ENEIKKAEJOFHOIHMMOIDIEBMJCAA.jack.kerkhof@guest-tek.com>
On Fri, 2004-08-27 at 11:12, Jack Kerkhof wrote:
> The query:
>
> select count(*) from billing where timestamp > now()-60
>
> should obviously use the index
>
> CREATE INDEX billing_timestamp_idx ON billing USING btree
> ("timestamp" timestamp_ops);
>
> on a table with 1400000 rows.
>
> But it uses a Seq Scan. If I set enable_seqscan=no, it indicates a
> queryplan could not be calculated.
Have you tried this:
marlowe=> select now()-60;
ERROR: operator does not exist: timestamp with time zone - integer
HINT: No operator matches the given name and argument type(s). You may
need to add explicit type casts.
you likely need:
smarlowe=> select now()-'60 seconds'::interval;
?column?
-------------------------------
2004-08-29 12:25:38.249564-06
inside there.
Also, count(*) is likely to always generate a seq scan due to the way
aggregates are implemented currently in pgsql. you might want to try:
select somefield from sometable where timestampfield > now()-'60
seconds'::interval
and count the number of returned rows. If there's a lot, it won't be
any faster, if there's a few, it should be a win.
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 using the --to and --cc options:
reply via email
To: pgsql-performance@postgresql.org
Cc: smarlowe@qwest.net, jack.kerkhof@guest-tek.com
Subject: Re: Why does a simple query not use an obvious index?
In-Reply-To: <1093804128.5493.22.camel@localhost.localdomain>
* 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