The rows returned from my test query was about 53,000, so with a fetch size of 10 this would take 5,300 network trips to retrieve all the data and store it in memory. Before tuning this operation took about 2.5 minutes to run.
One thing to keep in mind when tuning the fetch size is memory consumption. You should be sure you will have enough memory available to store the result set. For my example I set the fetch size to 10,000 as my result sets were always very large. I was also working with a batch job which ran during off-peak hours, so I knew my available memory would be plentiful.
After setting the fetch size the operation took 15 seconds to run, way better than before. Nice!
Sample code:
On a dao class extending JdbcDaoSupport I stuck this in an overridden initDao() method.
simpleJdbcTemplate.
setFetchSize(1000);
Here is another good post about JDBC fetch size: http://webmoli.com/2009/02/01/jdbc-performance-tuning-with-optimal-fetch-size/
Thanks so much this solve my problem :)
ReplyDeleteHello Alex,
ReplyDeleteyou are right, we can dramatically improve performance if we set proper fetch size. Unfortunatelly I discovered that many developers do not configure it at all, letting the driver to use its default value...
That is why I have written simple benchmark to show how much we can improve our fetching performance and how to setup this in core jdbc or hibernate or spring templates:
http://makejavafaster.blogspot.com/2015/06/jdbc-fetch-size-performance.html
Developers in my neighborhood usually forget about this setting, so it is always good to remind about this.