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, "Claudio Freire" <klaussfreire(at)gmail(dot)com>
Subject: Re: strange query plan with LIMIT
Date: 2011-06-08 09:47:43
Message-ID: c48d12d868ecc4b9ab6063f562650722.squirrel@sq.gransy.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-performance

> On Wednesday 08 June 2011 17:39, Claudio Freire wrote:
>> Of course optimally executing a plan with limit is a lot different
>> than one without.
>
> I imagined that limit just cuts out a slice of the query results.
> If it can find 80000 rows in 0.5 seconds then I would have thought that
> returning just the first 100 of them should be just as easy.

But that's exactly the problem with LIMIT clause. The planner considers
two choices - index scan with this estimate

Index Scan using tdiag_pkey on tdiag (cost=0.00..19114765.76
rows=1141019 width=114)

and bitmap index scan with this estimate

Bitmap Heap Scan on tdiag (cost=25763.48..638085.13 rows=1141019
width=114)

and says - hey, the index scan has much lower starting cost, and I'm using
limit so it's much better! Let's use index scan. But then it finds out it
needs to scan most of the table and that ruins the performance.

Have you tried to create a composite index on those two columns? Not sure
if that helps but I'd try that.

Tomas

In response to

Responses

Browse pgsql-performance by date

  From Date Subject
Next Message Pavel Stehule 2011-06-08 09:58:04 Re: strange query plan with LIMIT
Previous Message anthony.shipman 2011-06-08 09:36:31 Re: strange query plan with LIMIT