SQL Fetch size
JDBC performance tuning with optimal fetch size
February 1, 2009 31 Comments
Tuning performance using fetch size is an old technique some of you might already be using this configuration; some may know about it but may not have implemented. Recently I have implemented in my current project, would like to share my experience. In a typical production environment database and application will be running on different physical server. Even if you have high-end server class machine the network traffic between application and database server is one of the key factor of your application performance.
Most of the JDBC drivers’ default fetch size is 10. In normal JDBC programming if you want to retrieve 1000 rows it requires 100 network round trips between your application and database server to transfer all data. Definitely this will impact your application response time. The reason is JDBC drivers are designed to fetch small number of rows from database to avoid any out of memory issues. For example if your query retrieves 1 million rows, the JVM heap memory may not be good enough to hold that large amount of data hence JDBC drivers are designed to retrieve small number (10 rows) of rows at a time that way it can support any number of rows as long as you have better design to handle large row set at your application coding. If you configure fetch size as 100, number of network trips to database will become 10. This will dramatically improve performance of your application.
MySQL
When I was working on MySQL I felt its performance is always better than SQL Server and Oracle. The reason is by default, ResultSets are completely retrieved from database server.
Reference: http://dev.mysql.com/doc/refman/5.0/en/connector-j-reference-implementation-notes.html
Oracle
The default fetch size is 10. So you tend to find Oracle retrieval performance is slower than other server. You can observe executing a query in SQL Plus is faster than JDBC. You can change default fetch size by setting connection property “defaultRowPrefetch”.
Reference: http://download-uk.oracle.com/docs/cd/B25221_04/web.1013/b13593/optimiz011.htm#BEEBHBBG
SQL Server
By default it retrieves all the rows from database unless you specify cursor type in the JDBC driver. Like MySQL you can observe SQL Server JDBC performance is better than Oracle.
Reference: http://technet.microsoft.com/en-us/library/aa342344(SQL.90).aspx
DB2
Seems the default fetch size is 32. Don’t know much about this database as I don’t have this database server. You can change default fetch size by through connection property “block size”.
Important note to consider when tuning fetch size:
- Make sure your JDBC driver supports configuring fetch size.
- The fetch size should be based on your JVM heap memory setting. Since JVM heap size varies between different environment, don’t hard-code fetch size keep it as configurable parameters.
- If your fetch size is large, you might encounter out of memory issue. For example a less number of column tables might support large rows fetch size than more number of columns.
- You can set fetch size based certain datatype like blob, image, etc. We follow certain naming convention for columns for example all image and blob column will have suffix “Blob”. I set high fetch size if the query doesn’t contain “Blob” word otherwise set low fetch size.
Sample Code:
Connection connection = DriverManager.getConnection("");
Statement statement = connection.createStatement();
statement.setFetchSize(1000); // configure the fetch size
ResultSet resultSet = statement.executeQuery("");
http://webmoli.com/2009/02/01/jdbc-performance-tuning-with-optimal-fetch-size/
SQL Fetch size的更多相关文章
- JDBC fetch size
make your java run faster A blog on java performance and optimization. On JDBC, Hibernate, caching, ...
- SQL Server无法收缩日志文件 2 因为逻辑日志文件的总数不能少于 2问题
SQL Server无法收缩日志文件 2 因为逻辑日志文件的总数不能少于 2问题 最近服务器执行收缩日志文件大小的job老是报错 我所用的一个批量收缩日志脚本 USE [master] GO /*** ...
- 可输出sql的PrepareStatement封装
import java.io.InputStream; import java.io.Reader; import java.net.URL; import java.sql.Connection; ...
- Microsoft SQL Server 管理 (常用管理及维护命令)
--查询当前连接的实例名 select @@servername --察看任何数据库属性 sp_helpdb master --设置单用户模式,同时立即断开所有用户 alter database No ...
- 如何识别SQL Server中的CPU瓶颈
原文:如何识别SQL Server中的CPU瓶颈 原文出自: http://www.mssqltips.com/sqlservertip/2316/how-to-identify-sql-server ...
- SQL SERVER统计服务器所有的数据库(数据库文件)、表(表行数)、字段(各字段)等详细信息
原文:SQL SERVER统计服务器所有的数据库(数据库文件).表(表行数).字段(各字段)等详细信息 USE STAT GO SET NOCOUNT ON IF EXISTS(SELECT 1 FR ...
- Spark SQL笔记——技术点汇总
目录 概述 原理 组成 执行流程 性能 API 应用程序模板 通用读写方法 RDD转为DataFrame Parquet文件数据源 JSON文件数据源 Hive数据源 数据库JDBC数据源 DataF ...
- SQL优化思路大全
一.百万级数据库优化方案 1.对查询进行优化,要尽量避免全表扫描,首先应考虑在 where 及 order by 涉及的列上建立索引. 2.应尽量避免在 where 子句中对字段进行 null 值判断 ...
- 数据库SQL优化大总结之 百万级数据库优化方案(转载)
网上关于SQL优化的教程很多,但是比较杂乱.近日有空整理了一下,写出来跟大家分享一下,其中有错误和不足的地方,还请大家纠正补充. 这篇文章我花费了大量的时间查找资料.修改.排版,希望大家阅读之后,感觉 ...
随机推荐
- 《SDN核心技术剖析和实战指南》2.2OpenFlow交换机规范小结
这节的内容相当多,主要是介绍OpenFlow标准特别是流表的相关特性. 典型的OpenFlow标准由三部分组成:安全通道,流表和OpenFlow协议.注意这个标准是一份设备标准,并不单指OpenFlo ...
- 导航条上UIBarButtonItem的更改方法(使用initWithCustomView:btn)
UINavigationController *nav = [[[UINavigationController alloc] initWithRootViewController:self.newMe ...
- 二十六个月Android学习工作总结
1.客户端的功能逻辑不难,UI界面也不难,但写UI花的时间是写功能逻辑的两倍. 2.写代码前的思考过程非常重要,即使在简单的功能,也需要在本子上把该功能的运行过程写出来. 3.要有自己的知识库,可以是 ...
- HDOJ 5093 Battle ships 二分图匹配
二分图匹配: 分别按行和列把图展开.hungary二分图匹配. ... 例子: 4 4 *ooo o### **#* ooo* 按行展开. .. . *ooo o#oo oo#o ooo# **#o ...
- Android模块化编程之引用本地的aar
随着项目越来越多,代码的复用就变得异常重要,这时候就要进行模块化编程,就是把一些通用的组件或者类库做成单独的模块,其他项目直接进行引用就好.针对Android开发最常见的就是Android Libra ...
- Synopsys逻辑工艺库
Synopsys逻辑工艺库(DC综合) 逻辑库包括仅与综合过程有关的信息且通过DC用于设计的综合和优化. 这一信息包括引脚到引脚的时序.面积.引脚类型和功耗以及其他DC需要的必须数据. 逻辑库是一个文 ...
- mybatis之mapper.xml分析
select: id:方法名,在同一个mapper.xml中,要保持唯一 parameterType:指定输入的参数类型,不是必须的,如果不指定,mybatis会自动识别(推荐指定). resultT ...
- Nginx常见502错误
1.配置错误因为nginx找不到php-fpm了,所以报错,一般是fastcgi_pass后面的路径配置错误了,后面可以是socket或者是ip:port2.资源耗尽lnmp架构在处理php时,ngi ...
- Webstorm10.0.3破解程序及汉化包下载、Webstorm配置入门指南
核心提示: WebStorm 是jetbrains公司旗下一款JavaScript 开发工具.被广大中国JS开发者誉为“Web前端开发神器”.“最强大的HTML5编辑器”.“最智能的JavaSscri ...
- 使用ICallbackEventHandler接口更高效实现Ajax
使用ICallbackEventHandler接口可以方便地高效地实现Ajax功能 1.处理页面需实现ICallbackEventHandler接口,此接口有两个方法 a.GetCallbackRes ...