pg.ddx.io  pgsql-sql@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Michael Fuhr <mike@fuhr.org>
To: pgsql-performance@postgresql.org
Subject: Re: [GENERAL] Query is not using index when it should
Date: Sat, 11 Dec 2004 09:25:39 -0700
Message-ID: <20041211162539.GA66539@winnie.fuhr.org> (raw)
In-Reply-To: <20041211143213.GA2659@uio.no>
References: <80oeh2tm7x.fsf@junk.nocrew.org>
	<20041210182453.G92467@megazone.bigpanda.com>
	<80d5xgudg6.fsf@junk.nocrew.org>
	<20041211143213.GA2659@uio.no>

On Sat, Dec 11, 2004 at 03:32:13PM +0100, Steinar H. Gunderson wrote:
> On Sat, Dec 11, 2004 at 03:17:13PM +0100, Tomas Skäre wrote:
> > select c.* from cjm_object c
> >  inner join
> >   (select max(timestamp) as timestamp,objectid,field from cjm_object
> >    group by objectid,field) t
> >   using(timestamp,objectid,field)
> >  where 1=1 and data is not null
> >  order by objectid,field;
> 
> Usually, SELECT max(field) FROM table is better written in PostgreSQL as
> SELECT field FROM table ORDER field DESC LIMIT 1.
> 
> I don't see the point of "where 1=1", though...

I've seen that in generated queries.  The generating program uses
"WHERE 1=1" to simplify the addition of other conditions: instead
of checking if it needs to add a WHERE and putting ANDs in the right
places, it simply appends subsequent conditions with " AND condition".

-- 
Michael Fuhr
http://www.fuhr.org/~mfuhr/



view thread (7+ messages)  latest in thread

Message-ID: <20041211162539.GA66539@winnie.fuhr.org>
Permalink:  ../20041211162539.GA66539@winnie.fuhr.org/
Also on:    postgresql.org/message-id/20041211162539.GA66539@winnie.fuhr.org

 · 

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 using the --to and --cc options:
  reply via email

  To: pgsql-sql@postgresql.org
  Cc: mike@fuhr.org, pgsql-performance@postgresql.org
  Subject: Re: [GENERAL] Query is not using index when it should
  In-Reply-To: <20041211162539.GA66539@winnie.fuhr.org>

* 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