×

Loading...
Ad by
  • 最优利率和cashback可以申请特批,好信用好收入offer更好。请点链接扫码加微信咨询,Scotiabank -- Nick Zhang 6478812600。
Ad by
  • 最优利率和cashback可以申请特批,好信用好收入offer更好。请点链接扫码加微信咨询,Scotiabank -- Nick Zhang 6478812600。

Gavin 恳请大家将LAZY LOAD 设成TRUE, How can I avoid n+1 SQL SELECT queries when running a Hibernate query?

以下内容来自 HIBERNATE OFFICIAL WEB SITE https://www.hibernate.org/118.html#A23

How can I avoid n+1 SQL SELECT queries when running a Hibernate query?

Follow the best practices guide! Ensure that all <class> and <collection> mappings specify lazy="true" in Hibernate2 (this is the new default in Hibernate3). Use HQL LEFT JOIN FETCH to specify which associations you need to be retrieved in the initial SQL SELECT.

A second way to avoid the n+1 selects problem is to use fetch="subselect" in Hibernate3.

If you are still unsure, refer to the Hibernate documentation and Hibernate in Action.
Report

Replies, comments and Discussions:

  • 工作学习 / 学科技术讨论 / 【Hibernate Discussion 由于原贴太长,这里再开一贴】昨日发了一篇关于Hibernate工作面试失败有感的贴子,未曾想到引起了众多 XDZM 的兴趣。争论,赞成,反对的都有。希望通过讨论,大伙儿都能有所提高,在技术上,和开会argue时 (“科学骂人”的那种,呵呵)。
    An outstanding question is 一兄弟说了下面的话。此话可能有点极端,但俺基本赞成。您呢?

    For the N-tier web app running in an app server, lazy loading is useless. And by default, we always set it to false. -wukongjj(不经历风雨怎么见彩虹); 10.28 17:08 (#5640620@0)
    • Completely agree. Momory is really cheap these days. Performance outweighs the memory consumption.
      • Performace and scalability is 2 different things. I can develop an application that load everything, code, data into memory as soon as the application launched. The system would have great performance, but it may not be scalable!
        Say for example, I have a huge financial calculator which can use 1 GB memory for the executable + data. So even with a servr with 128 GB memeory, you may only be able to launch about 100 instances.

        With proper design, you can host the application on the same machine that supports 500 concurrent users with 30% less slower.

        You have to balance between budget / performance / scalability.
    • Gavin 千方百计的想大家使用他的LAZY LOADING,
      首先他搞了个OPEN SESSION IN VEW PATTERN, 后来他在写SEAM 的时候又引入了CONVERSTION
      为什么大家还是有点不买账。还是要把他的LAZY LOADING 设成 FALSE? 是什么原因? HIBERNATE ,SEAM 和JPA都出自一个牛人Gavin。
      • 我自己从不用LAZY LOADING, 一则没太有必要, 大不了加内存呗, 二则影响PERFORMANCE, 该LOAD的东西一开始就LOAD不是更好吗?三则不安全, 虽然是OPEN SOURCE,没谁有时间去看它怎么实现的, 保证SYNC很麻烦的, 万一他那儿有BUG怎么办?
        • 大系统, 像Oracle Agile PLM, 没有Lazy Loading, 用户自定义的class, subclass, process extension根本就无从实现. 这是系统custom design 的必须.
        • 这就不对了。lazy loading会导致n+1问题。但是,单纯地设定lazy=false会导致1+1问题(如果你不care多一个数据库的round trip,那当我没说)。fetch=Subselect和join的目的,就是把1+1的那个1去掉,只用一个sql,一个数据库round trip就可以了。
          • Gavin 恳请大家将LAZY LOAD 设成TRUE, How can I avoid n+1 SQL SELECT queries when running a Hibernate query?
            以下内容来自 HIBERNATE OFFICIAL WEB SITE https://www.hibernate.org/118.html#A23

            How can I avoid n+1 SQL SELECT queries when running a Hibernate query?

            Follow the best practices guide! Ensure that all <class> and <collection> mappings specify lazy="true" in Hibernate2 (this is the new default in Hibernate3). Use HQL LEFT JOIN FETCH to specify which associations you need to be retrieved in the initial SQL SELECT.

            A second way to avoid the n+1 selects problem is to use fetch="subselect" in Hibernate3.

            If you are still unsure, refer to the Hibernate documentation and Hibernate in Action.
    • ORM 太落后了, 我们现在用PURE-XML, XML 一存就好了。数据读取全用XPATH,100mb 的XML 有专门的压缩器提高perfromance。
      • 压缩啥,这不是size的问题。你知道Dom4J parse Xpath有多慢吗?Xpath只是一个syntactic sugar而已。你们的应用肯定不是time critical的,也不scalable to thousands of users。除非你们自己implement一个XML representation。
      • pure XML也太落后了, 现在都JSON代替了, 又快又简洁.
    • lazy loading有没有用取决于你怎么使用ORM
      很多人觉得lazy loading没用是因为他们只是简单的做个table 到object的映射,并没有建立一个big object graph.对于前者,每次操作比较清晰简单,涉及到的object不多,所以lazy loading 看不出什么优势;但是对于后者,在一个object graph 中navigation,只有需要在这数据的时候才load可以避免一开始就load大量数据,但是实际上后面并不使用的情况。多数人还是thinking in Database tables, not thinking in Objects。hibernate不仅仅帮你做了table<->object mapping, 还通过association使建立一个object graph 同时隐藏背后数据库操作成为可能。
    • 最近刚好搞了点hibernate performance tuning,如果在内存不是非常紧张的情况下那个lazy-loading对性能提高真的没用,使用二级CACHE,系统性能提高很快!