`

hibernate.jdbc.fetch_size 和 hibernate.jdbc.batch_size

阅读更多

hibernate.jdbc.fetch_size 50

hibernate.jdbc.batch_size 25

 

这两个选项非常非常非常重要!!!将严重影响Hibernate的CRUD性能!

 

C = create, R = read, U = update, D = delete

 

Fetch Size 是设定JDBC的Statement读取数据的时候每次从数据库中取出的记录条数。

 

例如一次查询1万条记录,对于Oracle的JDBC驱动来说,是不会1次性把1万条取出来的,而只会取出Fetch Size条数,当纪录集遍历完了这些记录以后,再去数据库取Fetch Size条数据。

 

因此大大节省了无谓的内存消耗。当然Fetch Size设的越大,读数据库的次数越少,速度越快;Fetch Size越小,读数据库的次数越多,速度越慢。

 

这有点像平时我们写程序写硬盘文件一样,设立一个Buffer,每次写入Buffer,等Buffer满了以后,一次写入硬盘,道理相同。

 

Oracle数据库的JDBC驱动默认的Fetch Size=10,是一个非常保守的设定,根据我的测试,当Fetch Size=50的时候,性能会提升1倍之多,当Fetch Size=100,性能还能继续提升20%,Fetch Size继续增大,性能提升的就不显著了。

 

因此我建议使用Oracle的一定要将Fetch Size设到50。

 

不过并不是所有的数据库都支持Fetch Size特性,例如MySQL就不支持。

 

MySQL就像我上面说的那种最坏的情况,他总是一下就把1万条记录完全取出来,内存消耗会非常非常惊人!这个情况就没有什么好办法了 :(

 

Batch Size是设定对数据库进行批量删除,批量更新和批量插入的时候的批次大小,有点相当于设置Buffer缓冲区大小的意思。

 

Batch Size越大,批量操作的向数据库发送sql的次数越少,速度就越快。我做的一个测试结果是当Batch Size=0的时候,使用Hibernate对Oracle数据库删除1万条记录需要25秒,Batch Size = 50的时候,删除仅仅需要5秒!!!

//
我们通常不会直接操作一个对象的标识符(identifier), 因此标识符的setter方法应该被声明为私有的(private)。这样当一个对象被保存的时候,只有Hibernate可以为它分配标识符。 你会发现Hibernate可以直接访问被声明为public,private和protected等不同级别访问控制的方法(accessor method)和字段(field)。 所以选择哪种方式来访问属性是完全取决于你,你可以使你的选择与你的程序设计相吻合。

所有的持久类(persistent classes)都要求有无参的构造器(no-argument constructor); 因为Hibernate必须要使用Java反射机制(Reflection)来实例化对象。构造器(constructor)的访问控制可以是私有的(private), 然而当生成运行时代理(runtime proxy)的时候将要求使用至少是package级别的访问控制,这样在没有字节码编入 (bytecode instrumentation)的情况下,从持久化类里获取数据会更有效率一些。

 

 

 

hibernate.max_fetch_depth 设置外连接抓取树的最大深度

取值. 建议设置为03之间

就是每次你在查询时,会级联查询的深度,譬如你对关联vo设置了eager的话,如果fetch_depth值太小的话,会发多很多条sql

 

 

batch-size可以设定在Hbm的class 和 集合定义中. 开始我一直以为batch-size是获取child的数量, 其实真正获取的是parent的数量.

但是奇怪的是当我测试时  我把batch-size的值从1设置到5, 当设成1和2时其实每次获取的还是一个parent.当到3时就是同时获取3个parent.当多出select出来的parent时,感觉好像就没有规律了.我建议一般都把batch-size设成3

下面的来自hibernate文档.(奇怪的是hibernate中文文档并没有关于 batch fetching 的介绍.)


Hibernate can make efficient use of batch fetching, that is, Hibernate can load several uninitialized proxies if one proxy is accessed. Batch fetching is an optimization for the lazy loading strategy. There are two ways you can tune batch fetching: on the class and the collection level.

Batch fetching for classes/entities is easier to understand. Imagine you have the following situation at runtime: You have 25 Cat instances loaded in a Session, each Cat has a reference to its owner, a Person. The Person class is mapped with a proxy, lazy="true". If you now iterate through all cats and call getOwner() on each, Hibernate will by default execute 25 SELECT statements, to retrieve the proxied owners. You can tune this behavior by specifying a batch-size in the mapping of Person:

<class name="Person" lazy="true" batch-size="10">...</class>Hibernate will now execute only three queries, the pattern is 10, 10, 5. You can see that batch fetching is a blind guess, as far as performance optimization goes, it depends on the number of unitilized proxies in a particular Session.

You may also enable batch fetching of collections. For example, if each Person has a lazy collection of Cats, and 10 persons are currently loaded in the Sesssion, iterating through all persons will generate 10 SELECTs, one for every call to getCats(). If you enable batch fetching for the cats collection in the mapping of Person, Hibernate can pre-fetch collections:

<class name="Person">
    <set name="cats" lazy="true" batch-size="3">
        ...
    </set>
</class>With a batch-size of 3, Hibernate will load 3, 3, 3, 1 collections in 4 SELECTs. Again, the value of the attribute depends on the expected number of uninitialized collections in a particular Session.

分享到:
评论
3 楼 Smile__ 2013-09-11  
不过这个东西以前还真没研究过 。
2 楼 Smile__ 2013-09-11  
想不到你也是北大青鸟的 。哈
1 楼 zl239 2012-07-14  

相关推荐

    优化Hibernate性能的几点建议

    中取出的记录条数,一般设置为30、50、100.Oracle数据库的JDBC驱动默认的Fetch Size=15,...<prop key="hibernate.jdbc.batch_size">100</prop> --> <prop key="hibernate.jdbc.batch_size">50 </property>

    hibernate.properties

    #hibernate.default_batch_fetch_size 8 ## rollback generated identifier values of deleted entities to default values #hibernate.use_identifier_rollback true ## enable bytecode reflection...

    Hibernate配置各种数据源详解

    Hibernate配置各种数据源 &lt;hibernate-configuration&gt; &lt;!– 各属性的配置–&gt; &lt;!—为true表示将Hibernate发送给数据库的sql显示出来 –&gt; ...”dialect”&gt;...”jdbc.batch_size”&gt;30&lt;/property&gt;

    Hibernate 课件_配置文件详解

    我培训用的 Hibernate课件, 个人能力范围内...jdbc.fetch_size;jdbc.batch_size; POJO 类和数据库的映射文件*.hbm.xml;主键生成策略generator;映射集合属性;延迟加载策略;映射组件属性;关联关系映射:双向 1-N;继承映射

    Hibernate 中文 html 帮助文档

    19.1.2. 调整抓取策略(Tuning fetch strategies) 19.1.3. 单端关联代理(Single-ended association proxies) 19.1.4. 实例化集合和代理(Initializing collections and proxies) 19.1.5. 使用批量抓取(Using...

    HibernateAPI中文版.chm

    3.4. Hibernate JDBC和连接(connection)属性 3.5. Hibernate缓存属性 3.6. Hibernate事务属性 3.7. 其他属性 3.8. Hibernate SQL方言 (hibernate.dialect) 3.9. Hibernate日志类别 3.10. JTA ...

    Hibernate_3.2.0_符合Java习惯的关系数据库持久化

    3.4. Hibernate JDBC和连接(connection)属性 3.5. Hibernate缓存属性 3.6. Hibernate事务属性 3.7. 其他属性 3.8. Hibernate SQL方言 (hibernate.dialect) 3.9. Hibernate日志类别 3.10. JTA ...

    Hibernate中文详细学习文档

    19.1.2. 调整抓取策略(Tuning fetch strategies) 19.1.3. 单端关联代理(Single-ended association proxies) 19.1.4. 实例化集合和代理(Initializing collections and proxies) 19.1.5. 使用批量抓取(Using...

    Hibernate+中文文档

    3.4. Hibernate JDBC和连接(connection)属性 3.5. Hibernate缓存属性 3.6. Hibernate事务属性 3.7. 其他属性 3.8. Hibernate SQL方言 (hibernate.dialect) 3.9. Hibernate日志类别 3.10. JTA ...

    hibernate3.2中文文档(chm格式)

    3.4. Hibernate JDBC和连接(connection)属性 3.5. Hibernate缓存属性 3.6. Hibernate事务属性 3.7. 其他属性 3.8. Hibernate SQL方言 (hibernate.dialect) 3.9. Hibernate日志类别 3.10. JTA ...

    hibernate3.6 文档(pdf 格式)

    3.3. JDBC connections ............................................................................................ 32 3.4. Optional configuration properties ..............................................

    hibernate3.04中文文档.chm

    20.1.2. 调整抓取策略(Tuning fetch strategies) 20.1.3. 单端关联代理(Single-ended association proxies) 20.1.4. 实例化集合和代理(Initializing collections and proxies) 20.1.5. 使用批量抓取...

    Hibernate Reference Documentation3.1

    3.3. JDBC connections 3.4. Optional configuration properties 3.4.1. SQL Dialects 3.4.2. Outer Join Fetching 3.4.3. Binary Streams 3.4.4. Second-level and query cache 3.4.5. Query Language Substitution...

    最全Hibernate 参考文档

    3.3. JDBC连接 3.4. 可选的配置属性 3.4.1. SQL方言 3.4.2. 外连接抓取(Outer Join Fetching) 3.4.3. 二进制流 (Binary Streams) 3.4.4. 二级缓存与查询缓存 3.4.5. 查询语言中的替换 3.4.6. Hibernate的统计...

    Hibernate教程

    Hibernate参考文档 目录 前言 1. 翻译说明 2. 版权声明 1. 在Tomcat中快速上手 1.1. 开始Hibernate之旅 1.2. 第一个持久化类 1.3. 映射cat 1.4. 与Cat同乐 1.5. 结语 2. Hibernate入门 2.1. 前言 2.2. 第...

    hibernate 体系结构与配置 参考文档(html)

    1. Hibernate入门 1.1. 前言 1.2. 第一部分 - 第一个Hibernate应用程序 1.2.1. 第一个class 1.2.2. 映射文件 1.2.3. Hibernate配置 1.2.4. 用Ant构建 1.2.5. 启动和辅助类 1.2.6. 加载并存储对象 1.3. 第...

    精通 Hibernate:Java 对象持久化技术详解(第2版).part2

     16.3.4 批量延迟检索和批量立即检索(使用batch-size属性)  16.3.5 用带子查询的select语句整批量初始化orders集合(fetch属性为“subselect”)  16.3.6 迫切左外连接检索(fetch属性为“join”)  16.4 多对...

    hibernate 框架详解

    目录 前言 1.... 2.... 1. 在Tomcat中快速上手 ... 1.1. 开始Hibernate之旅 1.2.... 1.3.... 1.4.... 调整抓取策略(Tuning fetch strategies) 20.1.3. 单端关联代理(Single-ended association proxies) ...

    Hibernate3+中文参考文档

    3.3. JDBC连接 3.4. 可选的配置属性 3.4.1. SQL方言 3.4.2. 外连接抓取(Outer Join Fetching) 3.4.3. 二进制流 (Binary Streams) 3.4.4. 二级缓存与查询缓存 3.4.5. 查询语言中的替换 3.4.6. Hibernate的统计...

    Hibernate参考文档

    19.1.2. 调整抓取策略(Tuning fetch strategies) 19.1.3. 单端关联代理(Single-ended association proxies) 19.1.4. 实例化集合和代理(Initializing collections and proxies) 19.1.5. 使用批量抓取(Using...

Global site tag (gtag.js) - Google Analytics