Received: from maia.hub.org (maia-3.hub.org [200.46.204.243]) by mail.postgresql.org (Postfix) with ESMTP id E48951337B4A for ; Thu, 24 Feb 2011 23:09:26 -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 66439-05 for ; Fri, 25 Feb 2011 03:09:19 +0000 (UTC) X-Greylist: delayed 00:06:39.162089 by SQLgrey-1.7.6 Received: from titanium.nettersworld.net (unknown [202.67.150.137]) by mail.postgresql.org (Postfix) with SMTP id 8C7FC133616B for ; Thu, 24 Feb 2011 23:09:18 -0400 (AST) Received: (qmail 23623 invoked from network); 25 Feb 2011 11:02:36 +0800 Received: from e139.ip.nettersworld.net (HELO localhost) (202.67.150.139) by titanium.nettersworld.net with SMTP; 25 Feb 2011 11:02:36 +0800 Received: from titanium.nettersworld.net ([202.67.150.137]) by localhost (titanium.nettersworld.net [127.0.0.1]) with ESMTP id 06406-07 for ; Fri, 25 Feb 2011 11:02:33 +0800 (HKT) Received: from longwl7 (unknown [58.247.233.96]) by titanium.nettersworld.net (Postfix) with ESMTP id 899BE28454 for ; Fri, 25 Feb 2011 11:02:31 +0800 (HKT) From: "Sam Wong" To: Subject: Perl Binding affects speed? Date: Fri, 25 Feb 2011 11:02:23 +0800 Message-ID: <019101cbd498$7192ba70$54b82f50$@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: AcvUliQwYBkLWP0tSLCij6NUFKkkqA== 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/491 X-Sequence-Number: 42629 I run into performance problem when I pass the condition/variable in binding ways, however if I put them in the query string it's absolutely fine. ---- Here is my table and index: CREATE TABLE shipment_lookup ( shipment_id text NOT NULL, lookup text NOT NULL ); CREATE INDEX shipment_lookup_prefix ONshipment_lookup USING btree (upper(lookup)); ---- I have 10 million rows in the table. * My query is: $dbh->selectall_arrayref("SELECT * from shipment_lookup WHERE (UPPER(lookup) LIKE '0GURG5YGVQA9%')"); Here is the explain I get by using Perl and pgAdmin III. Index Scan using shipment_lookup_prefix on shipment_lookup (cost=0.01..5.00 rows=921 width=28) Index Cond: ((upper(lookup) >= '0GURG5YGVQA9'::text) AND (upper(lookup) < '0GURG5YGVQA:'::text)) Filter: (upper(lookup) ~~ '0GURG5YGVQA9%'::text) Index is used, and it just takes 50ms to execute. So far so good. * 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. So what would be the issue...? I can't isolate if it's the Perl or pgsql. Thanks, Sam ---- Version Info: Postgresql: "PostgreSQL 8.4.5, compiled by Visual C++ build 1400, 32-bit" on Windows 2003 Perl: 5.10.1 DBD::Pg: 2.17.2