Re: LIMIT: does it cause the query to find all matching sets - Mailing list pgsql-general

From Bruce Momjian
Subject Re: LIMIT: does it cause the query to find all matching sets
Date
Msg-id 200209301508.g8UF82615027@candle.pha.pa.us
Whole thread Raw
In response to LIMIT: does it cause the query to find all matching sets first?  (Jean-Christian Imbeault <jc@mega-bucks.co.jp>)
List pgsql-general
Jean-Christian Imbeault wrote:
> Does using LIMIT cause query execution to stop as soon as the LIMIT
> number of matches have been found, or are *all* the matching rows found
> first and then the first LIMIT number are returned?
>
> Of course this would be for a query without and ORDER BY clause.

A query without ORDER BY is possible, and it will stop after it hits the
limit.  Of course, without ORDER BY there is no way to know which rows
were returned.

Second, if you do use ORDER BY, LIMIT can sometimes use an index and not
have to execute the entire query, so yes, that optimization is in there.

--
  Bruce Momjian                        |  http://candle.pha.pa.us
  pgman@candle.pha.pa.us               |  (610) 359-1001
  +  If your life is a hard drive,     |  13 Roberts Road
  +  Christ can be your backup.        |  Newtown Square, Pennsylvania 19073

pgsql-general by date:

Previous
From: Tom Lane
Date:
Subject: Re: LIMIT: does it cause the query to find all matching sets first?
Next
From: Adam Witney
Date:
Subject: Re: Beta2 - A Late Announcement