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 ...
随机推荐
- Adroid反编译资料收集
Android反编译神器jadx的使用 https://blog.csdn.net/Fisher_3/article/details/78654450 Android 反编译利器,jadx 的高级技巧 ...
- UML和模式应用5:细化阶段(9)---迈向对象设计
1.前言 开发者如何设计对象,可以采用如下三种方式: 编码:在编码的同时进行设计 绘图然后编码:绘制一些UML,然后转到如上编码方式,在集成开发环境中编码 只绘图,不编码:使用工具从图中生成一切 本章 ...
- 如何提交内核补丁--checkpatch.pl使用【转】
转自:https://blog.csdn.net/qq_29350001/article/details/52056667 转自: http://blog.csdn.net/ganggexiongqi ...
- SYN flooding引发的网络故障
故障现象: 1.应用无法通过外网访问,应用服务器所在的内网网段之间(web和db数据库之间访问丢包严重)不能互相访问 其他网段正常 2.怀疑是网络设备问题,将连接该网段设备的交换机重启后故障依旧,通过 ...
- sklearn聚类模型:基于密度的DBSCAN;基于混合高斯模型的GMM
1 sklearn聚类方法详解 2 对比不同聚类算法在不同数据集上的表现 3 用scikit-learn学习K-Means聚类 4 用scikit-learn学习DBSCAN聚类 (基于密度的聚类) ...
- vue系列之生命周期
代码: <body> <div id="app"> {{message}} </div> <script type="text/ ...
- 解决Linux安装 VMware tools 工具的方法
一:启动linux服务器,并用远程登录工具访问linux服务器 1:启动系统 2:用服务器控制台 :查看点ip地址 3:用客户端 连接服务器 二:挂起 vm虚拟机的 tools 安装光盘 三:开始 ...
- SpringMVC(2):Spring MVC入门
原文出处: 张开涛 2.1.Spring Web MVC是什么 spring Web MVC是一种基于Java的实现了Web MVC设计模式的请求驱动类型的轻量级Web框架,即使用了MVC架构模式的思 ...
- LeetCode(34):搜索范围
Medium! 题目描述: 给定一个按照升序排列的整数数组 nums,和一个目标值 target.找出给定目标值在数组中的开始位置和结束位置. 你的算法时间复杂度必须是 O(log n) 级别. 如果 ...
- 数论-质数 poj2689,阶乘分解,求阶乘的尾零hdu1124, 求尾零为x的最小阶乘
/* 要求出[1,R]之间的质数会超时,但是要判断[L,R]之间的数是否是素数却不用筛到R 因为要一个合数n的最大质因子不会超过sqrt(n) 所以只要将[2,sqrt(R)]之间的素数筛出来,再用这 ...
