Re: Limit changes query plan

From: Gregory Stark <stark(at)enterprisedb(dot)com>
To: "Gaetano Mendola" <mendola(at)bigfoot(dot)com>
Cc: <pgsql-hackers(at)postgresql(dot)org>, "Martijn van Oosterhout" <kleptog(at)svana(dot)org>
Subject: Re: Limit changes query plan
Date: 2008-02-01 12:35:26
Message-ID: 87wspoam7l.fsf@oxford.xeocode.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-hackers

"Gaetano Mendola" <mendola(at)bigfoot(dot)com> writes:

> I don't get why a limit is going to change the query plan and most of all decreasing
> the performances.

Until we see the explain analyze it won't be clear what exactly is going on.
But in theory a LIMIT can definitely change the plan because the planner knows
it won't need to generate all the rows to satisfy the LIMIT.

In the plans you gave note that the plan for the unlimited query has a Sort so
it has to produce all the records every time. The second query produces the
records in order so if the LIMIT is satisfied quickly then it can save a lot
of work.

It's evidently guessing wrong about the limit being satisfied early. The
non-indexed restrictions might be pruning out a lot more records than the
planner expects. Or possibly the table is just full of dead records.

--
Gregory Stark
EnterpriseDB http://www.enterprisedb.com
Ask me about EnterpriseDB's PostGIS support!

In response to

Responses

Browse pgsql-hackers by date

  From Date Subject
Next Message Gaetano Mendola 2008-02-01 14:23:13 Re: Limit changes query plan
Previous Message Gaetano Mendola 2008-02-01 11:42:56 Re: Limit changes query plan