Re: LIMIT Question

From: "Gurjeet Singh" <singh(dot)gurjeet(at)gmail(dot)com>
To: "Sam Mason" <sam(at)samason(dot)me(dot)uk>
Cc: pgsql-general(at)postgresql(dot)org
Subject: Re: LIMIT Question
Date: 2008-02-29 14:56:43
Message-ID: 65937bea0802290656r3df32808t36b1bc7cd28333e6@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

On Fri, Feb 29, 2008 at 7:20 PM, Sam Mason <sam(at)samason(dot)me(dot)uk> wrote:

> On Fri, Feb 29, 2008 at 05:28:29PM +0530, Gurjeet Singh wrote:
> > On Fri, Feb 29, 2008 at 3:55 PM, Martijn van Oosterhout <
> kleptog(at)svana(dot)org> wrote:
> > > Except if you have an index on the column you're ordering by. Then the
> > > server can really return the first row quickly.
> >
> > Quickly for sure... but I don't think 'without processing all the rows
> that
> > qualify'.
>
> Postgres will always try to do the smallest amount of work possible.
> Putting a LIMIT in will cause the planner to pick a plan that returns an
> appropriate number of rows quickly, sometimes it can be a very different
> plan.
>
> > I think it will still process all the rows and return just one.
>
> How do you explain that when you run:
>
> SELECT 1/v FROM (VALUES (1),(0)) c(v);
>
> Without a limit you get a "division by zero" exception, and when you
> have a limit of one row you get a result back.
>

You are correct, and repeating what I said in the first mail. Your query
does not involve an ORDER BY (or other clauses), hence the first row that
the executor encounters, satisfies what the query asked for. But if it had
an ORDER BY you will again get the division ERROR.

Above I was referring to the case where the planner chooses an Index access.

Best regards,
--
gurjeet[(dot)singh](at)EnterpriseDB(dot)com
singh(dot)gurjeet(at){ gmail | hotmail | indiatimes | yahoo }.com

EnterpriseDB http://www.enterprisedb.com

17° 29' 34.37"N, 78° 30' 59.76"E - Hyderabad
18° 32' 57.25"N, 73° 56' 25.42"E - Pune *
37° 47' 19.72"N, 122° 24' 1.69" W - San Francisco

http://gurjeet.frihost.net

Mail sent from my BlackLaptop device

In response to

Browse pgsql-general by date

  From Date Subject
Next Message Scara Maccai 2008-02-29 15:04:02 Re: partitioning using dblink
Previous Message Scott Marlowe 2008-02-29 14:15:45 Re: rule question