Re: Getting row with id=max(id)

From: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>
To: Gerald Gutierrez <gml1(at)coldresist(dot)com>
Cc: pgsql-sql(at)postgresql(dot)org
Subject: Re: Getting row with id=max(id)
Date: 2001-06-08 16:34:00
Message-ID: 10021.992018040@sss.pgh.pa.us
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-sql

Gerald Gutierrez <gml1(at)coldresist(dot)com> writes:
> I'd like to get the row with ID=4. I've tried:
> SELECT * FROM mytable WHERE id=(SELECT MAX(id) FROM mytable);
> The subquery can take a /really/ long time on a table that is large. The query:
> SELECT * FROM mytable ORDER BY id DESC LIMIT 1;
> doesn't seem to help very much.

It should help a lot, if you have an index on id. Have you vacuum
analyzed the table recently?

regards, tom lane

In response to

Browse pgsql-sql by date

  From Date Subject
Next Message Josh Berkus 2001-06-08 16:53:16 Re: [HACKERS] Re: behavior of ' = NULL' vs. MySQL vs. Standards
Previous Message Peter Eisentraut 2001-06-08 16:31:57 Re: Hidden Select