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 ...
随机推荐
- 【libreoffice】libreoffice实现office转pdf、html、jpg等格式数据
其实libreoffice有好多功能,完全可以替代office 1.windows下将word转为pdf 1 安装libreoffice 到官网下载后安装即可.https://donate.libr ...
- centos6.5环境自动化运维之puppet实现nginx反向代理功能及puppet安装配置详解
puppet是一种Linux.Unix.windows平台的集中配置管理系统,使用自有的puppet描述语言,可管理配置文件.用户.cron任务.软件包.系统服务等.puppet把这些系统实体称之为资 ...
- Android 工程中各种文件的介绍
一:Android.mk Android.mk 内部定义了一个或者多个源代码的模块,该文件的产生是和NDK相关的,NDK 是Android提供的一种工具,可以为编译mk文件内部的源代码提供依赖 ...
- 下载文件的一致性验证之MD5值校验
前几天写脚本遇到下载功能校验,一时间不到怎么校验好? 于是请教大神,大神给出方案如下: 先去了解一下你们的产品针对这个下载功能有没有做什么功能设计...然后再针对这些功能设计去设计测试用例...比如 ...
- [转] php die()与exit()的区别实例详解
1 前言 php中die()与exit()的真正区别是什么因,我们百度一下发现die是退出并释放内存,exit是退出但不释放内存了,那么真的是这样吗,需要的朋友可以参考下 2 正文 网上搜索die与e ...
- Android学习笔记————利用JDBC连接服务器数据库
/******************************************************************************************** * auth ...
- 转:vue+canvas如何实现b站萌系登录界面
https://juejin.im/post/5ae802a46fb9a07ac55fec04
- css怎么让页面上的内容不能被选中
body{ -webkit-user-select:none; -moz-user-select:none; -ms-user-select:none; user-se ...
- My Sql控制台命令
1.连接Mysql 格式: mysql -h主机地址 -u用户名 -p用户密码 1.连接到本机上的MYSQL.首先打开DOS窗口,然后进入目录mysql\bin,再键入命令mysql -u root ...
- poj 2485 求最小生成树中 最长的一条边
Sample Input 1 //T 3 //n0 990 692 //邻接矩阵990 0 179692 179 0Sample Output 692 prim # include <iostr ...
