Re: Different execution plans for semantically equivalent queries

From: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>
To: Mikkel Lauritsen <renard(at)tala(dot)dk>
Cc: pgsql-performance(at)postgresql(dot)org
Subject: Re: Different execution plans for semantically equivalent queries
Date: 2011-02-06 21:29:01
Message-ID: 15642.1297027741@sss.pgh.pa.us
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-performance

Mikkel Lauritsen <renard(at)tala(dot)dk> writes:
> I would like to do a query which retrieves the newest record for each
> type, and the persistence framework that I'm using does something
> which is structurally like

> SELECT * FROM table t1 WHERE 0 = (SELECT COUNT(*) FROM table t2 WHERE
> t2.type = t1.type AND t2.timestamp > t1.timestamp)

I suspect that *any* database is going to have trouble optimizing that.
You'd be well advised to lobby the persistence framework's authors to
produce less brain-dead SQL. The NOT EXISTS formulation seems to
express what's wanted much less indirectly.

regards, tom lane

In response to

Responses

Browse pgsql-performance by date

  From Date Subject
Next Message Mikkel Lauritsen 2011-02-06 22:03:18 Re: Different execution plans for semantically equivalent queries
Previous Message Andy 2011-02-06 20:38:18 Re: general hardware advice