Re: Getting row with id=max(id) - Mailing list pgsql-sql

From Peter Eisentraut
Subject Re: Getting row with id=max(id)
Date
Msg-id Pine.LNX.4.30.0106071929140.757-100000@peter.localdomain
Whole thread Raw
In response to Getting row with id=max(id)  (Gerald Gutierrez <gml1@coldresist.com>)
Responses Re: Getting row with id=max(id)
List 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@gmx.net   http://funkturm.homeip.net/~peter



pgsql-sql by date:

Previous
From: Laurent Patureau
Date:
Subject: UPDATE with concatenate
Next
From: Stephan Szabo
Date:
Subject: Re: Are SQL commands "atomic" ?