简体   繁体   English

Nhibernate和Unitwork模式的性能非常差

[英]Very poor performance by Nhibernate & Unitwork pattern

I have one complex object like this 我有一个像这样的复杂物体

 A
|
|
|                 |->C(As child)---->it has 4 hastomany properties(each B has 10000 C child)
B(has many child)->
                  |->D(As child)---->it has 4 hastomany properties(each B has 1o000 D child)
                  |->B Has many prop also
|
|
|A has many prop also

I, m suffering with performance. 我,我的表现受苦。 All together while retrieving this record i can expect Nh to fire 1000-2000 queries. 总的来说,在检索此记录时,我可以预期Nh会触发1000-2000个查询。 But Worst performance by NH 10-20K queries it is firing. 但是NH 10-20K的最差性能正在射击。

Here im doing reading and writing on entity A only. 在这里,我只是在实体A上进行读写。 I'm not individually inserting any of A's child nor retrival. 我不会单独插入A的任何孩子,也不会检索。 I'm firign Get command on entity A and writing back Entity A only. 我首先在实体A上获取命令,然后仅写回实体A。

Cascading is taking care of insertion of A's child->its child too. 级联也在照顾插入A的孩子->它的孩子。

here im suffering with Performace very badly, i dont know what to do here. 在这里,我对Performace感到非常痛苦,我不知道该怎么办。

I would guess that your mappings are eagerly fetching all of those children which I would not do. 我想您的映射会急切地获取所有我不会这样做的孩子。 It's hard to say without looking at your mappings but if you see something like this in your mappings this is your problem: 不查看您的映射就很难说,但是如果您在映射中看到这样的内容,这就是您的问题:

HasMany(x => x.OrderLines)
            .FetchType.Select();

or 要么

HasMany(x => x.OrderLines)
            .FetchType.Join();

This means when you load the parent object all of these collections will also be loaded which can lead to the infamous "select n+1" problem. 这意味着,当您加载父对象时,所有这些集合也将被加载,这可能会导致臭名昭著的“ select n + 1”问题。

Below is from the nhibernate documentation: 以下是来自nhibernate文档的信息:

(3) fetch (optional, defaults to join): enables outer-join or sequential select fetching for this association. (3)提取(可选,默认为加入):为此关联启用外部结合或顺序选择提取。 This is a special case; 这是一个特例; for full eager fetching (in a single SELECT) of an entity and its many-to-many relationships to other entities, you would enable join fetching not only of the collection itself, but also with this attribute on the nested element. 为了完全渴望获取(在单个SELECT中)实体及其与其他实体的多对多关系,您不仅要启用集合本身的联接获取,还要启用嵌套元素上的此属性的联接获取。

If you are unable to change your mappings I would recommend creating queries up front to get all the child collections. 如果您无法更改映射,建议您先创建查询以获取所有子集合。 This way you are only doing 1 query per entity rather than the 1000's nhibernate is doing currently via lazy loading. 这样,您每个实体只执行1个查询,而不是通过延迟加载当前执行的1000个nhibernate查询。

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

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