Received: from localhost (unknown [200.46.204.182]) by postgresql.org (Postfix) with ESMTP id 578099FB2B4 for ; Wed, 10 Oct 2007 14:05:56 -0300 (ADT) Received: from postgresql.org ([200.46.204.71]) by localhost (mx1.hub.org [200.46.204.182]) (amavisd-maia, port 10024) with ESMTP id 64861-04 for ; Wed, 10 Oct 2007 14:05:43 -0300 (ADT) X-Greylist: from auto-whitelisted by SQLgrey-1.7.5 Received: from outmail129161.authsmtp.com (outmail129161.authsmtp.com [62.13.129.161]) by postgresql.org (Postfix) with ESMTP id 4851D9F948E for ; Wed, 10 Oct 2007 14:05:50 -0300 (ADT) Received: from outmail128175.authsmtp.net (outmail128175.authsmtp.net [62.13.128.175]) by punt3.authsmtp.com (8.13.8/8.13.8/Kp) with ESMTP id l9AH5mfi021931; Wed, 10 Oct 2007 18:05:48 +0100 (BST) Received: from [192.168.0.3] (85-211-149-22.dyn.gotadsl.co.uk [85.211.149.22]) (authenticated bits=0) by mail.authsmtp.com (8.13.8/8.13.8/Kp) with ESMTP id l9AH5lmT078237; Wed, 10 Oct 2007 18:05:47 +0100 (BST) Subject: Re: hashjoin chosen over 1000x faster plan From: Simon Riggs To: Kevin Grittner Cc: pgsql-performance@postgresql.org In-Reply-To: <470C9813.EE98.0025.0@wicourts.gov> References: <470B99BE.EE98.0025.0@wicourts.gov> <1191997904.4233.125.camel@ebony.site> <470C9813.EE98.0025.0@wicourts.gov> Content-Type: text/plain Date: Wed, 10 Oct 2007 18:05:55 +0100 Message-Id: <1192035955.4233.306.camel@ebony.site> Mime-Version: 1.0 X-Mailer: Evolution 2.8.2 Content-Transfer-Encoding: 7bit X-Server-Quench: 0bd112a1-7753-11dc-b86a-001185d377ca X-AuthRoute: OCdxZQATClZOTQEd DAteCiNZVAwpPBRK HVkIKg5MJUcNSQVJ NksadBtFaQVbYlpF HGQLW1NEUVV7WWZ/ bw0fZQBDYEtPQQRu WVZLQE1QERpoT0AI d2YbDkQWdwJOen13 K0RmXXYVXBYudEd/ E09JEmoDZnphaTRM TRJQcVJJcANLfxdA PlN4UyBcLwdSbGoL JyIJERcLGAAXKC1O QgMKLFRaSkYMBTM6 DxccEC9nRBVdHzV7 IRU9NlMGVF4VO0Na X-Authentic-SMTP: 61633235383639.squirrel.dmpriest.net.uk:752/Kp X-Report-SPAM: If SPAM / abuse - report it at: http://www.authsmtp.com/abuse X-Virus-Status: No virus detected - but ensure you scan with your own anti-virus system! X-Virus-Scanned: Maia Mailguard 1.0.1 X-Archive-Number: 200710/99 X-Sequence-Number: 27121 On Wed, 2007-10-10 at 09:15 -0500, Kevin Grittner wrote: > >>> On Wed, Oct 10, 2007 at 1:31 AM, in message > <1191997904.4233.125.camel@ebony.site>, Simon Riggs > wrote: > > On Tue, 2007-10-09 at 15:09 -0500, Kevin Grittner wrote: > > > >> I have a situation where a query is running much slower than I would > >> expect. The ANALYZE showed that it is hashing some information which > >> is rarely needed. When I set enable_hashjoin = off for the > >> connection the query run in 1/1000 the time. > > > > Can you confirm the two queries give identical outputs? > > I checked; the output is identical. > > > It isn't clear > > to me why the second sort is (never executed) in your second plan, which > > I would only expect to see for an inner merge join. > > I assume that is because there were no rows to sort. The > CaseTypeHistEvent view is only needed if there is a link to an event > which reopens the charge after it is disposed. This only happens for > about 1% of the Charge records. So CHST.EventType is mostly NULL? So the good news is that the default plan works best when it does actually find a match. So for 1% of cases you will have an execution time of about 1s, <1ms for the others if you fiddle with the planner methods. The planner thinks every row will find a match, yet the actual number is only 1%. Hmmm, same section of code as last week. Basically the planner doesn't ever optimise for the possibility of the never-executed case because even a single row returned would destroy that assumption. If we had an Option node in there, we could run the first part of the plan before deciding whether to do an MJ or an HJ. Doing that would avoid doing 2 sorts and return even quicker in the common case (about 80% time) without being slower in the slowest. -- Simon Riggs 2ndQuadrant http://www.2ndQuadrant.com