Received: from localhost (unknown [200.46.204.184]) by developer.postgresql.org (Postfix) with ESMTP id C198D2E0041 for ; Tue, 6 May 2008 13:49:21 -0300 (ADT) Received: from developer.postgresql.org ([200.46.204.71]) by localhost (mx1.hub.org [200.46.204.184]) (amavisd-maia, port 10024) with ESMTP id 13280-07 for ; Tue, 6 May 2008 13:49:12 -0300 (ADT) X-Greylist: delayed 00:05:43.533544 by SQLgrey-1.7.5 Received: from gateout02.mbox.net (gateout02.mbox.net [165.212.64.22]) by developer.postgresql.org (Postfix) with ESMTP id 0AFA92E002D for ; Tue, 6 May 2008 13:49:14 -0300 (ADT) Received: from gwsout02.mbox.net (gwsout02.mbox.net [165.212.64.25]) by gateout02.mbox.net (Postfix) with ESMTP id B77A86CC1C3; Tue, 6 May 2008 16:43:41 +0000 (GMT) Received: from GW1.EXCHPROD.USA.NET [165.212.116.254] by gwsout02.mbox.net via smtad (C8.MAIN.3.34P) with ESMTP id XID185meFqrp1554Xo2; Tue, 06 May 2008 16:43:41 -0000 X-USANET-Source: 165.212.116.254 IN sthomas@leapfrogonline.com GW1.EXCHPROD.USA.NET X-USANET-MsgId: XID185meFqrp1554Xo2 Received: from s1hub2.EXCHPROD.USA.NET ([10.120.220.32]) by GW1.EXCHPROD.USA.NET with Microsoft SMTPSVC(6.0.3790.1830); Tue, 6 May 2008 10:43:39 -0600 Received: from [192.168.50.198] (70.230.73.42) by exchange.postoffice.net (10.120.220.32) with Microsoft SMTP Server (TLS) id 8.1.263.0; Tue, 6 May 2008 17:43:39 +0100 Subject: Re: multiple joins + Order by + LIMIT query performance issue From: Shaun Thomas To: Antoine Baudoux CC: "pgsql-performance@postgresql.org" In-Reply-To: <938EC908-FB9A-4BFA-950A-053BE3C87085@taktik.be> References: <938EC908-FB9A-4BFA-950A-053BE3C87085@taktik.be> Content-Type: text/plain Organization: Leapfrog Online Date: Tue, 6 May 2008 11:43:40 -0500 Message-ID: <1210092220.14833.33.camel@berners-lee> MIME-Version: 1.0 X-Mailer: Evolution 2.12.1 Content-Transfer-Encoding: 7bit X-OriginalArrivalTime: 06 May 2008 16:43:39.0635 (UTC) FILETIME=[56365430:01C8AF98] X-Virus-Scanned: Maia Mailguard 1.0.1 X-Spam-Status: No, hits=1.125 tagged_above=0 required=5 tests=AWL=-1.307, DNS_FROM_OPENWHOIS=2.431, WHOIS_NETSOLPR=0.001 X-Spam-Level: * X-Archive-Number: 200805/88 X-Sequence-Number: 30056 On Tue, 2008-05-06 at 16:03 +0100, Antoine Baudoux wrote: > My understanding is that in the first case the sort is > done after all the table joins and filtering, but in the > second case ALL the rows in t_event are scanned and sorted > before the join. You've actually run into a problem that's bitten us in the ass a couple of times. The problem with your second query is that it's *too* efficient. You'll notice the first plan uses a bevy of nest-loops, which is very risky if the row estimates are not really really accurate. The planner says "Hey, customer_id=1 could be several rows in the t_network table, but not too many... I better check them one by one." I've turned off nest-loops sometimes to avoid queries that would run several hours due to mis-estimation, but it looks like yours was just fine. The second query says "Awesome! Only one network... I can just search the index of t_event backwards for this small result set!" But here's the rub... try your query *without* the limit clause, and you may find it's actually faster, because the planner suddenly thinks it will have to scan the whole table, so it choses an alternate plan (probably back to the nest-loop). Alternatively, take off the order-by clause, and it'll remove the slow backwards index-scan. I'm not sure what causes this, but the problem with indexes is that they're not necessarily in the order you want unless you also cluster them, so a backwards index scan is almost always the wrong answer. Personally I consider this a bug, and it's been around since at least the 8.1 tree. The only real answer is that you have a fast version of the query, so try and play with it until it acts the way you want. -- Shaun Thomas Database Administrator Leapfrog Online 807 Greenwood Street Evanston, IL 60201 Tel. 847-440-8253 Fax. 847-570-5750 www.leapfrogonline.com