Received: from sss.pgh.pa.us ([192.204.191.242]) by postgresql.org (8.11.3/8.11.1) with ESMTP id f58GY5E67403 for ; Fri, 8 Jun 2001 12:34:06 -0400 (EDT) (envelope-from tgl@sss.pgh.pa.us) Received: from sss2.sss.pgh.pa.us (tgl@localhost [127.0.0.1]) by sss.pgh.pa.us (8.11.3/8.11.3) with ESMTP id f58GY1R10024; Fri, 8 Jun 2001 12:34:01 -0400 (EDT) To: Gerald Gutierrez cc: pgsql-sql@postgresql.org Subject: Re: Getting row with id=max(id) In-reply-to: <5.1.0.14.0.20010607100000.02b8ae58@coldresist.com> References: <5.1.0.14.0.20010607100000.02b8ae58@coldresist.com> Comments: In-reply-to Gerald Gutierrez message dated "Thu, 07 Jun 2001 10:00:02 -0700" Date: Fri, 08 Jun 2001 12:34:00 -0400 Message-ID: <10021.992018040@sss.pgh.pa.us> From: Tom Lane X-Archive-Number: 200106/129 X-Sequence-Number: 3350 Gerald Gutierrez 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