防止流利的NHibernate选择n + 1 [英] Prevent Fluent NHibernate select n+1

查看:121
本文介绍了防止流利的NHibernate选择n + 1的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个相当深的对象图(5-6个节点),当我遍历它的一部分时,NHProf告诉我我有一个选择N + 1的问题(我这样做)。



我知道的两个解决方案是


  1. 急切的载入儿童

  2. 分解我的对象图(和急切的加载)

我真的不想做这些(虽然以后我可能会把它分解出来,因为我越来越喜欢它)



现在....



有没有可能告诉NHibernate(与FluentNHibernate),每当我尝试访问孩子,一次加载所有,而不是select-n + 1-ing迭代他们?

我还得到了无限结果集,这大概是同样的问题(或者说,如果可能,将由上述解决方案解决)。

每个子集(整个图)只有20个成员,但是20 ^ 5是很多的,所以我不想急于加载什么时候我得到根,但只要得到所有的孩子集合,每当我走近它。



编辑:一个事后....如果我想介绍当我想要渲染孩子时分页?我是否有必要在这里打破我的对象图,还是有一些潜行我可以用来解决所有这些问题? 解决方案

听起来,你想追求使用你的域模型的方法,而不是创建一个特定的nhibernate查询来处理这种情况。鉴于此,我建议你看看你可以应用到你的集合的批量大小的属性。流利的NHibernate流畅界面还​​不支持这个属性,但作为一个解决方案,您可以使用:
$ b $ pre $ HasMany(x => ; x.Children).AsSet()。SetAttribute(batch-size,20)

鉴于您的具体情况普遍缺乏信息,我不能确定批量是否是理想的解决方案,但我当然建议您放弃。如果你还没有,我建议你阅读这些:



http://www.nhforge.org/wikis/howtonh/lazy-loading-eager-loading.aspx



http://nhibernate.info/doc/nhibernate-reference/ performance.html



NHibernate性能文档将解释批量大小的工作方式。



编辑:我不知道有任何方式来从您的域模型页面。我建议你为需要分页的场景编写NH查询。


I have a fairly deep object graph (5-6 nodes), and as I traverse portions of it NHProf is telling me I've got a "Select N+1" problem (which I do).

The two solutions I'm aware of are

  1. Eager load children
  2. Break apart my object graph (and eager load)

I don't really want to do either of these (although I may break the graph apart later as I forsee it growing)

For now....

Is it possible to tell NHibernate (with FluentNHibernate) that whenever I try to access children, to load them all in one go, instead of select-n+1-ing as I iterate over them?

I'm also getting "unbounded results set"s, which is presumably the same problem (or rather, will be solved by the above solution if possible).

Each child collection (throughout the graph) will only ever have about 20 members, but 20^5 is a lot, so I don't want to eager load everything when I get the root, but simply get all of a child collection whenever I go near it.

Edit: an afterthought.... what if I want to introduce paging when I want to render children? Do I HAVE to break my object graph here, or is there some sneakiness I can employ to solve all these issues?

解决方案

It sounds to me that you want to pursue the approach of using your domain model rather than creating a specific nhibernate query to handle this scenario. Given this, I would suggest you take a look at the batch-size attribute which you can apply to your collections. The Fluent NHibernate fluent interface does not yet support this attribute, but as a work around you can use:

HasMany(x => x.Children).AsSet().SetAttribute("batch-size", "20")

Given the general lack of information about your exact scenario, I cannot say for sure whether batch-size is the ideal solution, but I certainly recommend you give it a go. If you haven't already, I suggest you read these:

http://www.nhforge.org/wikis/howtonh/lazy-loading-eager-loading.aspx

http://nhibernate.info/doc/nhibernate-reference/performance.html

The NHibernate performance documentation will explain how batch-size works.

Edit: I am not aware of any way to page from your domain model. I recommend you write NH queries for scenarios where paging is required.

这篇关于防止流利的NHibernate选择n + 1的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆