adding 'limit' leads to very slow query

From: Michael McFarland
Subject: adding 'limit' leads to very slow query
Date: ,
Msg-id: opsnaj0hctsvs6tg@localhost.localdomain
(view: Whole thread, Raw)
Responses: Re: adding 'limit' leads to very slow query  (John A Meinel)
Re: adding 'limit' leads to very slow query  (Stephan Szabo)
List: pgsql-performance

Tree view

adding 'limit' leads to very slow query  ("Michael McFarland", )
 Re: adding 'limit' leads to very slow query  (John A Meinel, )
 Re: adding 'limit' leads to very slow query  (Stephan Szabo, )
 Re: adding 'limit' leads to very slow query  ("Michael McFarland", )
  Re: adding 'limit' leads to very slow query  (Stephan Szabo, )

   I'm trying to understand why a particular query is slow, and it seems
like the optimizer is choosing a strange plan.  See this summary:


* I have a large table, with an index on the primary key 'id' and on a
field 'foo'.
> select count(*) from foo;
1,000,000
> select count(*) from foo where bar = 41;
7

* This query happens very quickly.
> explain select * from foo where barId = 412 order by id desc;
Sort ()
   Sort key= id
  ->   Index scan using bar_index on foo ()
     Index cond: barId = 412

But this query takes forever

> explain select * from foo where barId = 412 order by id desc limit 25;
Limit ()
  ->   Index scan backward using primarykey_index
   Filter:  barID = 412


Could anyone shed some light on what might be happening here?

  - Michael


--
Using Opera's revolutionary e-mail client: http://www.opera.com/mail/


pgsql-performance by date:

From: "Magnus Hagander"
Date:
Subject: Re: [pgsql-hackers-win32] Help with tuning this query (with
From: Gaetano Mendola
Date:
Subject: bad plan