Re: [SQL] How to get last 10 rows in a table on a large database?

Поиск
Список
Период
Сортировка
От Tom Lane
Тема Re: [SQL] How to get last 10 rows in a table on a large database?
Дата
Msg-id 2514.941911947@sss.pgh.pa.us
обсуждение исходный текст
Ответ на How to get last 10 rows in a table on a large database?  (Alex@Icepick.com (Alex))
Ответы Re: [SQL] How to get last 10 rows in a table on a large database?  (Oleg Bartunov <oleg@sai.msu.su>)
Список pgsql-sql
Alex@Icepick.com (Alex) writes:
> Does anyone have a good idea to get the last 10 rows from a table? I
> tried 
> SELECT * FROM table ORDER BY datetime DESC LIMIT 10
> But my table has over 1 million rows, so this takes forever.. anyone
> has a better idea on how to do this? 

FWIW, that query should work the way you want in 7.0 (assuming you have
an index on datetime).  6.5.* isn't bright enough to use an index for
ORDER BY DESC, though.  I think Stoyan's suggestion of declaring a
cursor is probably the best workaround for now --- or you could consult
the patches list archives for the patch that will make 6.5 use an index.
        regards, tom lane


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

Предыдущее
От: "Frederic boucher"
Дата:
Сообщение: Re: [SQL] Accessing other database...
Следующее
От: Tom Lane
Дата:
Сообщение: Re: [SQL] How to get last 10 rows in a table on a large database?