Re: Getting row with id=max(id)

From: Peter Eisentraut <peter_e(at)gmx(dot)net>
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-07 17:31:34
Message-ID: Pine.LNX.4.30.0106071929140.757-100000@peter.localdomain
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-sql

Gerald Gutierrez writes:

> 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?

One of these two. ;-)

The second is generally thought to be faster, at least if you use the
latest version of PostgreSQL.

> A related question is: is there a way to time a query in psql, like the
> client of MySQL does?

Not in a built-in way.

--
Peter Eisentraut peter_e(at)gmx(dot)net http://funkturm.homeip.net/~peter

In response to

Responses

Browse pgsql-sql by date

  From Date Subject
Next Message Stephan Szabo 2001-06-07 17:39:08 Re: Are SQL commands "atomic" ?
Previous Message Laurent Patureau 2001-06-07 17:14:18 UPDATE with concatenate