X-Original-To: pgsql-performance-postgresql.org@localhost.postgresql.org Received: from localhost (av.hub.org [200.46.204.144]) by postgresql.org (Postfix) with ESMTP id EDD5E11F66F1 for ; Thu, 6 Apr 2006 13:18:22 -0300 (ADT) Received: from postgresql.org ([200.46.204.71]) by localhost (av.hub.org [200.46.204.144]) (amavisd-new, port 10024) with ESMTP id 01265-05 for ; Thu, 6 Apr 2006 13:18:18 -0300 (ADT) X-Greylist: from auto-whitelisted by SQLgrey- Received: from mx2.ipf.is (scania.ipf.is [85.197.192.147]) by postgresql.org (Postfix) with ESMTP id D6BFB11F6552 for ; Thu, 6 Apr 2006 13:18:17 -0300 (ADT) Received: (qmail 19602 invoked by uid 1010); 6 Apr 2006 16:17:39 +0000 Received: from 85.197.216.186 by scania (envelope-from , uid 1008) with qmail-scanner-1.25-st-qms (clamdscan: 0.88/1235. spamassassin: 3.1.1. perlscan: 1.25-st-qms. Clear:RC:1(85.197.216.186):SA:0(-4.3/4.5):. Processed in 2.997852 secs); 06 Apr 2006 16:17:39 -0000 X-Antivirus-IPF-Mail-From: gnari@hive.is via scania X-Antivirus-IPF: 1.25-st-qms (Clear:RC:1(85.197.216.186):SA:0(-4.3/4.5):. Processed in 2.997852 secs Process 19595) Received: from dsl-216-186.hive.is (HELO ?192.168.1.34?) (85.197.216.186) by mx2.ipf.is with SMTP; 6 Apr 2006 16:17:36 +0000 Subject: Re: Query planner is using wrong index. From: Ragnar To: Brian Herlihy Cc: pgsql-performance@postgresql.org In-Reply-To: <20060406140151.83127.qmail@web52310.mail.yahoo.com> References: <20060406140151.83127.qmail@web52310.mail.yahoo.com> Content-Type: text/plain; charset=ISO-8859-1 Date: Thu, 06 Apr 2006 16:20:29 +0000 Message-Id: <1144340430.32289.97.camel@localhost.localdomain> Mime-Version: 1.0 X-Mailer: Evolution 2.4.1 Content-Transfer-Encoding: 8bit X-Virus-Scanned: by amavisd-new at hub.org X-Spam-Status: No, score=0.06 required=5 tests=[AWL=0.060] X-Spam-Score: 0.06 X-Spam-Level: X-Archive-Number: 200604/117 X-Sequence-Number: 18201 On fös, 2006-04-07 at 00:01 +1000, Brian Herlihy wrote: > --- Ragnar 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