Re: Limit changes query plan - Mailing list pgsql-hackers

From Gregory Stark
Subject Re: Limit changes query plan
Date
Msg-id 87wspoam7l.fsf@oxford.xeocode.com
Whole thread Raw
In response to Re: Limit changes query plan  (Gaetano Mendola <mendola@bigfoot.com>)
Responses Re: Limit changes query plan
List pgsql-hackers
"Gaetano Mendola" <mendola@bigfoot.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!


pgsql-hackers by date:

Previous
From: Gaetano Mendola
Date:
Subject: Re: Limit changes query plan
Next
From: Gaetano Mendola
Date:
Subject: Re: Limit changes query plan