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 009339DCC16 for ; Wed, 21 Dec 2005 14:51:08 -0400 (AST) Received: from postgresql.org ([200.46.204.71]) by localhost (av.hub.org [200.46.204.144]) (amavisd-new, port 10024) with ESMTP id 95235-07 for ; Wed, 21 Dec 2005 14:51:08 -0400 (AST) X-Greylist: from auto-whitelisted by SQLgrey- Received: from tigger.fuhr.org (tigger.fuhr.org [63.214.45.158]) by postgresql.org (Postfix) with ESMTP id B9EF59DCC4D for ; Wed, 21 Dec 2005 14:51:06 -0400 (AST) Received: from winnie.fuhr.org (winnie.fuhr.org [10.1.0.1]) by tigger.fuhr.org (8.13.3/8.13.3) with ESMTP id jBLIp29k097113 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-SHA bits=256 verify=OK); Wed, 21 Dec 2005 11:51:05 -0700 (MST) (envelope-from mfuhr@winnie.fuhr.org) Received: from winnie.fuhr.org (localhost [127.0.0.1]) by winnie.fuhr.org (8.13.4/8.13.4) with ESMTP id jBLIp2rh056644; Wed, 21 Dec 2005 11:51:02 -0700 (MST) (envelope-from mfuhr@winnie.fuhr.org) Received: (from mfuhr@localhost) by winnie.fuhr.org (8.13.4/8.13.4/Submit) id jBLIp2wu056643; Wed, 21 Dec 2005 11:51:02 -0700 (MST) (envelope-from mfuhr) Date: Wed, 21 Dec 2005 11:51:02 -0700 From: Michael Fuhr To: Sz?cs =?iso-8859-1?Q?G=E1bor?= Cc: "pgsql-performance@postgresql.org" Subject: Re: Wrong index used when ORDER BY LIMIT 1 Message-ID: <20051221185101.GA56524@winnie.fuhr.org> References: <43A998D4.4070300@gmail.com> Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <43A998D4.4070300@gmail.com> X-Virus-Scanned: by amavisd-new at hub.org X-Spam-Status: No, score=0.031 required=5 tests=[AWL=0.031] X-Spam-Score: 0.031 X-Spam-Level: X-Archive-Number: 200512/540 X-Sequence-Number: 16361 On Wed, Dec 21, 2005 at 07:03:00PM +0100, Sz?cs Gbor wrote: > Version: 7.4.6 [...] > Query is: > SELECT idopont WHERE muvelet = x ORDER BY idopont LIMIT 1. > > I expected the planner to choose the index on muvelet, then sort by idopont. > Instead, it took the other index. I think the planner is guessing that since you're ordering on idopont, scanning the idopont index will find the first matching row faster than using the muvelet index would. In many cases that's a good bet, but in this case the guess is wrong and you end up with a suboptimal plan. I just ran some tests with 8.1.1 and it chose the better plan for a query similar to what you're doing. One of the developers could probably explain why; maybe it's because of the changes that allow better use of multicolumn indexes. Try 8.1.1 if you can and see if you get better results. > -- workaround 2: quite ugly but seems to work (at least for this > -- one test case): > # explain analyze > select idopont from > (select idopont from muvelet_vonalkod > where muvelet=6859 order by idopont) foo > order by idopont limit 1; Another workaround is to use OFFSET 0 in the subquery. -- Michael Fuhr