Received: from localhost (unknown [200.46.204.183]) by developer.postgresql.org (Postfix) with ESMTP id CE57D2E004B for ; Tue, 6 May 2008 15:14:39 -0300 (ADT) Received: from developer.postgresql.org ([200.46.204.71]) by localhost (mx1.hub.org [200.46.204.183]) (amavisd-maia, port 10024) with ESMTP id 18402-03 for ; Tue, 6 May 2008 15:14:35 -0300 (ADT) X-Greylist: from auto-whitelisted by SQLgrey-1.7.5 Received: from gateout01.mbox.net (gateout01.mbox.net [165.212.64.21]) by developer.postgresql.org (Postfix) with ESMTP id 986BD2E004C for ; Tue, 6 May 2008 15:14:36 -0300 (ADT) Received: from gwsout01.mbox.net (gwsout01.mbox.net [165.212.64.24]) by gateout01.mbox.net (Postfix) with ESMTP id EE43A4116E4; Tue, 6 May 2008 18:14:28 +0000 (GMT) Received: from GW2.EXCHPROD.USA.NET [165.212.116.254] by gwsout01.mbox.net via smtad (C8.MAIN.3.34P) with ESMTP id XID751meFsod3870Xo1; Tue, 06 May 2008 18:14:28 -0000 X-USANET-Source: 165.212.116.254 IN sthomas@leapfrogonline.com GW2.EXCHPROD.USA.NET X-USANET-MsgId: XID751meFsod3870Xo1 Received: from s1hub2.EXCHPROD.USA.NET ([10.120.220.32]) by GW2.EXCHPROD.USA.NET with Microsoft SMTPSVC(6.0.3790.1830); Tue, 6 May 2008 12:14:27 -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 19:14:27 +0100 Subject: Re: multiple joins + Order by + LIMIT query performance issue From: Shaun Thomas To: Tom Lane CC: Antoine Baudoux , "pgsql-performance@postgresql.org" In-Reply-To: <16494.1210096749@sss.pgh.pa.us> References: <938EC908-FB9A-4BFA-950A-053BE3C87085@taktik.be> <1210092220.14833.33.camel@berners-lee> <16494.1210096749@sss.pgh.pa.us> Content-Type: text/plain Organization: Leapfrog Online Date: Tue, 6 May 2008 13:14:29 -0500 Message-ID: <1210097669.14833.45.camel@berners-lee> MIME-Version: 1.0 X-Mailer: Evolution 2.12.1 Content-Transfer-Encoding: 7bit X-OriginalArrivalTime: 06 May 2008 18:14:27.0822 (UTC) FILETIME=[0595B0E0:01C8AFA5] X-Virus-Scanned: Maia Mailguard 1.0.1 X-Spam-Status: No, hits=1.498 tagged_above=0 required=5 tests=AWL=-0.934, DNS_FROM_OPENWHOIS=2.431, WHOIS_NETSOLPR=0.001 X-Spam-Level: * X-Archive-Number: 200805/102 X-Sequence-Number: 30070 On Tue, 2008-05-06 at 18:59 +0100, Tom Lane wrote: > Whether the scan is forwards or backwards has nothing > to do with it. The planner is using the index ordering > to avoid having to do a full-table scan and sort. Oh, I know that. I just noticed that when this happened to us, more often than not, it was a reverse index scan that did it. The thing that annoyed me most was when it happened on an index that, even on a table having 20M rows, the cardinality is < 10 on almost every value of that index. In our case, having a "LIMIT 1" was much worse than just getting back 5 or 10 rows and throwing away everything after the first one. > but when it's a win it can be a big win, too, so "it's > a bug take it out" is an unhelpful opinion. That's just it... it *can* be a big win. But when it's a loss, you're index-scanning a 20M+ row table for no reason. We got around it, obviously, but it was a definite surprise when a query that normally runs in 0.5ms time randomly and inexplicably runs at 4-120s. This is disaster for a feed loader chewing through a few ten-thousand entries. But that's just me grousing about not having query hints or being able to tell Postgres to never, ever, ever index-scan certain tables. :) -- Shaun Thomas Database Administrator Leapfrog Online 807 Greenwood Street Evanston, IL 60201 Tel. 847-440-8253 Fax. 847-570-5750 www.leapfrogonline.com