pg.ddx.io  pgsql-sql@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Gerald Gutierrez <pozix@home.com>
To: pgsql-sql@postgresql.org
Subject: Getting row with id=max(id)
Date: Wed, 06 Jun 2001 23:20:28 -0700
Message-ID: <5.1.0.14.0.20010606231409.02b92168@mail.rchmd1.bc.wave.home.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?




view thread (8+ messages)  latest in thread

Message-ID: <5.1.0.14.0.20010606231409.02b92168@mail.rchmd1.bc.wave.home.com>
Permalink:  ../5.1.0.14.0.20010606231409.02b92168@mail.rchmd1.bc.wave.home.com/
Also on:    postgresql.org/message-id/5.1.0.14.0.20010606231409.02b92168@mail.rchmd1.bc.wave.home.com

 · 

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 from your mail client; the link fills in
  the headers below: reply via email
  Some clients, webmail especially, drop In-Reply-To and References from
  a link, and the reply then starts a new thread: use the mbox below.

  To: pgsql-sql@postgresql.org
  Cc: pozix@home.com
  Subject: Re: Getting row with id=max(id)
  In-Reply-To: <5.1.0.14.0.20010606231409.02b92168@mail.rchmd1.bc.wave.home.com>
  References: <5.1.0.14.0.20010606231409.02b92168@mail.rchmd1.bc.wave.home.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