Re: strange query plan with LIMIT

From: tv(at)fuzzy(dot)cz
To: anthony(dot)shipman(at)symstream(dot)com
Cc: pgsql-performance(at)postgresql(dot)org, tv(at)fuzzy(dot)cz
Subject: Re: strange query plan with LIMIT
Date: 2011-06-08 08:33:15
Message-ID: 22bab989ef6b6b073db4cecca0a19dd7.squirrel@sq.gransy.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-performance

> What seems odd to me is that the only difference between the two is the
> limit
> clause:
>
> select * from tdiag where (create_time >= '2011-06-03
> 09:49:04.000000+0' and create_time < '2011-06-06 09:59:04.000000+0') order
> by
> diag_id limit 1;
>
> select * from tdiag where (create_time >= '2011-06-03
> 09:49:04.000000+0' and create_time < '2011-06-06 09:59:04.000000+0') order
> by
> diag_id;
>
> and yet the plan completely changes.

As Claudio Freire already pointed out, this is expected behavior. With
LIMIT the planner prefers plans with low starting cost, as it expects to
end soon and building index bitmap / hash table would be a waste. So
actually it would be very odd if the plan did not change in this case ...

Anyway I have no idea how to fix this "clean" - without messing with
enable_* or cost variables or other such dirty tricks.

regards
Tomas

In response to

Browse pgsql-performance by date

  From Date Subject
Next Message anthony.shipman 2011-06-08 08:34:07 Re: strange query plan with LIMIT
Previous Message Claudio Freire 2011-06-08 07:39:00 Re: strange query plan with LIMIT