nHibernate3;从 EAV 数据模式中检索 4xxx 记录。当 nHibernate 或 .NET 第一次初始化这些集合时,我们会看到严重的惩罚。后续调用的执行效率似乎更高。在 SQL Server Management Studio 中运行相同的查询会产生预期的快速返回时间。
使用 Fluent 和运行时映射而不是 .hbm.xml;好奇序列化映射是否有帮助?
nHibernate Profiler 和 log4net 日志记录似乎没有给我太多的帮助。在此过程中,总共约有 140,000 个实体被水合。
Attached a screenshot of my dotTrace performance tracing that shows the collection initialization penalty:
![dotTrace of slow nHibernate collection initialization](https://i.stack.imgur.com/oCTmu.png)
已经尝试过 join 和 eager fetchtypes,没有明显的结果,但我不能 100% 确定我正确地实现了这些 - 只是父表需要如此指定,还是子表也需要标记?
var products = ((HandleSession)_handleSession).Session.CreateCriteria(typeof(Product))
.SetFetchMode("Product", FetchMode.Eager)
.List<Product>()
.AsEnumerable();
With reflection optimizer enabled (I think) via web.config:
![With reflection optimizer enabled](https://i.stack.imgur.com/bvjwp.png)
这是花费最多时间的地方:
return new ProductList(products.Select(p => p.ToProductContract()));
这只是执行此操作的扩展方法:
public static ProductContract ToProductContract(this Product product)
{
return new ProductContract
{
Name = product.ProductName,
ProductTypeName = product.ProductType.ProductTypeName,
UpdateTimeStamp = product.UpdateDateTime,
ProductNumber = product.ProductNumber,
Attributes = product.ProductAttributes.ToCommonAttribute().ToList(),
GroupCategories = product.ProductGroups.ToGroupCategory().ToList(),
PublicUniqueId = product.PublicUniqueId
};
}
映射:
internal class ProductMapping : ClassMap<Product>
{
private const string _iscurrentindicator = "IsCurrentIndicator=1";
public ProductMapping()
{
Table("Product");
Id(Reveal.Member<Product>("ProductId")).GeneratedBy.Identity().Column("ProductID");
Map(x => x.ProductNumber).Column("ProductNumber").Not.Nullable();
Map(x => x.ProductName).Column("ProductName").Not.Nullable();
Map(x => x.InsertDateTime).Column("InsertedDateTime").Nullable().ReadOnly();
Map(x => x.UpdateDateTime).Column("UpdatedDateTime").Nullable();
Map(x => x.PublicUniqueId).Column("ProductGUID").Generated.Insert();
References(x => x.ProductType).Column("ProductTypeId").Not.Nullable();
HasMany(x => x.ProductAttributes)
.KeyColumn("ProductId")
.Inverse()
.Fetch
.Subselect()
.Where(_iscurrentindicator)
.Cascade
.SaveUpdate();
HasMany(x => x.ProductGroups).KeyColumn("ProductId").Fetch.Subselect().Where(_iscurrentindicator);
DynamicUpdate();
DynamicInsert();
BatchSize(500);
}
}
internal class ProductGroupMapping : ClassMap<ProductGroup>
{
public ProductGroupMapping()
{
Table("ProductGroup");
Id(x => x.ProductGroupId).Column("ProductGroupId").GeneratedBy.Identity();
References(x => x.Product).Column("ProductId").Not.Nullable();
References(x => x.Group).Column("GroupId").Not.Nullable();
//Where("IsCurrentIndicator=1");
}
}
internal class ProductAttributeMapping : ClassMap<ProductAttribute>
{
public ProductAttributeMapping()
{
Table("ProductAttribute");
LazyLoad();
Id(x => x.ProductAttributeId).GeneratedBy.Identity().Column("ProductAttributeID");
References(x => x.Product).Column("ProductID").Not.Nullable();
References(x => x.Attribute).Column("AttributeID").Not.Nullable().Fetch.Join();
Map(x => x.PositionNumber).Column("PositionNumber").Nullable();
Map(x => x.ValueText).Column("ValueText").Nullable();
Map(x => x.ValueBinary).Column("ValueBinary").Nullable();
Component(x => x.OperationalAuditHistory, m =>
{
Table("ProductAttribute");
m.Map(x => x.ExpirationDateTime).Column("ExpirationDateTime").Nullable();
m.Map(x => x.IsCurrent).Column("IsCurrentIndicator").Not.Nullable();
m.Map(x => x.OperationCode).Column("OperationCode").Nullable();
m.Map(x => x.OperationDateTime).Column("OperationDateTime").Nullable();
m.Map(x => x.OperationSystemName).Column("OperationSystemName").Nullable();
m.Map(x => x.OperationUserName).Column("OperationUserName").Nullable();
m.Map(x => x.LastUserPriority).Column("LastUserPriority").Nullable();
});
DynamicInsert();
BatchSize(50);
}
}
不幸的是,对于 .Future,我似乎仍然得到了类似的结果。这是一条新的踪迹;目前我已经切换到Release,关键项目使用x64,所以时间变短了,但是比例还是差不多;以及 .Eager:
var products = ((HandleSession) _handleSession).Session.CreateCriteria(typeof (Product))
.SetFetchMode("ProductAttribute", FetchMode.Join)
.SetFetchMode("ProductGroup", FetchMode.Join)
.SetFetchMode("ProductType", FetchMode.Join)
.Future<Product>()
.AsEnumerable();
![dotTrace - Release mode, targeting x64, with .Future()](https://i.stack.imgur.com/vlKoA.png)
生成带有 .Eager 和 .Future 的 SQL:
选择 this_.ProductID 作为
ProductID0_1_, this_.ProductNumber 为
ProductN2_0_1_, this_.ProductName 为
产品N3_0_1_,this_.InsertedDateTime
作为插入4_0_1_,
this_.UpdatedDateTime 为
更新D5_0_1_,this_.ProductGUID为
ProductG6_0_1_, this_.ProductTypeId 为
产品T7_0_1_,
ProductType2_.ProductTypeID 为
产品T1_6_0_,
产品类型2_.产品类型名称为
产品T2_6_0_来自产品this_
内连接 ProductType ProductType2_ on
this_.ProductTypeId=producttyp2_.ProductTypeID;
选择productatt0_.ProductId为
产品ID2_,
产品att0_.ProductAttributeID为
产品A1_2_,
产品att0_.ProductAttributeID为
产品A1_2_1_,
产品att0_.PositionNumber为
Position2_2_1_,productatt0_.ValueText
作为 ValueText2_1_,
产品att0_.ValueBinary为
ValueBin4_2_1_,productatt0_.ProductID
作为产品ID2_1_,
Productatt0_.AttributeID 为
属性6_2_1_,
Productatt0_.ExpirationDateTime 为
到期7_2_1_,
Productatt0_.IsCurrentIndicator 为
是Curren8_2_1_,
产品att0_.操作代码为
操作9_2_1_,
产品att0_.OperationDateTime 为
操作10_2_1_,
Productatt0_.OperationSystemName 为
操作11_2_1_,
Productatt0_.OperationUserName 为
操作12_2_1_,
Productatt0_.LastUserPriority 为
最后使用13_2_1_,
attribute1_.AttributeId 为
属性1_1_0_,
attribute1_.AttributeName 为
属性2_1_0_,
attribute1_.DisplayName 为
显示N3_1_0_,
attribute1_.DataTypeName 为
数据类型4_1_0_,
attribute1_.ConstraintText 为
约束5_1_0_,
attribute1_.ConstraintMin 为
约束6_1_0_,
attribute1_.ConstraintMax 为
Constrai7_1_0_,attribute1_.ValuesMin
作为 ValuesMin1_0_,
attribute1_.ValuesMax 为
ValuesMax1_0_,attribute1_.Precision
作为 Precision1_0_ FROM ProductAttribute
Productatt0_内连接属性
属性1_开
产品att0_.AttributeID=attribute1_.AttributeId
在哪里
(productatt0_.IsCurrentIndicator=1)
和productatt0_.ProductId(选择
this_.ProductID 来自产品 this_
内连接 ProductType ProductType2_ on
this_.ProductTypeId=producttyp2_.ProductTypeID)
选择productgro0_.ProductId为
产品ID1_,
Productgro0_.ProductGroupId 为
产品G1_1_,
Productgro0_.ProductGroupId 为
产品G1_3_0_、产品gro0_.产品Id
作为 ProductId3_0_、productgro0_.GroupId
作为 GroupId3_0_ FROM ProductGroup
产品gro0_ 哪里
(productgro0_.IsCurrentIndicator=1)
和productgro0_.ProductId(选择
this_.ProductID 来自产品 this_
内连接 ProductType ProductType2_ on
this_.ProductTypeId=producttyp2_.ProductTypeID)