简体   繁体   English

为什么我不会在每个延迟加载的关系中使用@BatchSize?

[英]Why would I not use @BatchSize on every lazy loaded relationship?

The @BatchSize annotation of hibernate allows for batched fetching of lazy-loaded entities. hibernate的@BatchSize注释允许批量提取延迟加载的实体。 Eg if i got something like: 例如,如果我有类似的东西:

public class Product {


    @OneToMany(fetchType=LAZY)
    @BatchSize(size=10)
    private ProductCategory category;

}

Now if I get the category of a product, Hibernate will fetch the categories of up to ten more products which are in the current session and have not yet had their category field initialized. 现在,如果我得到产品的类别,Hibernate将获取最多十个产品的类别,这些产品在当前会话中并且尚未初始化其类别字段。 This saves a ton of SQL calls to the database. 这样可以节省大量SQL调用数据库。 So far so good. 到现在为止还挺好。 Now I wonder why would I not use the @BatchSize annotation on EVERY lazy loaded relationship? 现在我想知道为什么我不会在每个懒惰的加载关系上使用@BatchSize注释? After all why would I want extra calls to the database? 毕竟为什么我想要额外调用数据库? There clearly must be a reason for this, otherwise the Hibernate guys could have made it the default, but I currently can't see it. 显然必须有这样的原因,否则Hibernate的人可能会把它作为默认值,但我目前看不到它。

I am not going to answer to your question directly but I am going to answer a more generic question which could be "I have found something that works quicker for me, Why not apply it everywhere ?" 我不会直接回答你的问题,但我会回答一个更通用的问题,可能是“我找到了一些对我来说更快的东西,为什么不在任何地方应用呢?”

The short answer is : You should not do preemptive optimization. 简短的回答是:你不应该做抢先优化。

hibernate is a wonderful ORM that allows all kind of optimization. hibernate是一个很棒的ORM,可以进行各种优化。 You should measure all the processes that causes an issue (the classic N+1 even if it is fast, any slow processes, etc.) and optimize to solve it. 您应该测量导致问题的所有过程(即使是快速的经典N + 1 ,任何缓慢的过程等)并进行优化以解决它。

You might gain a lot better performance by eager loading some properties because you always use them, you might need a BatchSize of 100 for some other because you know it's about the number of relations you have for that property. 通过急切加载某些属性可能会获得更好的性能,因为您总是使用它们,您可能需要将BatchSize为100,因为您知道它与您拥有该属性的关系数量有关。

Ultimately, you should not care about optimization unless you need to care about it. 最终,您不应该关心优化,除非您需要关心它。 And you need to care when you have done measurements and found issues. 当您完成测量并发现问题时,您需要关心。

why would I not use the @BatchSize annotation on EVERY lazy loaded relationship? 为什么我不会在每个延迟加载的关系上使用@BatchSize注释?

Because it is an optimization you might not need in every single case. 因为它是一种优化,您可能不需要在每种情况下。 Batch fetching like this is useful when your application is going to access product.category for many different products , so you can have a single select from category... query executed rather than N of them. 当您的应用程序要访问许多不同products product.category ,这样的批量提取很有用,因此您可以select from category...一个select from category...查询执行而不是N个。

However, what if when your application accesses product.category for one Product instance, it is unlikely to access the category field of other Product instances in the same session? 但是,如果您的应用程序访问一个Product实例的product.category ,则不太可能访问同一会话中其他Product实例的category字段? If you have @BatchSize enabled for that association, then you have just loaded a number of other Category instances into the session for no gain - they will never be used. 如果您为该关联启用了@BatchSize ,那么您刚刚在会话中加载了许多其他Category实例而无法获得 - 它们将永远不会被使用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM