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 C#算法问题, 不知道怎么处理这个数据的转换
  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!
  • ¥15 drone 推送镜像时候 purge: true 推送完毕后没有删除对应的镜像,手动拷贝到服务器执行结果正确在样才能让指令自动执行成功删除对应镜像,如何解决?
  • ¥15 求daily translation(DT)偏差订正方法的代码
  • ¥15 js调用html页面需要隐藏某个按钮
  • ¥15 ads仿真结果在圆图上是怎么读数的
  • ¥20 Cotex M3的调试和程序执行方式是什么样的?
  • ¥20 java项目连接sqlserver时报ssl相关错误