Reputation: 2135
I have read the description of prepareStatement(String sql)
and also lot of posts related to performance and caching of PreparedStatement
and I am clear that database will parse and compile the query of the prepared statement so that for subsequent same queries, another round of parse and compile will not happen but I am not clear on whether:
prepareStatement(String sql)
will result in a database call or not? I think it will.prepareStatement(String sql)
will result in a database call then I don't understand what this line from docs means - "A SQL statement with or without IN parameters can be pre-compiled and stored in a PreparedStatement object."? Because real performance benefit we are getting is at RDBMS side, what PreparedStatement
object is doing in that?PreparedStatement
object, now if I execute the same query using a Statement
object then will RDBMS still parse and compile or will not?Upvotes: 0
Views: 436
Reputation: 2496
Yes, every call of prepareStatement
should result a database call. If no, it may be, say, this scenario:
execute("create table x(y integer)");
prepareStatement("select * from x"); // #1
execute("rename table x to old_x");
execute("create table x(z varchar(100))");
prepareStatement("select * from x"); // #2 - this stamenent is not equal to previous
PreparedStatement
is generally a wrapper of cursor handle. prepareStatement
sends SQL statement to RDBMS. RDBMS compiles it and returns a handle for it. Next database calls uses this handle so RDBMS will use compiled statement.
It depends on RDBMS. Say, Oracle will use 'soft parse' in this case. Soft parse means than database locates equal statement in it's cache and uses it if possible. It is more effective than recompilation but less effective than using prepared statement.
Upvotes: 1
Reputation: 2135
Although @Sanders and @Douglas are good but they are not complete (even considering only JDBC driver), so I am also putting my answer, which is also not complete, I would recommend reading all 3 answers to get good knowledge:
connection. prepareStatement()
then if it is a standlone Java program then a round trip to server will happen while if it is a J2EE server then there is possibility that round trip to server will not happen because of JEE PreparedStatement cacheUpvotes: 0
Reputation: 696
The following applies only to the Oracle Database JDBC drivers. Other drivers may be different.
A call to Connection.prepareStatement(String) does not do a database round trip. It constructs a new PreparedStatement and stores the SQL String in it. That's about it.
Even unused PreparedStatements are moderately complex so this isn't an entirely trivial call. Even so, there isn't much value in caching unused PreparedStatements. The cost of constructing a new one is low. Emphasis on "unused". The first execution of a PreparedStatement does do a full round trip and completes constructing the PreparedStatement. Having used a PreparedStatement once the cost of reusing it is much less than the cost of creating a new one and using it for the first time.
Executing the same SQL in two PreparedStatements is more expensive for both the client and the server than executing a single PreparedStatement twice. For the client, the additional cost is pretty straightforward. It is the cost of fully constructing the second PreparedStatement, this includes the work the client does after the first execute round trip. Things are not so simple in the database.
The Oracle Database has multiple levels of caching and reuse to minimize the cost of executing a given SQL string multiple times. A PreparedStatement holds a cursor id. That id refers to a cursor in the server. A cursor is complex structure that is the database's representation of a SQL execution. Some of the structure can be shared by other cursors executing the same SQL. Some of the structure is unique to a single execution. Some structure can be shared among some cursors executing the SQL but not others. It is a complex system.
As a general rule creating a new PreparedStatement requires a hard parse. If the server has seen the SQL before then the hard parse may not be a full hard parse as the server can reuse some of the cursor structures. If the app re-executes a PreparedStatement then ideally the server doesn't have to do anything to the cursor; it just re-executes it. But there are many cases where the server has to do a soft parse before it can re-execute. A soft parse is less work than a hard parse, but not trivial.
The above ignores the Implicit Statement Cache. The Implicit Statement Cache stores the structures created by executing Prepared and Callable statements. If the app executes a SQL, closes the PreparedStatement, then creates a new PreparedStatement with the same SQL the structure created by the first execution is reused. The cost of the first execution of a PreparedStatement that was in the cache is for all practical purposes the same as re-executing the same PreparedStatement.
Upvotes: 2