• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

PrepairedStatement compilation

 
Kaustubh G Sharma
Ranch Hand
Posts: 1283
Chrome Java Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
PrepairedStatement compiles only 1 time while statement compiles everytime it executed


please explain it...
 
Mohamed Sanaulla
Saloon Keeper
Posts: 3165
34
Google App Engine Java Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Correction: PreparedStatment and Statement.

From the API for PreparedStatment:
A SQL statement is precompiled and stored in a PreparedStatement object. This object can then be used to efficiently execute this statement multiple times.

For Statment:
The object used for executing a static SQL statement and returning the results it produces.


 
Martin Vajsar
Sheriff
Posts: 3752
62
Chrome Netbeans IDE Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The exact mechanism is database-dependent, but generally it works like this:

When you submit a new SQL statement, the database must "compile" it - determine which tables/views are used by the statement, check the access rights, decide how to actually perform the statement (which indexes to use, if any, how and in what order to join tables and so on). This compilation phase takes some time, and in some databases (eg. Oracle) is less scalable than the execution phase. More or less all of this processing takes place every time you execute a Statement, even if you execute exactly the same statement over and over (some databases can detect repeated execution of identical statements and reuse previous compilation, but it always incurs some overhead compared to PreparedStatement).

When you're using PreparedStatement, the database caches the results of the compilation phase and whenever you execute it again, the compilation is skipped. Moreover, PreparedStatement should use binds instead of literals. In this case you can execute the statement with different values for the bind variables and generally it won't recompile.

Proper use of PreparedStatement with binds can speed things up tremendously. On Oracle I was able to reduce the processing time by a third or half when I modified thousands of insert statements stuffed with literals to use binds; although that was in a different language, I'd expect the same outcome in Java.
 
Kaustubh G Sharma
Ranch Hand
Posts: 1283
Chrome Java Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Great Thanks MAN
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic