From: tomas@nocrew.org
To: pgsql-performance@postgresql.org
Subject: Re: [GENERAL] Query is not using index when it should
Date: 12 Dec 2004 09:32:25 +0100
Message-ID: <806537udba.fsf@junk.nocrew.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>
"Steinar H. Gunderson" <sgunderson@bigfoot.com> writes:
> 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.
Well, my subquery doesn't return just one row, but one for each
objectid,field combination in the table. I could rewrite it to
something like this:
select c.* from cjm_object c
where exists (select timestamp from cjm_object t
where c.objectid=t.objectid
and c.field=t.field
order by timestamp desc limit 1)
and data is not null
order by objectid;
But that seems to be even slower, even if it can use an index scan in
the subquery. Also it doesn't give the same result set, but I haven't
looked into what's wrong yet.
> I don't see the point of "where 1=1", though...
It's just because the actual query is generated by a program, and it's
easier to always have "where 1=1" and then add optional conditions
with "and ...".
Tomas
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: tomas@nocrew.org, pgsql-performance@postgresql.org
Subject: Re: [GENERAL] Query is not using index when it should
In-Reply-To: <806537udba.fsf@junk.nocrew.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