Statement Versus PreparedStatement
There’s a popular belief that using a
PreparedStatement
object is faster than using a Statement
object.
After all, a prepared statement has to verify its metadata against
the database only once, while a statement has to do it every time. So
how could it be any other way? Well, the truth of the matter is that
it takes about 65 iterations of a prepared statement before its total
time for execution catches up with a statement. This has performance
implications for your application, and exploring these issues is what
this section is all about.
When it comes to which SQL statement object performs better under
typical use, a Statement
or a
PreparedStatement
, the truth is that the
Statement
object yields the best performance. When
you consider how SQL statements are typically used in an
application -- 1 or 2 here, maybe 10-20 (rarely more) per
transaction -- you realize that a Statement
object will perform them in less time than a
PreparedStatement
object. In the next two
sections, we’ll look at this performance issue with respect to
both the OCI driver and the Thin driver.
The OCI Driver
Table 19-3 shows the timings in
milliseconds for 1 insert and 1,000 inserts in the TESTXXXPERF table.
The inserts are done first using a
Statement
object and then a PreparedStatement
object. If you
look at the results for 1,000 inserts, you may think that a prepared
statement performs better. After all, at 1,000 inserts, the
PreparedStatement
object is almost twice ...
Get Java Programming with Oracle JDBC now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.