Received: from localhost (unknown [200.46.204.183]) by mail.postgresql.org (Postfix) with ESMTP id BAD1F64FD33 for ; Tue, 30 Dec 2008 15:42:55 -0400 (AST) Received: from mail.postgresql.org ([200.46.204.86]) by localhost (mx1.hub.org [200.46.204.183]) (amavisd-maia, port 10024) with ESMTP id 73727-04 for ; Tue, 30 Dec 2008 15:42:53 -0400 (AST) X-Greylist: domain auto-whitelisted by SQLgrey-1.7.6 Received: from mail-ew0-f21.google.com (mail-ew0-f21.google.com [209.85.219.21]) by mail.postgresql.org (Postfix) with ESMTP id 498D864FCE2 for ; Tue, 30 Dec 2008 15:42:52 -0400 (AST) Received: by ewy14 with SMTP id 14so6488929ewy.19 for ; Tue, 30 Dec 2008 11:42:50 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=gamma; h=domainkey-signature:received:received:message-id:date:from:to :subject:cc:in-reply-to:mime-version:content-type :content-transfer-encoding:content-disposition:references; bh=jnM8b8cJGXVN/SKzlbRA52l4NR9aVU5ml0FgzpgLNXo=; b=PnLfhXixBfwP78+wJai1rS7baSnMQ+DVR3qTALDWK3ZZZg+ZIK2/eu3gL/KUybC5dI hIklyzD2ZAsthTLYCt/RkT/Vu/xZKE+aENWm2TFDuKWpEoAKx2K7dZOCtMYFZkER5HY4 XWRsyrQnoxtMJ9dPZATtq66Feva8yutFdpCqQ= DomainKey-Signature: a=rsa-sha1; c=nofws; d=gmail.com; s=gamma; h=message-id:date:from:to:subject:cc:in-reply-to:mime-version :content-type:content-transfer-encoding:content-disposition :references; b=tfxau0RoqCslTJ579Y85PZV/J0Qn8Z+Um/hKaL/J3JiJLAlh0jbzEfa7LOUeeVSEqf skVGtul5nWGCF1IFmqvkGipDfQAmaK6YII6lQASOhcKHaidWbq6WZenDiiWQwaaCrz6g N2I3hN+XXgXMXSeiIWu1ehuyCp20xCshcef/0= Received: by 10.210.144.3 with SMTP id r3mr17751476ebd.67.1230666170309; Tue, 30 Dec 2008 11:42:50 -0800 (PST) Received: by 10.210.77.7 with HTTP; Tue, 30 Dec 2008 11:42:49 -0800 (PST) Message-ID: Date: Tue, 30 Dec 2008 14:42:49 -0500 From: "Merlin Moncure" To: bricklen Subject: Re: Poor plan choice in prepared statement Cc: pgsql-performance@postgresql.org In-Reply-To: <33b743250812301059u31ce82faw3d72a38280b37402@mail.gmail.com> MIME-Version: 1.0 Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: 7bit Content-Disposition: inline References: <33b743250812301059u31ce82faw3d72a38280b37402@mail.gmail.com> X-Virus-Scanned: Maia Mailguard 1.0.1 X-Spam-Status: No, hits=0 tagged_above=0 required=5 tests=none X-Spam-Level: X-Archive-Number: 200812/231 X-Sequence-Number: 32267 On Tue, Dec 30, 2008 at 1:59 PM, bricklen wrote: > Hi, I am re-posting my question here after trying to find a solution > in the PHP pgsql list with no luck. > > I am experiencing some performance issues that I think are stemming > from prepared statements. I have a pretty simple query: > -- bad plan, from prepared statement > -- > dev=# prepare fooplan (date,date,int,int) as > dev-# SELECT cl.idOffer AS campaign, cl.idAffiliate AS affiliate, > cl.idCreative AS creative, cl.subid, cl.datetime > dev-# FROM click AS cl LEFT JOIN conversion AS co ON cl.clickGenerated > = co.clickGenerated > dev-# WHERE cl."date" >= $1 > dev-# AND cl."date" <= $2 > dev-# AND cl.idAffiliate = $3 > dev-# LIMIT $4; Your problem is that the query as written is hard to plan. The database has no idea what you pass in, it has to guess. (IMO, It almost always guesses wrong...I think it should assume 1 row returned). Also, the db has no idea what you want to pass in at plan time for date. what indexes do you have on click? merlin