I am using Spring NamedParameterJdbcTemplate in my search process, but when I get many results, its execution take several time and I don’t know if there are a way to optimize it.
Here my code:
NamedParameterJdbcTemplate jt = new NamedParameterJdbcTemplate(dataSource);
StringBuilder sb = new StringBuilder(query);
query = sb.toString();
return jt.query(query, map, new MyRowMapper());
Have you any idea please?
Thanks all!
Few things you can do
1. Why to create instance
NamedParameterJdbcTemplateevery time when method is invoked? Create it once and pass previously created instance to the method.keep
NamedParameterJdbcTemplateas class attribute and initialize it once.2. What you are trying here?
Pass
querydirectly to.query()method if its of typeString.3. Inside
MyRowMapper, make sure you are fetching and assigning only required fields inmapRowmethod. This might be affecting the execution time asmapRowis called once for every record in resultset.