如何完美的解释”延迟加载“提高查询性能这个问题?

我们在一个1对多的映射实体中这样配置了信息:
@OneToMany(mappedBy = "protocolType", cascade = CascadeType.ALL,fetch=FetchType.LAZY)
@OrderBy("name ASC")
public List getLstMntConfProt()
{
return lstMntConfProt;
}

public void setLstMntConfProt(List<MntConfProt> lstMntConfProt)
{
    this.lstMntConfProt = lstMntConfProt;
}

在同样对查询中fetch=FetchType.LAZY的查询效率是4S,
而使用 fetch=FetchType.Eager耗时 123S,
请问如何完美的解释这个问题? 如果只是简单笼统的说 延迟加载可以提高性能 很难说服每个人!(打印出两者执行的sql是个不错的选择)

[quote]在同样对查询中fetch=FetchType.LAZY的查询效率是4S,
而使用 fetch=FetchType.Eager耗时 123S, [/quote]

[code="java"]
public class DictionaryType {

private List<DictionaryData> dictionaryDatas = new ArrayList<DictionaryData>(0);

}
[/code]
DictionaryType与DictionaryData是一对多的关系

假如现在有1条type_code='FUNC'的DictionaryType记录,对应着1000个DictionaryData

那么,假设我们要查询出所有DictionaryType
1、使用FetchType.LAZY
[code="sql"]select this_.dict_type_id as dict1_1_0_, this_.type_code as type2_1_0_, this_.type_name as type3_1_0_ from dictionary_type this_ where this_.type_code='FUNC'[/code]结果是1条记录,共3个字段

2、使用FetchType.Eager
[code="sql"]select this_.dict_type_id as dict1_1_1_, this_.type_code as type2_1_1_, this_.type_name as type3_1_1_, dictionary2_.dict_type_id as dict5_3_, dictionary2_.dict_data_id as dict1_3_, dictionary2_.dict_data_id as dict1_0_0_, dictionary2_.data_code as data2_0_0_, dictionary2_.data_name as data3_0_0_, dictionary2_.dict_type_id as dict5_0_0_, dictionary2_.priority as priority0_0_ from dictionary_type this_ left outer join dictionary_data dictionary2_ on this_.dict_type_id=dictionary2_.dict_type_id where this_.type_code='FUNC'[/code]

结果是1000条记录,共10个字段

相比而言,是不是会慢很多呢?
如果有10个DictionaryType,FetchType.Eager查询出来的可能有上万条记录,而我们可能只想显示DictionaryType,而暂时用不到DictionaryData,那这样的速度是很糟糕的

延迟加载能提高性能,是说当你可能不用到被延迟加载的东西(如你的getLstMntConfProt()),不用查出来就是搞高性能了。

如果你总是用到被延迟加载的东西,那就不能提高性能了。

延迟加载

  延迟加载(lazy load)是Hibernate3关联关系对象默认的加载方式,延迟加载机制是为了避免一些无谓的性能开销而提出来的,所谓延迟加载就是当在真正需要数据的时候,才真正执行数据加载操作。可以简单理解为,只有在使用的时候,才会发出sql语句进行查询。
  延迟加载的有效期是在session打开的情况下,当session关闭后,会报异常。   Hibernate2实现延迟加载有2种方式:1.实体对象 2.集合