Re: [SQL] Limit rows - Mailing list pgsql-sql

From Tom Lane
Subject Re: [SQL] Limit rows
Date
Msg-id 5408.938648218@sss.pgh.pa.us
Whole thread Raw
In response to Re: [SQL] Limit rows  (Mark Jewiss <Mark.Jewiss@knowledge.com>)
List pgsql-sql
Mark Jewiss <Mark.Jewiss@knowledge.com> writes:
> [ re LIMIT ]
> I'm interested now in how this method actually works - is a query
> performed that reads all of the rows of a table into memory, and then the
> non-requested rows are discarded before the results are sent back?

> I'm interested in how this would work with a massive table......

The executor will stop generating rows as soon as it's satisfied the
limit+offset.  Whether that's actually quick depends on your query;
for example, if you do something that requires an explicit sort step,
the full sort has to be done anyway (since there's no way to tell
which rows it'd return first without finishing the sort...).  You can
use EXPLAIN if you're not sure whether a query will use a sort.

Also, a large offset and a small limit might not be as fast as you'd
like, since the rows discarded by OFFSET will be generated and then
dropped.  So, you should not consider this feature as a substitute for a
cursor.  If you intend to fetch most of a table a little bit at a time,
you want to use DECLARE CURSOR and FETCH.
        regards, tom lane


pgsql-sql by date:

Previous
From: Tom Lane
Date:
Subject: Re: [SQL] Date: Wed, 29 Sep 1999 23:49:15 +0300
Next
From: Chairudin Sentosa Harjo
Date:
Subject: Constraint Problem