防止 Fluent NHibernate select n+1 [英] Prevent Fluent NHibernate select n+1

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

问题描述

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

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. 急切的孩子
  2. 分解我的对象图(和急切加载)

我真的不想做其中任何一个(尽管我可能会在以后分解图表,因为我预计它会增长)

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

现在....

是否可以告诉 NHibernate(使用 FluentNHibernate),每当我尝试访问子项时,一次性加载它们,而不是在我迭代它们时选择-n+1-ing?

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).

每个子集合(在整个图中)只有大约 20 个成员,但是 20^5 很多,所以我不想在获得根目录时急切加载所有内容,而只是获取所有子集合每当我靠近它时都会收集.

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.

事后的想法......如果我想在渲染孩子时引入分页怎么办?我是否必须在这里打破我的对象图,或者我可以利用一些偷偷摸摸来解决所有这些问题?

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?

推荐答案

在我看来,您希望采用使用域模型的方法,而不是创建特定的 nhibernate 查询来处理这种情况.鉴于此,我建议您查看可以应用于您的集合的批量大小属性.Fluent NHibernate 流畅接口尚不支持此属性,但您可以使用:

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

NHibernate 性能文档将解释批量大小的工作原理.

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

我不知道有什么方法可以从您的域模型中分页.我建议您为需要分页的场景编写 NH 查询.

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.

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

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