Re: [SQL] does LIMIT save time?

From: wieck(at)debis(dot)com (Jan Wieck)
To: lutzeb(at)aeccom(dot)com (Dirk Lutzebaeck)
Cc: pgsql-sql(at)postgreSQL(dot)org
Subject: Re: [SQL] does LIMIT save time?
Date: 1999-08-19 21:24:09
Message-ID: m11HZfN-0003kLC@orion.SAPserv.Hamburg.dsh.de
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-sql

>
>
> Hi,
>
> is using SELECT LIMIT faster than using SELECT without LIMIT on
> the same query? If yes, in what way? What is if I use ORDER in the
> SELECT clause?

First of all, LIMIT reduces the amount of data sent to the
client application. Thus it reduces network traffic (or
interprocess communication if run on the same system), and it
saves wasted memory in the client to buffer received results
that aren't wanted.

For the backend processing time it depends on the execution
plan generated by the optimizer if much or less. If the
query plan tells the executor that the result must be sorted
or grouped, then the entire result set must first get
collected and sorted before the correct portion of the result
set could be returned. Such a sort or group step is caused by
the ORDER or GROUP clauses. The optimizer has limited
capability to realize if the index he has choosen for a scan
will already present the data in the wanted order and then
suppress the sorting.

So in general, LIMIT itself could never hurt. ORDER can but
someone who cares for speed is normally willing to setup
appropriate multi key indices to prevent sort steps.

>
> Is it possible to get the *total* number of rows selected (ie. doing
> the query virtually without LIMIT) when using a LIMIT clause?

Absolutely not. In the optimal case (no sort step) the
executor aborts the table scans when reaching the limit. A
well placed LIMIT can dramatically reduce disk IO. Now look
at the main road and count the red cars passing by over the
day. After 10 minutes you stop - do you know how many cars
will have passed at midnight? The executor is exactly in that
situation - he stop's scanning so he can't know.

Jan

--

#======================================================================#
# It's easier to get forgiveness for being wrong than for being right. #
# Let's break this rule - forgive me. #
#========================================= wieck(at)debis(dot)com (Jan Wieck) #

In response to

Responses

Browse pgsql-sql by date

  From Date Subject
Next Message Hamid Khoshnevis 1999-08-19 21:28:58 RE: [SQL] Using intersect in subquery
Previous Message Mike Field 1999-08-19 21:11:15 multiple keyword search