pg.ddx.io  pgsql-sql@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Tom Lane <tgl@sss.pgh.pa.us>
To: Gerald Gutierrez <gml1@coldresist.com>
Cc: pgsql-sql@postgresql.org
Subject: Re: Getting row with id=max(id)
Date: Fri, 08 Jun 2001 12:34:00 -0400
Message-ID: <10021.992018040@sss.pgh.pa.us> (raw)
In-Reply-To: <5.1.0.14.0.20010607100000.02b8ae58@coldresist.com>
References: <5.1.0.14.0.20010607100000.02b8ae58@coldresist.com>

Gerald Gutierrez <gml1@coldresist.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



view thread (8+ messages)

Message-ID: <10021.992018040@sss.pgh.pa.us>
Permalink:  ../10021.992018040@sss.pgh.pa.us/
Also on:    postgresql.org/message-id/10021.992018040@sss.pgh.pa.us

 · 

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 using the --to and --cc options:
  reply via email

  To: pgsql-sql@postgresql.org
  Cc: tgl@sss.pgh.pa.us, gml1@coldresist.com
  Subject: Re: Getting row with id=max(id)
  In-Reply-To: <10021.992018040@sss.pgh.pa.us>

* 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