Re: [SQL] indexes using datetime

From: Herouth Maoz <herouth(at)oumail(dot)openu(dot)ac(dot)il>
To: Michael Richards <miker(at)scifair(dot)acadiau(dot)ca>
Cc: pgsql-sql(at)postgreSQL(dot)org
Subject: Re: [SQL] indexes using datetime
Date: 1999-09-06 15:25:13
Message-ID: l03130306b3f98cc074df@[147.233.159.109]
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-sql

At 03:46 +0300 on 29/08/1999, Tom Lane wrote:

> Michael Richards <miker(at)scifair(dot)acadiau(dot)ca> writes:
> > explain select * from logins where logintime>'now'::datetime-'40
> > days'::timespan;
> > NOTICE: QUERY PLAN:
> > Seq Scan on logins (cost=5839.78 rows=44958 width=44)
>
> > Very bad query plan :(
>
> Yah. It's got nothing to do with datetime though. Problem is that the
> system can only make indexscans work with WHERE clauses of the form
> "field op constant" (for "op"s related to the sort ordering of the
> index of course). Your righthand side is not a constant.

A tip: use

SELECT 'now'::datetime - '40 days'::timespan;

Get the result on your client side, say in a variable named $x, and use it
in the "real" query:

SELECT * FROM logins WHERE logintime > $x;

(Actual embedding protocol depends, of course, on the language you use).

Then it's a constant, as far as Postgres is concerned, and the time wasted
for the small query is really not an issue.

Herouth

--
Herouth Maoz, Internet developer.
Open University of Israel - Telem project
http://telem.openu.ac.il/~herutma

In response to

Browse pgsql-sql by date

  From Date Subject
Next Message Herouth Maoz 1999-09-06 15:36:36 Re: [SQL] SELECT multiple tables with same fields
Previous Message Eric Enockson 1999-09-06 14:58:48 creating indexes on character fields