Re: [SQL] Limit rows

From: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>
To: Mark Jewiss <Mark(dot)Jewiss(at)knowledge(dot)com>
Cc: Oleg Bartunov <oleg(at)sai(dot)msu(dot)su>, pgsql-sql(at)postgresql(dot)org
Subject: Re: [SQL] Limit rows
Date: 1999-09-29 23:36:58
Message-ID: 5408.938648218@sss.pgh.pa.us
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-sql

Mark Jewiss <Mark(dot)Jewiss(at)knowledge(dot)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

Browse pgsql-sql by date

  From Date Subject
Next Message Chairudin Sentosa Harjo 1999-09-30 08:05:26 Constraint Problem
Previous Message Tom Lane 1999-09-29 23:24:26 Re: [SQL] Date: Wed, 29 Sep 1999 23:49:15 +0300