Received: from localhost (maia-2.hub.org [200.46.204.187]) by postgresql.org (Postfix) with ESMTP id ABF1D9FB544 for ; Mon, 16 Apr 2007 11:40:19 -0300 (ADT) Received: from postgresql.org ([200.46.204.71]) by localhost (mx1.hub.org [200.46.204.187]) (amavisd-maia, port 10024) with ESMTP id 32630-01 for ; Mon, 16 Apr 2007 11:40:02 -0300 (ADT) X-Greylist: delayed 00:14:34.675292 by SQLgrey-1.7.4 Received: from svr4.postgresql.org (svr4.postgresql.org [66.98.251.159]) by postgresql.org (Postfix) with ESMTP id EBD9F9FB583 for ; Mon, 16 Apr 2007 11:39:38 -0300 (ADT) X-Greylist: domain auto-whitelisted by SQLgrey-1.7.3 Received: from mail.part.net (ip67-88-113-20.z113-88-67.customer.algx.net [67.88.113.20]) by svr4.postgresql.org (Postfix) with ESMTP id A37B85AF024 for ; Mon, 16 Apr 2007 14:25:03 +0000 (GMT) Received: from localhost (mail [127.0.0.1]) by mail.part.net (Postfix) with ESMTP id 3C81A18099 for ; Mon, 16 Apr 2007 08:25:00 -0600 (MDT) X-Virus-Scanned: amavisd-new at part.net Received: from mail.part.net ([127.0.0.1]) by localhost (mail.part.net [127.0.0.1]) (amavisd-new, port 10024) with ESMTP id Lh0o+4Dv5Pq0 for ; Mon, 16 Apr 2007 08:24:51 -0600 (MDT) Received: from [192.168.5.22] (titanium.part.net [192.168.5.22]) by mail.part.net (Postfix) with ESMTP id 1236318096 for ; Mon, 16 Apr 2007 08:24:51 -0600 (MDT) Message-ID: <46238730.6060903@part.net> Date: Mon, 16 Apr 2007 08:24:48 -0600 From: James House User-Agent: Thunderbird 1.5.0.10 (Windows/20070221) MIME-Version: 1.0 To: pgsql-jdbc@postgresql.org Subject: Prepared Statements: Inefficient Type Conversion? Content-Type: multipart/mixed; boundary="------------090809000008090901020401" X-Virus-Scanned: Maia Mailguard 1.0.1 X-Spam-Status: No, hits=0.136 tagged_above=0 required=5 tests=BAYES_50=0.001, FORGED_RCVD_HELO=0.135 X-Spam-Level: X-Archive-Number: 200704/45 X-Sequence-Number: 16894 This is a multi-part message in MIME format. --------------090809000008090901020401 Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit I have a table "upids" that has approx 40 million rows. One column is named "upid" and is of type NUMERIC. Both of the following queries (ran as a statement) execute in equivalent time, 32 milliseconds: select vdi_id, leaf_category_id, vdi_key1, vdi_key2, vdi_key3 from upids where upid = 12239984; select vdi_id, leaf_category_id, vdi_key1, vdi_key2, vdi_key3 from upids where upid = '12239984'; (the subtle difference is that the value being passed is a number in one case, a string in the other case). Now, if I execute the following PreparedStatement, the query time is a minute and a half: pstmt = conn.prepareStatement(query); pstmt.setString(1, upid); rs = pstmt.executeQuery(); But with this prepared statement execution it is once again approx 30 milliseconds: pstmt = conn.prepareStatement(query); pstmt.setLong(1, Long.parseLong(upid)); rs = pstmt.executeQuery(); It seems that using the prepared statement to pass the value as a string causes all of the 40 million upid values in the table to convert to string, rather than the string parameter being converted to a number. Can't the driver be smarter than that? My problem is that my application code thinks of upid as a string, but the database has been designed for it to be an integer - which in this particular instance of the application the database is coincidentally correct in that all values of upid can be represented as a number, but my application code also has to work in other instances where it is truly a string - hence my prepared statement needs to use setString(). Also, this works fine on Oracle (no performance penalty), for which the application was originally made. Thanks for any pointers that may help me around this. --------------090809000008090901020401 Content-Type: text/x-vcard; charset=utf-8; name="jhouse.vcf" Content-Transfer-Encoding: 7bit Content-Disposition: attachment; filename="jhouse.vcf" begin:vcard fn:James House n:House;James org:Partnet, Inc.;Engineering adr;dom:;;;Salt Lake City;UT email;internet:jhouse@part.net title:Software Development Manager tel;work:801 581-1118 x.9718 tel;fax:801 581-1785 x-mozilla-html:TRUE version:2.1 end:vcard --------------090809000008090901020401--