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

From: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>
To: Alex(at)Icepick(dot)com
Cc: pgsql-sql(at)postgreSQL(dot)org
Subject: Re: [SQL] How to get last 10 rows in a table on a large database?
Date: 1999-11-06 18:12:27
Message-ID: 2514.941911947@sss.pgh.pa.us
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-sql

Alex(at)Icepick(dot)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

Responses

Browse pgsql-sql by date

  From Date Subject
Next Message Tom Lane 1999-11-06 18:16:58 Re: [SQL] How to get last 10 rows in a table on a large database?
Previous Message Bruce Momjian 1999-11-06 16:45:10 Re: [SQL] pg_dump and "archive = none" ??8