Re: loading only few rows from huge table

From: Bruno Wolff III <bruno(at)wolff(dot)to>
To: Mark <sendmailtomark(at)yahoo(dot)com>
Cc: pgsql-general(at)postgresql(dot)org
Subject: Re: loading only few rows from huge table
Date: 2004-12-24 17:41:38
Message-ID: 20041224174138.GB8243@wolff.to
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

On Wed, Dec 22, 2004 at 11:31:59 -0800,
Mark <sendmailtomark(at)yahoo(dot)com> wrote:
> Hi,
> I have a table with 100K rows. One of columns is a timestamp and
> indicates when this row inserted.
>
> What will the the best way of getting 10 latest rows from that table
> and introducing partial data retrieval (rows 50-60, 100- 120, etc)

If there is an index on the timestamp column you can efficiently get
the 10 rows with the latest timestamps by using ORDER BY and LIMIT.
Getting rows out of the middle of the table by row number efficiently is
going to be harder. You can use OFFSET to get groups of records starting
from number other than the latest. As the offset gets bigger this will
run slower. If the timestamps are unique and you always continue from
where you left off and you can save the last timestamp you retrieved, then
you can use a WHERE clause to get the offset efficiently.

In response to

Responses

Browse pgsql-general by date

  From Date Subject
Next Message Wiebe de Jong 2004-12-24 18:27:14 looking for connection leaks
Previous Message Tom Lane 2004-12-24 14:33:39 Re: ISO_8859_8 encoding