iteye_20681 2011-01-07 15:51
浏览 246
已采纳

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

我们在一个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是个不错的选择)

  • 写回答

3条回答 默认 最新

  • ageofnodoubt 2011-01-08 10:38
    关注

    [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,那这样的速度是很糟糕的

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 求差集那个函数有问题,有无佬可以解决
  • ¥15 【提问】基于Invest的水源涵养
  • ¥20 微信网友居然可以通过vx号找到我绑的手机号
  • ¥15 寻一个支付宝扫码远程授权登录的软件助手app
  • ¥15 解riccati方程组
  • ¥15 display:none;样式在嵌套结构中的已设置了display样式的元素上不起作用?
  • ¥15 使用rabbitMQ 消息队列作为url源进行多线程爬取时,总有几个url没有处理的问题。
  • ¥15 Ubuntu在安装序列比对软件STAR时出现报错如何解决
  • ¥50 树莓派安卓APK系统签名
  • ¥65 汇编语言除法溢出问题