Re: BUG #1393: Adding 'LIMIT 1' to the query halts forever

From: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>
To: Michael Fuhr <mike(at)fuhr(dot)org>
Cc: "Fahad G(dot)" <Fahad(dot)Gilani(at)anusf(dot)anu(dot)edu(dot)au>, pgsql-bugs(at)postgresql(dot)org
Subject: Re: BUG #1393: Adding 'LIMIT 1' to the query halts forever
Date: 2005-01-16 19:56:11
Message-ID: 19205.1105905371@sss.pgh.pa.us
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-bugs

Michael Fuhr <mike(at)fuhr(dot)org> writes:
> Limit (cost=0.00..25.79 rows=1 width=8) (actual time=631.964..631.964 rows=0 loops=1)
> -> Index Scan using foo_value_idx on foo (cost=0.00..2552.75 rows=99 width=8) (actual time=631.942..631.942 rows=0 loops=1)
> Filter: (id = -1)
> Total runtime: 632.135 ms
> (4 rows)

> Maybe I don't understand something about what EXPLAIN is showing,
> but why does Limit have an estimated cost of 0.00..25.79 when the
> thing it's limiting has a cost of 0.00..2552.75?

This represents the planner assuming that the indexscan will only need
to be run 1/99th of the way to completion. That is, having estimated
that there were 99 matching rows to be found, it assumes those are
uniformly distributed in the index-by-value, and that the scan can stop
as soon as the first one is found.

Since in reality there aren't *any* matching rows, the index scan has to
go all the way to the end :-(. Even if there were matching rows, they
might be much further out in the index order than the
uniform-distribution hypothesis predicts, because the id and value
columns might have been correlated.

Basically, what you're looking at here is that the planner is thinking
it should go for a fast-start plan in a scenario where that bet loses.
It's still a good bet though. I'm not sure how to formulate the notion
that there's too much risk of a slow result in this scenario.

regards, tom lane

In response to

Responses

Browse pgsql-bugs by date

  From Date Subject
Next Message Luiz Gonzaga da Mata 2005-01-16 20:04:57 Bug in check constraint?
Previous Message Michael Fuhr 2005-01-16 19:38:25 Re: BUG #1393: Adding 'LIMIT 1' to the query halts forever