Getting row with id=max(id)

Поиск
Список
Период
Сортировка
От Gerald Gutierrez
Тема Getting row with id=max(id)
Дата
Msg-id 5.1.0.14.0.20010607100000.02b8ae58@coldresist.com
обсуждение исходный текст
Ответы Re: Getting row with id=max(id)  (Peter Eisentraut <peter_e@gmx.net>)
Re: Getting row with id=max(id)  (Tom Lane <tgl@sss.pgh.pa.us>)
Re: Getting row with id=max(id)  (<jeff@pgsql.com>)
Список pgsql-sql
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? 



В списке pgsql-sql по дате отправления:

Предыдущее
От: Stephan Szabo
Дата:
Сообщение: Re: dropping constraints
Следующее
От: Peter Eisentraut
Дата:
Сообщение: Re: Re: About i8n