From: Gerald Gutierrez <gml1@coldresist.com>
To: pgsql-sql@postgresql.org
Subject: Getting row with id=max(id)
Date: Thu, 07 Jun 2001 10:00:02 -0700
Message-ID: <5.1.0.14.0.20010607100000.02b8ae58@coldresist.com> (raw)
I'd like to retrieve a row of a table that has the maximum ID. For example,
with:
id | s
----+-------
1 | alpha
2 | beta
3 | gamma
4 | delta
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. What query is the fastest at getting this row?
A related question is: is there a way to time a query in psql, like the
client of MySQL does?
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: gml1@coldresist.com
Subject: Re: Getting row with id=max(id)
In-Reply-To: <5.1.0.14.0.20010607100000.02b8ae58@coldresist.com>
* 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