Hi i have a class which contains different methods for checking database values.
Each method is for a particular table, so the select sql remains the same each time the method is called only the where clause value to be checked changes ,so i am using prepared statement
like
public boolean checkVal(Session session,String b,String c,String d)
{
String sql="select a from tbl_name where b=? and c=? " +
"and d=? ";
Query query=session.createSQLQuery(sql).addScalar("a",Hibernate.STRING);
query.setParameter(0,b);
query.setParameter(1,c);
query.setParameter(2,d);
if(){
some check and return
}
}
this method is to be called like 100000 times, so i think the sql will get regenerated and using a prepared statement here is useless, so what should i do to increase performance? what should be the right approach for doing this?
The SQL String can be static and final; there’s no reason to recreate it each time.
But that’s not your biggest problem.
PreparedStatementwas born for binding and repeated execution. There’s nothing better. The JDBC driver can cache the PreparedStatement once it’s parsed and validated, so you only do that work once.Have you measured performance and found a performance issue with this method? If not, don’t guess that you’ve got a problem until you have some data.
I’d ask why the method has to be called repeatedly. Is it once each for 100,000 users? If yes, there’s nothing you can do except reduce your audience or eliminate the need for the call. (Think caching.) 100,000 for a single user? Figure out how to make one call or batch them. You’ll die a latent death with that arrangement. Hyperbole on your part? Take a deep breath, relax, and get some data.