Received: from localhost (unknown [200.46.204.183]) by mail.postgresql.org (Postfix) with ESMTP id F36E964FF6F for ; Tue, 30 Dec 2008 16:14:37 -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 80647-01 for ; Tue, 30 Dec 2008 16:14:35 -0400 (AST) X-Greylist: domain auto-whitelisted by SQLgrey-1.7.6 Received: from rv-out-0506.google.com (rv-out-0506.google.com [209.85.198.225]) by mail.postgresql.org (Postfix) with ESMTP id 414EF64FF4F for ; Tue, 30 Dec 2008 16:14:35 -0400 (AST) Received: by rv-out-0506.google.com with SMTP id b25so6605257rvf.43 for ; Tue, 30 Dec 2008 12:14:34 -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=8lx7+COew3ldAEZ4sBHMEBmTn9PS8AfdkkX2DrMAXU0=; b=NUZvnwO/4e0AePg0NEhTUwiChamnlGvIdpcdrR0Fa3HI9PIa0Y8vRZf621B3IXVahR Suuv4PLURKf+2UZgHw76Fux3Oa3AL989VFbORhDpX9WXTflr9oVPkOwZlDmzWK3hLDZp /MRTMEZVO4RMa4u7J3VswubmzCur6/dEU1Ohw= 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=jJIT1WVB1pwuefpfkGxIvSQ6kelmg1PbVIL1Qku50kCNj088xRAyUAtXGDK1/dIgVE w1rkEg2QGN1NZg+L/dq7EwltEnnZ7Uy5rmvWJvvKurdU/OK0YFcWVZqecks+IAHZfQ7E K4FreYgmbiG/5tiLguW5dNuHUBJ37E4wXpy6Y= Received: by 10.142.11.20 with SMTP id 20mr6248333wfk.197.1230668074147; Tue, 30 Dec 2008 12:14:34 -0800 (PST) Received: by 10.142.255.15 with HTTP; Tue, 30 Dec 2008 12:14:34 -0800 (PST) Message-ID: <33b743250812301214k7a0ec4fby614d693ec7915d3@mail.gmail.com> Date: Tue, 30 Dec 2008 12:14:34 -0800 From: bricklen To: "Scott Marlowe" Subject: Re: Poor plan choice in prepared statement Cc: "Merlin Moncure" , pgsql-performance@postgresql.org In-Reply-To: MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 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/234 X-Sequence-Number: 32270 Hi Scott, On Tue, Dec 30, 2008 at 12:09 PM, Scott Marlowe wrote: > On Tue, Dec 30, 2008 at 12:42 PM, Merlin Moncure wrote: >> 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. > > One of the things you can try here is to build your query then execute > it so it has to be planned each time. > Yeah, I've tested that in the application itself and it worked correctly. I am trying to discover a way to use bind variables in PHP without using the prepare function (to block sql injection), or if I must use the prepare function, then force it to replan each time somehow. That's part of where I'm stuck (and I'm no php guy).