Re: Query planner is using wrong index. - Mailing list pgsql-performance

From Ragnar
Subject Re: Query planner is using wrong index.
Date
Msg-id 1144340430.32289.97.camel@localhost.localdomain
Whole thread Raw
In response to Re: Query planner is using wrong index.  (Brian Herlihy <btherl@yahoo.com.au>)
Responses Re: Query planner is using wrong index.
List pgsql-performance
On fös, 2006-04-07 at 00:01 +1000, Brian Herlihy wrote:
> --- Ragnar <gnari@hive.is> wrote:
>
> > On fim, 2006-04-06 at 19:27 +1000, Brian Herlihy wrote:
> >
> > > Yes, the primary key is far better.  I gave it the ultimate test - I
> > dropped
> > > the (p2, p3) index.  It's blindingly fast when using the PK,
> >
> > I have problems understanding exactly how an index on
> > (p1,p2,p3) can be faster than and index on (p2,p3) for
> > a query not involving p1.

> db# explain analyze select * from t WHERE p1 = 'a' and p2 = 'uk.altavista.com'
> AND p3 = 'web/results?itag=&q=&kgs=&kls=';

this is different from what you said earlier. in your
original post you showed a problem query without any
reference to p1 in the WHERE clause. this confused me.

>  Index Scan using p2_p3_idx on t  (cost=0.00..6.02 rows=1 width=102) (actual
> time=2793.247..2793.247 rows=0 loops=1)
>    Index Cond: (((p2)::text = 'uk.altavista.com'::text) AND ((p3)::text =
> 'web/results?itag=&q=&kgs=&kls='::text))
>    Filter: ((p1)::text = 'a'::text)
>  Total runtime: 2793.303 ms
> (4 rows)

try to add an ORDER BY clause:

explain analyze
  select * from t
  WHERE p1 = 'a'
    and p2 = 'uk.altavista.com'
    AND p3 = 'web/results?itag=&q=&kgs=&kls='
  ORDER BY p1,p2,p3;

this might push the planner into using the primary key

gnari





pgsql-performance by date:

Previous
From: "Luke Lonergan"
Date:
Subject: Re: Sun Fire T2000 and PostgreSQL 8.1.3
Next
From: John DeSoi
Date:
Subject: Re: CURSOR OR OFFSET/LIMIT