Thursday 15 September 2011

oracle - PreparedStatement is faster in Java, How db do it? -



oracle - PreparedStatement is faster in Java, How db do it? -

i know preparedstatement faster statement in java.

i don't know how oracle db server it.

preparedstatement gets pre-compiled in database server -> less work. it's cut down load on database.

string sql = "select * users u u.id = ?"; preparedstatement pstmt = connenction.preparestatement(sql); pstmt.setlong(1, userid); pstmt.executequery();

the query cached in database server, , compile once? if yes, how database server knows query execute before? how long cached?

the query cached in database server, , compile once?

more precisely, query plan cached on server. when run query, rdbms prepares plan first, executes it. preparing plan requires parsing query, analyzing , optimizing it, considering indexes available , statistics collected on participating tables.

if yes, how database server knows query execute before?

by comparing string of query other queries available in cache. since utilize parameterized queries, other query textually identical. caching sec major reason* utilize query parameters: if prepare statements this

// wrong! don't this! string sql = "select * users u u.id = "+userid; preparedstatement pstmt = connenction.preparestatement(sql);

all performance improvements gone, because providing different id create different query needs new plan.

* top reason parameterizing queries is, of course, avoiding injection attacks.

java oracle jdbc prepared-statement

No comments:

Post a Comment