Re: Why is the optimiser choosing the slower query, or, understanding explain analyze output

From: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>
To: Alistair Bayley <alistair(at)abayley(dot)org>
Cc: Jeff Janes <jeff(dot)janes(at)gmail(dot)com>, "pgsql-performance(at)postgresql(dot)org" <pgsql-performance(at)postgresql(dot)org>
Subject: Re: Why is the optimiser choosing the slower query, or, understanding explain analyze output
Date: 2014-02-18 02:30:14
Message-ID: 12233.1392690614@sss.pgh.pa.us
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-performance

Alistair Bayley <alistair(at)abayley(dot)org> writes:
> On 18 February 2014 14:40, Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us> wrote:
>> I notice though that the cost estimate for the seqscan plan isn't all that
>> much lower than that for the indexscan plan. Probably lowering
>> random_page_cost a bit would change the planner's mind. We have no
>> information about total size of database vs available RAM, but if it's
>> a mostly memory-resident database then such a change would be a good idea.

> [ database size is 3GB, RAM 2GB ]

The usual advice for database-in-RAM scenarios is to set random_page_cost
= 1, or even to lower both random_page_cost and seq_page_cost below 1.
In this case, since it's not going to be entirely RAM-resident, a
compromise setting around 2 might be a good idea.

> I'm particularly interested in the massive different between cost and
> actual for the index plan. The seq scan plan has 451984/248694 (ratio
> 1.82) for cost/actual, while the index plan has 502051/11597 (ratio
> 43.29). At least the seq scan plan is only out by a factor of ~2.

Most likely this means that the index plan is taking a lot more advantage
of locality-of-reference than the planner is giving it credit for.
I wouldn't put too much faith in those numbers by themselves though,
because that's what nearly always happens if you run the same case
through EXPLAIN more than once: all the data it needs is already in
cache. It's a good idea to pay attention to what happens when the plan
does have to read in some new data.

regards, tom lane

In response to

Browse pgsql-performance by date

  From Date Subject
Next Message Scott Marlowe 2014-02-18 04:04:09 Re: Optimal settings for RAID controller - optimized for writes
Previous Message Alistair Bayley 2014-02-18 02:18:19 Re: Why is the optimiser choosing the slower query, or, understanding explain analyze output