pg.ddx.io  pgsql-jdbc@postgresql.org mailing list archive  
help / color / mirror / Atom feed
Prepared Statements: Inefficient Type Conversion?
5+ messages / 2 participants
[nested] [flat]

* Prepared Statements: Inefficient Type Conversion?
@ 2007-04-16 14:24 James House <jhouse@part.net>
  2007-04-16 16:53 ` Re: Prepared Statements: Inefficient Type Conversion? Kris Jurka <books@ejurka.com>
  0 siblings, 1 reply; 5+ messages in thread

From: James House @ 2007-04-16 14:24 UTC (permalink / raw)
  To: pgsql-jdbc


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
^ permalink raw reply [nested|flat] 5+ messages in thread

* Re: Prepared Statements: Inefficient Type Conversion?
  2007-04-16 14:24 Prepared Statements: Inefficient Type Conversion? James House <jhouse@part.net>
@ 2007-04-16 16:53 ` Kris Jurka <books@ejurka.com>
  2007-04-16 19:21   ` Re: Prepared Statements: Inefficient Type Conversion? James House <jhouse@part.net>
  0 siblings, 1 reply; 5+ messages in thread

From: Kris Jurka @ 2007-04-16 16:53 UTC (permalink / raw)
  To: James House <jhouse@part.net>; +Cc: pgsql-jdbc



On Mon, 16 Apr 2007, James House wrote:

> I have a table "upids" that has approx 40 million rows.  One column is named 
> "upid" and is of type NUMERIC.
>
> 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?

You can try adding the url parameter stringtype=unspecified which will 
pass the String parameter untyped instead of as a String.

> 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.
>

You could also convert the upid column to a text type which sounds like it 
might be more correct for your application.

Kris Jurka



^ permalink  raw  reply  [nested|flat] 5+ messages in thread

* Re: Prepared Statements: Inefficient Type Conversion?
  2007-04-16 14:24 Prepared Statements: Inefficient Type Conversion? James House <jhouse@part.net>
  2007-04-16 16:53 ` Re: Prepared Statements: Inefficient Type Conversion? Kris Jurka <books@ejurka.com>
@ 2007-04-16 19:21   ` James House <jhouse@part.net>
  2007-04-16 19:29     ` Re: Prepared Statements: Inefficient Type Conversion? Kris Jurka <books@ejurka.com>
  0 siblings, 1 reply; 5+ messages in thread

From: James House @ 2007-04-16 19:21 UTC (permalink / raw)
  To: Kris Jurka <books@ejurka.com>; +Cc: pgsql-jdbc



Kris Jurka wrote:
>> 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?
>
>
> You can try adding the url parameter stringtype=unspecified which will 
> pass the String parameter untyped instead of as a String.
FWIW, that didn't seem to make a difference - actually, it takes even 
longer to execute.  

Thanks anyway.  We'll try to get the datatype of the column changed.

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
^ permalink raw reply [nested|flat] 5+ messages in thread

* Re: Prepared Statements: Inefficient Type Conversion?
  2007-04-16 14:24 Prepared Statements: Inefficient Type Conversion? James House <jhouse@part.net>
  2007-04-16 16:53 ` Re: Prepared Statements: Inefficient Type Conversion? Kris Jurka <books@ejurka.com>
  2007-04-16 19:21   ` Re: Prepared Statements: Inefficient Type Conversion? James House <jhouse@part.net>
@ 2007-04-16 19:29     ` Kris Jurka <books@ejurka.com>
  2007-04-16 19:33       ` Re: Prepared Statements: Inefficient Type Conversion? James House <jhouse@part.net>
  0 siblings, 1 reply; 5+ messages in thread

From: Kris Jurka @ 2007-04-16 19:29 UTC (permalink / raw)
  To: James House <jhouse@part.net>; +Cc: pgsql-jdbc



On Mon, 16 Apr 2007, James House wrote:

>> You can try adding the url parameter stringtype=unspecified which will pass 
>> the String parameter untyped instead of as a String.
>
> FWIW, that didn't seem to make a difference - actually, it takes even longer 
> to execute. 
>

That's odd I was expecting to see something like this:

jurka=# CREATE TABLE test (a numeric);
CREATE TABLE
jurka=# CREATE INDEX test_idx_a ON test(a);
CREATE INDEX
jurka=# EXPLAIN SELECT * FROM test WHERE a = '1'::text;
                       QUERY PLAN
------------------------------------------------------
  Seq Scan on test  (cost=0.00..29.65 rows=7 width=32)
    Filter: ((a)::text = '1'::text)
(2 rows)

Get changed to something like this:

jurka=# EXPLAIN SELECT * FROM test WHERE a = '1'::unknown;
                                QUERY PLAN
-------------------------------------------------------------------------
  Bitmap Heap Scan on test  (cost=4.30..14.45 rows=7 width=32)
    Recheck Cond: (a = 1::numeric)
    ->  Bitmap Index Scan on test_idx_a  (cost=0.00..4.30 rows=7 width=0)
          Index Cond: (a = 1::numeric)
(4 rows)

Perhaps you have a JDBC driver that is too old to understand the 
stringtype URL parameter?  Looks like it is only available in 8.2 and on. 
So if you don't have an 8.2 driver it won't do anything.

Kris Jurka



^ permalink  raw  reply  [nested|flat] 5+ messages in thread

* Re: Prepared Statements: Inefficient Type Conversion?
  2007-04-16 14:24 Prepared Statements: Inefficient Type Conversion? James House <jhouse@part.net>
  2007-04-16 16:53 ` Re: Prepared Statements: Inefficient Type Conversion? Kris Jurka <books@ejurka.com>
  2007-04-16 19:21   ` Re: Prepared Statements: Inefficient Type Conversion? James House <jhouse@part.net>
  2007-04-16 19:29     ` Re: Prepared Statements: Inefficient Type Conversion? Kris Jurka <books@ejurka.com>
@ 2007-04-16 19:33       ` James House <jhouse@part.net>
  0 siblings, 0 replies; 5+ messages in thread

From: James House @ 2007-04-16 19:33 UTC (permalink / raw)
  To: Kris Jurka <books@ejurka.com>; +Cc: pgsql-jdbc



Kris Jurka wrote:
> Perhaps you have a JDBC driver that is too old to understand the 
> stringtype URL parameter?  Looks like it is only available in 8.2 and 
> on. So if you don't have an 8.2 driver it won't do anything.

That's it we're using 8.1.4 database, so I'm using the 8.1.4 driver.

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
^ permalink raw reply [nested|flat] 5+ messages in thread


end of thread, other threads:[~2007-04-16 19:33 UTC | newest]

Thread overview: 5+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
2007-04-16 14:24 Prepared Statements: Inefficient Type Conversion? James House <jhouse@part.net>
2007-04-16 16:53 ` Kris Jurka <books@ejurka.com>
2007-04-16 19:21   ` James House <jhouse@part.net>
2007-04-16 19:29     ` Kris Jurka <books@ejurka.com>
2007-04-16 19:33       ` James House <jhouse@part.net>

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