Re: suggestions on improving a query

From: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>
To: "Adam Rich" <adam(dot)r(at)sbcglobal(dot)net>
Cc: rguha(at)indiana(dot)edu, pgsql-general(at)postgresql(dot)org
Subject: Re: suggestions on improving a query
Date: 2007-02-14 03:04:52
Message-ID: 6279.1171422292@sss.pgh.pa.us
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

"Adam Rich" <adam(dot)r(at)sbcglobal(dot)net> writes:
> This line:
> Index Scan using plp_total_idx on dockscore_plp
> (cost=0.00..16733229.92 rows=4669988 width=80)
> (actual time=98.323..322537.605 rows=25197 loops=1)
> Means the planner did what it did, because it estimated there would be
> nearly 5 million rows. However, there were only 25,000.

No, you have to be careful about interpreting the numbers when
underneath a Limit node. The rows estimate is an estimate of the total
number of rows if the plan node were run to completion ... but if the
Limit stops execution early, that's not what will happen. The actual
rows count shows how many rows really got pulled from the node before
the Limit stopped things.

The real problem here is that the planner is guessing that it won't take
very long to find 10 rows satisfying the target = '1YC1' condition while
scanning in dockscore_plp.total order. So it chooses a plan that would
have a long total runtime (notice the large cost estimates below the
Limit) expecting that only a small fraction of that total will actually
be expended. The expectation seems a bit off unfortunately :-(.
I can't tell from the given data whether the problem is just an
overestimate of the frequency of target = '1YC1', or if there's an
additional effect. For example, if that target value tended to only be
associated with larger values of dockscore_plp.total, then a plan like
this could lose big-time because it will have to scan a long way to find
those rows.

regards, tom lane

In response to

Responses

Browse pgsql-general by date

  From Date Subject
Next Message Bruce Momjian 2007-02-14 04:32:17 Re: pg_get_serial_sequence is inconsistent
Previous Message Bruce Momjian 2007-02-14 02:46:05 Re: [GENERAL] ISO week dates