JDBC fetch size
A blog on java performance and optimization. On JDBC, Hibernate, caching, algorithms, profiling and anything that can make your code run faster.
Sunday, June 21, 2015
JDBC fetch size - commonly forgotten setting
Although this parameter is widely available, I discovered that a lot of projects I have visited did not configure the fetch size and thus was using its default value. Yet, the default value can result in a poor performance on some jdbc drivers.
I want to show you how much you can improve your fetching performance if you adjust the fetch size to your statements. The scale of improvement depends on the driver you use.
Oracle jdbc driver
Assume we have table with 1 million rows and for any reason we have to fetch all records into JVM. How fast you can get all data? What fetch size will Oracle use if you don’t set it explicitly?
![]() |
| Figure 1 - fetching 1M rows with different fetchSize values (local oracle database) |
Figure 1 shows fetching times for different fetch size values for Oracle database. In this example database and java application are located on the same machine. I will show later how it looks for a remote database.
Setting Fetch Size with standard JDBC calls
This is how you can set fetch size for given PreparedStatement using JDBC API:
| PreparedStatement stmt = null; | |
| ResultSet rs = null; | |
| try { | |
| stmt = conn. prepareStatement("select a, b, c from table"); | |
| stmt.setFetchSize(200); | |
| rs = stmt.executeQuery(); | |
| while (rs.next()) { | |
| ... | |
| } | |
| } |
Lets see what happens if fetchSize property is set to 10. When rs.next() is called for first time, the oracle driver fetches first 10 records from database and store them in a memory buffer. So, for next 9 calls to rs.next() records are retrieved from this buffer. After the buffer is fully read, subsequent rs.next()will force driver to fetch a new bunch of rows (10) into the buffer.
So if we want to read 10k rows with fetch size set to 10, the driver will make 1000 round trips to the database using the underlying connection. If we set the fetchSize to 500 the driver will perform only 20 round trips to our database.
Look at Figure 1. Setting fetchSize to 100 gives you a 6 times shorter fetching time then with setting fetchSize to 10. Now, you should know that the default fetchSize for the oracle driver is 10...
Two important comments:
- fetchSize can be set on each Statement or PreparedStatement or even on ResultSet. By default, ResultSet uses fetchSize of Statement from which is born. The default value for Statement or PreparedStatementis jdbc driver specific
- fetchSize is only a hint for the driver – the Oracle driver respects this setting, while other drivers may ignore it and fetch all the records at once, for instance.
Setting Fetch Size with Spring JdbcTemplate
Ad hoc JdbcTemplate instance
| JdbcTemplate jdbc = new JdbcTemplate(dataSource); | |
| jdbc.setFetchSize(200); | |
| jdbc.query("select a, b, c from table", | |
| new RowCallbackHandler() { | |
| @Override | |
| public void processRow(ResultSet rs) throws SQLException { | |
| ... | |
| } | |
| } | |
| ); |
Shared JdbcTemplate instance
| public class MyJdbcDaoImpl extends JdbcDaoSupport implements MyJdbcDao { | |
| @Override | |
| protected void initTemplateConfig() { | |
| getJdbcTemplate().setFetchSize(200); | |
| } | |
| public MyResult loadAll() { | |
| final MyResult result = new MyResult(); | |
| getJdbcTemplate().query("select a, b, c from table", | |
| new RowCallbackHandler() { | |
| @Override | |
| public void processRow(ResultSet rs) throws SQLException { | |
| ... | |
| result.add(...); | |
| } | |
| } | |
| ); | |
| } // end of loadAll | |
| } |
When implementing a DAO that extends JdbcDaoSupport every call to getJdbcTemplate() returns the same shared JdbcTemplate instance. You can mix this with ad-hoc instances. For example, override initTemplateConfig()to set the default for this DAO but use ad-hoc JdbcTemplate for selected queries.
JDBC fetch size的更多相关文章
- SQL Fetch size
JDBC performance tuning with optimal fetch size February 1, 2009 31 Comments Tuning performance usin ...
- hibernate.properties官方属性用例(可用于hibernate.cfg.xml属性参考)
######################### Query Language ######################### ## define query language constant ...
- hibernate.cfg.xml文件的配置模板和不同数据库的配置參数
(1)hibernate.cfg.xml文件的配置模板 <?xml version="1.0" encoding="UTF-8"?> <!DO ...
- Hibernate配置文件中配置各种数据库的driver、URL
hibernate.properties ######################### Query Language ######################### ## define qu ...
- Hibernate配置文件中配置各种数据库链接
hibernate.properties ###################### ### Query Language ### ###################### ## define ...
- Spark SQL笔记——技术点汇总
目录 概述 原理 组成 执行流程 性能 API 应用程序模板 通用读写方法 RDD转为DataFrame Parquet文件数据源 JSON文件数据源 Hive数据源 数据库JDBC数据源 DataF ...
- hibernate学习(一)配置,导包
框架的作用 学过javaWeb基础的已经对web层 jsp servlet ,service 层 ,dao层的jdbc .DBUtils 有了很深的了解 并编写代码实现某种功能 为了提高开发 ...
- JAVA PERSISTENCE API (JPA)
13.2.1. About JPA The Java Persistence API (JPA) is the standard for using persistence in Java proje ...
- java框架之Hibernate(1)-简介及初使用
简介 hibernate 是一个开源 ORM ( Object / Relationship Mipping ) 框架,它是对象关联关系映射的持久层框架,它对 JDBC 做了轻量级的封装,而我们 ja ...
随机推荐
- python - classs内置方法 solt
solt # __solt__ # 是一个类变量,变量值可以是列表.元组或者是可迭代对象,也可以是一个字符串 # (以为这所有实例只有一种数据属性) # # 作用:(为了节省内存空间,减少过多的实例属 ...
- Java泛型方法与泛型类的使用------------(五)
泛型的本质就是将数据类型也参数化, 普通方法的输入参数的值是可以变的,但是类型(比如: String)是不能变的,它使得了在面对不同类型的输入参数的时候我们要重载方法才行. 泛型就是将这个数据类型也搞 ...
- oracle_数据库对象
- java中网络设置代理
三种方式: 1.JVM启动时加参数设置代理 在系统启动时,使用-D项来设置代理. 例如: java -Dhttp.ProxyHost="proxyUrl" -Dhttp.Proxy ...
- 梯度优化算法总结以及solver及train.prototxt中相关参数解释
参考链接:http://sebastianruder.com/optimizing-gradient-descent/ 如果熟悉英文的话,强烈推荐阅读原文,毕竟翻译过程中因为个人理解有限,可能会有谬误 ...
- JDK8 Lambda表达式对代码的简化
只是举个例子: public class LambdaDemo { public static String findData( String name , LambdaInterface finde ...
- nodejs 使用mysql 进行查询的问题
因为返回的是个对象 var selectSql1="select * from spc_word_mst where WORD_ID=? limit 0,1 "var select ...
- tomcat下部署应用helloworld
部署应用(简单)1.到Tomcat的安装目录的webapps目录,可以看到ROOT,examples, tomcat-docs之类Tomcat自带的的目录.2.在webapps目录下新建一个目录mya ...
- 005_nginx414_nginx 414 Request-URI Too Large
一.开发请求一个非常长的请求参数 https://jiaju.jyall.me/backend/dish/getSales/?dishId=167271&dishId=166975&d ...
- CentOS 6.5下PXE+Kickstart无人值守安装操作系统centos7.3
CentOS 6.5下PXE+Kickstart无人值守安装操作系统centos7.3 一.简介 1.1 什么是PXE PXE(Pre-boot Execution Environment,预启动执行 ...
