LIMIT Question

Lists: pgsql-general
From: Terry Lee Tucker <terry(at)chosen-ones(dot)org>
To: pgsql-general(at)postgresql(dot)org
Subject: LIMIT Question
Date: 2008-02-28 21:20:56
Message-ID: 200802281620.56739.terry@chosen-ones.org
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-general

When one uses LIMIT, as in LIMIT 1, is the entire query executed on the server
side, but only one record returned?

--------------------------------------------------------------------------------------------------------
PostgreSQL 7.4.19 on i686-redhat-linux-gnu, compiled by GCC gcc (GCC) 3.4.6
20060404 (Red Hat 3.4.6-9)

Thanks...
--
Terry Lee Tucker
Turbo's IT Manager
Turbo, division of Ozburn-Hessey Logistics
2251 Jesse Jewell Pkwy NE
Gainesville, GA 30501
Tel: (336) 372-6812 Fax: (336) 372-6812 Cell: (336) 404-6987
terry(at)turbocorp(dot)com
www.turbocorp.com


From: "Scott Marlowe" <scott(dot)marlowe(at)gmail(dot)com>
To: "Terry Lee Tucker" <terry(at)chosen-ones(dot)org>
Cc: pgsql-general(at)postgresql(dot)org
Subject: Re: LIMIT Question
Date: 2008-02-28 23:32:52
Message-ID: dcc563d10802281532n3da2ee0dlc2114f23af8136b8@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-general

On Thu, Feb 28, 2008 at 3:20 PM, Terry Lee Tucker <terry(at)chosen-ones(dot)org> wrote:
> When one uses LIMIT, as in LIMIT 1, is the entire query executed on the server
> side, but only one record returned?
>
> --------------------------------------------------------------------------------------------------------
> PostgreSQL 7.4.19 on i686-redhat-linux-gnu, compiled by GCC gcc (GCC) 3.4.6
> 20060404 (Red Hat 3.4.6-9)

Sometimes yes, sometimes no. Depends on how complex the query is and
whether or not pgsql's query planner can see a shortcut or not. It's
more likely that a later version will have the optimizations needed to
do that than an older version like 7.4 I'd think. But I'd ask someone
more expert on the planner like Tom to be sure.


From: "Gurjeet Singh" <singh(dot)gurjeet(at)gmail(dot)com>
To: "Scott Marlowe" <scott(dot)marlowe(at)gmail(dot)com>
Cc: "Terry Lee Tucker" <terry(at)chosen-ones(dot)org>, pgsql-general(at)postgresql(dot)org
Subject: Re: LIMIT Question
Date: 2008-02-29 09:23:05
Message-ID: 65937bea0802290123y2921c1cfg528a1affe7367a74@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-general

In my opinion (without looking at the code), if you have a grouping-function
or ORDER BY or GROUP BY clause, then yes, the whole query has to be executed
to show the first row of the result-set. But if the query doesn't have any
of these clauses, then the DB has the ability to send back the first row
from the result as soon as it processes it (i.e after WHERE clause
processing), and stop the query execution there.

Best regards,

On Fri, Feb 29, 2008 at 5:02 AM, Scott Marlowe <scott(dot)marlowe(at)gmail(dot)com>
wrote:

> On Thu, Feb 28, 2008 at 3:20 PM, Terry Lee Tucker <terry(at)chosen-ones(dot)org>
> wrote:
> > When one uses LIMIT, as in LIMIT 1, is the entire query executed on the
> server
> > side, but only one record returned?
> >
> >
> --------------------------------------------------------------------------------------------------------
> > PostgreSQL 7.4.19 on i686-redhat-linux-gnu, compiled by GCC gcc (GCC)
> 3.4.6
> > 20060404 (Red Hat 3.4.6-9)
>
> Sometimes yes, sometimes no. Depends on how complex the query is and
> whether or not pgsql's query planner can see a shortcut or not. It's
> more likely that a later version will have the optimizations needed to
> do that than an older version like 7.4 I'd think. But I'd ask someone
> more expert on the planner like Tom to be sure.
>
> ---------------------------(end of broadcast)---------------------------
> TIP 4: Have you searched our list archives?
>
> http://archives.postgresql.org/
>

--
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


From: Martijn van Oosterhout <kleptog(at)svana(dot)org>
To: Gurjeet Singh <singh(dot)gurjeet(at)gmail(dot)com>
Cc: Scott Marlowe <scott(dot)marlowe(at)gmail(dot)com>, Terry Lee Tucker <terry(at)chosen-ones(dot)org>, pgsql-general(at)postgresql(dot)org
Subject: Re: LIMIT Question
Date: 2008-02-29 10:25:41
Message-ID: 20080229102541.GA28668@svana.org
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-general

On Fri, Feb 29, 2008 at 02:53:05PM +0530, Gurjeet Singh wrote:
> In my opinion (without looking at the code), if you have a grouping-function
> or ORDER BY or GROUP BY clause, then yes, the whole query has to be executed
> to show the first row of the result-set. But if the query doesn't have any
> of these clauses, then the DB has the ability to send back the first row
> from the result as soon as it processes it (i.e after WHERE clause
> processing), and stop the query execution there.

Except if you have an index on the column you're ordering by. Then the
server can really return the first row quickly.

Have a nice day,
--
Martijn van Oosterhout <kleptog(at)svana(dot)org> http://svana.org/kleptog/
> Those who make peaceful revolution impossible will make violent revolution inevitable.
> -- John F Kennedy


From: "Gurjeet Singh" <singh(dot)gurjeet(at)gmail(dot)com>
To: "Martijn van Oosterhout" <kleptog(at)svana(dot)org>, "Gurjeet Singh" <singh(dot)gurjeet(at)gmail(dot)com>, "Scott Marlowe" <scott(dot)marlowe(at)gmail(dot)com>, "Terry Lee Tucker" <terry(at)chosen-ones(dot)org>, pgsql-general(at)postgresql(dot)org
Subject: Re: LIMIT Question
Date: 2008-02-29 11:58:29
Message-ID: 65937bea0802290358m64976999m2d0c7c428eeadbea@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-general

On Fri, Feb 29, 2008 at 3:55 PM, Martijn van Oosterhout <kleptog(at)svana(dot)org>
wrote:

> On Fri, Feb 29, 2008 at 02:53:05PM +0530, Gurjeet Singh wrote:
> > In my opinion (without looking at the code), if you have a
> grouping-function
> > or ORDER BY or GROUP BY clause, then yes, the whole query has to be
> executed
> > to show the first row of the result-set. But if the query doesn't have
> any
> > of these clauses, then the DB has the ability to send back the first row
> > from the result as soon as it processes it (i.e after WHERE clause
> > processing), and stop the query execution there.
>
> 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'. I think it will still process all the rows and return just one.

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


From: Sam Mason <sam(at)samason(dot)me(dot)uk>
To: pgsql-general(at)postgresql(dot)org
Subject: Re: LIMIT Question
Date: 2008-02-29 13:50:28
Message-ID: 20080229135027.GM1653@frubble.xen.chris-lamb.co.uk
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-general

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.

Sam


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
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