From: Kevin Grittner <Kevin.Grittner@wicourts.gov>
To: psql@elbrief.de
To: pgsql-bugs@postgresql.org
Subject: Re: BUG #7495: chosen wrong index
Date: Wed, 15 Aug 2012 10:08:21 -0500
Message-ID: <502B75150200002500049751@gw.wicourts.gov> (raw)
In-Reply-To: <E1T1e1N-0004mk-2z@wrigleys.postgresql.org>
References: <E1T1e1N-0004mk-2z@wrigleys.postgresql.org>
<psql@elbrief.de> wrote:
> insert into bla ( a , b )
> select a , a
> from generate_series( 1 , 1000000 ) as a ( a ) ;
> explain analyze select * from bla
> where b > 990000 order by a limit 10 ;
> [uses index on b and has a long run time]
The problem is that PostgreSQL doesn't have any sense of the
correlation between columns a and b (i.e., they are always equal)
and assumes that it will find enough matching rows soon enough on
the scan of the index on b to make it cheaper than sorting the
results of finding all rows that match the predicate. Try your test
suite again with the only change being the insert statement:
insert into bla ( a , b )
select a , floor(random() * 1000000) + 1
from generate_series( 1 , 1000000 ) as a ( a ) ;
On my machine, with that data, all of the queries run fast.
We've been looking at ways to develop statistics on multiple
columns, so that correlations like that don't confuse the optimizer,
or trying to evaluate the "risk" of a query taking a long time based
on unexpected correlations.
Not really a bug; more like a recognized opportunity to improve the
optimizer.
-Kevin
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Reply to all the recipients from your mail client; the link fills in
the headers below: reply via email
Some clients, webmail especially, drop In-Reply-To and References from
a link, and the reply then starts a new thread: use the mbox below.
To: pgsql-bugs@postgresql.org
Cc: Kevin.Grittner@wicourts.gov, psql@elbrief.de
Subject: Re: BUG #7495: chosen wrong index
In-Reply-To: <502B75150200002500049751@gw.wicourts.gov>
References: <E1T1e1N-0004mk-2z@wrigleys.postgresql.org> <502B75150200002500049751@gw.wicourts.gov>
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox