Re: LIMIT: does it cause the query to find all matching sets

From: Bruce Momjian <pgman(at)candle(dot)pha(dot)pa(dot)us>
To: Jean-Christian Imbeault <jc(at)mega-bucks(dot)co(dot)jp>
Cc: pgsql-general <pgsql-general(at)postgresql(dot)org>
Subject: Re: LIMIT: does it cause the query to find all matching sets
Date: 2002-09-30 15:08:02
Message-ID: 200209301508.g8UF82615027@candle.pha.pa.us
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: 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(at)candle(dot)pha(dot)pa(dot)us | (610) 359-1001
+ If your life is a hard drive, | 13 Roberts Road
+ Christ can be your backup. | Newtown Square, Pennsylvania 19073

In response to

Browse pgsql-general by date

  From Date Subject
Next Message Adam Witney 2002-09-30 15:08:29 Re: Beta2 - A Late Announcement
Previous Message Tom Lane 2002-09-30 15:07:47 Re: LIMIT: does it cause the query to find all matching sets first?