From: James House <jhouse@part.net>
To: pgsql-jdbc@postgresql.org
Subject: Prepared Statements: Inefficient Type Conversion?
Date: Mon, 16 Apr 2007 08:24:48 -0600
Message-ID: <46238730.6060903@part.net> (raw)
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.
Attachments:jhouse.vcf (text/x-vcard, 257B) download
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
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Reply to all the recipients from your mail client; the link fills in
the headers below: reply via email
Some clients, webmail especially, drop In-Reply-To and References from
a link, and the reply then starts a new thread: use the mbox below.
To: pgsql-jdbc@postgresql.org
Cc: jhouse@part.net
Subject: Re: Prepared Statements: Inefficient Type Conversion?
In-Reply-To: <46238730.6060903@part.net>
References: <46238730.6060903@part.net>
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox