Received: from maia.hub.org (maia-3.hub.org [200.46.204.243]) by mail.postgresql.org (Postfix) with ESMTP id 5A6891337B4F for ; Fri, 25 Feb 2011 09:24:35 -0400 (AST) Received: from mail.postgresql.org ([200.46.204.86]) by maia.hub.org (mx1.hub.org [200.46.204.243]) (amavisd-maia, port 10024) with ESMTP id 10507-04 for ; Fri, 25 Feb 2011 13:24:28 +0000 (UTC) X-Greylist: from auto-whitelisted by SQLgrey-1.7.6 Received: from titanium.nettersworld.net (unknown [202.67.150.137]) by mail.postgresql.org (Postfix) with SMTP id 480B7133740B for ; Fri, 25 Feb 2011 09:24:26 -0400 (AST) Received: (qmail 81319 invoked from network); 25 Feb 2011 21:24:22 +0800 Received: from e139.ip.nettersworld.net (HELO localhost) (202.67.150.139) by titanium.nettersworld.net with SMTP; 25 Feb 2011 21:24:22 +0800 Received: from titanium.nettersworld.net ([202.67.150.137]) by localhost (titanium.nettersworld.net [127.0.0.1]) with ESMTP id 58557-06; Fri, 25 Feb 2011 21:24:20 +0800 (HKT) Received: from longwl7 (unknown [58.247.211.11]) by titanium.nettersworld.net (Postfix) with ESMTP id CD3A428453; Fri, 25 Feb 2011 21:24:18 +0800 (HKT) From: "Sam Wong" To: "'Martin Kjeldsen'" , "'Marti Raudsepp'" Cc: References: <019101cbd498$7192ba70$54b82f50$@hellosam.net> In-Reply-To: Subject: Re: Perl Binding affects speed? Date: Fri, 25 Feb 2011 21:24:16 +0800 Message-ID: <01e601cbd4ef$4eb702c0$ec250840$@hellosam.net> MIME-Version: 1.0 Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: 7bit X-Mailer: Microsoft Outlook 14.0 Thread-Index: AQIfCMt398pau22VioB3IllxsOBkKwK4HChJAbwWUueTSCthwA== Content-Language: zh-hk X-Virus-Scanned: amavisd-new at nettersworld.net X-Virus-Scanned: Maia Mailguard 1.0.1 X-Spam-Status: No, hits=-1.107 tagged_above=-10 required=5 tests=BAYES_00=-1.9, RDNS_NONE=0.793 X-Spam-Level: X-Archive-Number: 201102/494 X-Sequence-Number: 42632 From: Martin Kjeldsen, Sent: 2011/2/25, 20:59 > > On 25/02/2011, at 13.25, Marti Raudsepp wrote: > > > On Fri, Feb 25, 2011 at 05:02, Sam Wong wrote: > >> * But if I do this - using binding: > >> $dbh->selectall_arrayref("SELECT * from shipment_lookup WHERE > >> (UPPER(lookup) LIKE ?)", undef, '0GURG5YGVQA9%'); It took 10 seconds > >> to finish the query, just like it was using full table scan instead! > >> Even though the 'explain' shows the same query plan. > > > > This is a pretty common shortcoming with placeholders. Since planning > > of parameterized queries is done *before* binding parameters, the > > planner has no knowledge of what the "?" placeholder actually is. Thus > > it often gets the selectivity statistics wrong and produces worse > > plans for your values. > > > > AFAIK the only workaround is to not use variable binding in these > > cases, but escape and insert your variables straight it into the SQL > > query. > > Instead of not using the placeholder syntax you can use: > > local $dbh->{pg_server_prepare} = 0; > > which disables prepared queries serverside in the current scope and > therefore doesn't have the late variable binding issue, but allows you to > avoid SQL injection attacks. > Thanks, I will look into that. Sam