nHibernate3; EAV データ スキーマから 4xxx レコードを取得します。nHibernate または .NET がこれらのコレクションを初めて初期化するとき、深刻なペナルティが発生します。後続の呼び出しは、より効率的に実行されるように見えます。SQL Server Management Studio で同じクエリを実行すると、返される時間が短くなることが予想されます。
.hbm.xml の代わりに Fluent およびランタイム マッピングを使用する。シリアル化されたマッピングがここで役立つかどうか知りたいですか?
nHibernate Profiler と log4net のログ記録では、あまり先に進むことができなかったようです。このプロセスでは、合計で 140,000 ほどのエンティティがハイドレートされます。
コレクションの初期化ペナルティを示す dotTrace パフォーマンス トレースのスクリーンショットを添付します。
結合と熱心なフェッチタイプを試してみましたが、明らかな結果はありませんでしたが、それらを正しく実装したことを100%確信しているわけではありません-親だけを指定する必要がありますか、それとも子テーブルにもフラグを立てる必要がありますか?
var products = ((HandleSession)_handleSession).Session.CreateCriteria(typeof(Product))
.SetFetchMode("Product", FetchMode.Eager)
.List<Product>()
.AsEnumerable();
web.config 経由でリフレクション オプティマイザーを有効にすると (私は思う):
これは、ほとんどの時間が費やされる場所です。
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 を使用しても、同様の結果が得られるようです。これが新しいトレースです。とりあえずリリースに切り替え、重要なプロジェクトでは 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();
.Eager と .Future を配置して生成された SQL:
this_.ProductID を ProductID0_1_ として、this_.ProductNumber を ProductN2_0_1_ として、this_.ProductName を ProductN3_0_1_ として、this_.InsertedDateTime を Inserted4_0_1_ として、this_.UpdatedDateTime を UpdatedD5_0_1_ として、this_.ProductGUID を ProductG6_0_1_ として、this_.ProductTypeId を ProductT7_0_1_ として、producttyp2_.ProductType_ID を Product_typ6 として選択します。 .ProductTypeName as ProductT2_6_0_ FROM Product this_ inner join ProductType producttyp2_ on this_.ProductTypeId=producttyp2_.ProductTypeID;
productatt0_.ProductId を ProductId2_ として、productatt0_.ProductAttributeID を ProductA1_2_ として、productatt0_.ProductAttributeID を ProductA1_2_1_ として、productatt0_.PositionNumber を Position2_2_1_ として、productatt0_.ValueText を ValueText2_1_ として、productatt0_.ValueBinary を ValueBin4_2_1_ として、productatt0_.ProductID を ProductID2_1 .ExpirationDateTime as Expirati7_2_1_, productatt0_.IsCurrentIndicator as IsCurren8_2_1_, productatt0_.OperationCode as Operatio9_2_1_, productatt0_.OperationDateTime as Operati10_2_1_, productatt0_.OperationSystemName as Operati11_2_1_, productatt0_.OperationUserName as Operati12_2_1_, productatt0_.LastUserPriority as LastUse13_2_1_, attribute1_.AttributeId as Attribut1_1_0_, attribute1_.AttributeName を Attribut2_1_0_ として、attribute1_.DisplayName を DisplayN3_1_0_ として、attribute1_.DataTypeName を DataType4_1_0_ として、attribute1_.ConstraintText を Constrai5_1_0_ として、attribute1_.ConstraintMin を Constrai6_1_0_ として、attribute1_.ConstraintMax を Constrai7_1_0_ として、attribute1_.ValuesMin を ValuesMin1_0_ として、attribute1_Max.1Values として attribute1_Max.1Values としてPrecision1_0_ FROM ProductAttribute productatt0_ inner join Attribute attribute1_ on productatt0_.AttributeID=attribute1_.AttributeId WHERE (productatt0_.IsCurrentIndicator=1) および productatt0_.ProductId in (select this_.ProductID FROM Product this_ inner join ProductType producttyp2_ on this_.ProductTypeId=producttyp2_.ProductTypeID)ConstraintText as Constrain5_1_0_、attribute1_.ConstraintMin as Constrai6_1_0_、attribute1_.ConstraintMax as Constrai7_1_0_、attribute1_.ValuesMin as ValuesMin1_0_、attribute1_.ValuesMax as ValuesMax1_0_、attribute1_.Precision1_0_ FROM ProductAttribute productatt0_ inner join Attribute attribute1_ on productatt0_.AttributeID=attribute1_ (AttributeId=attribute1_. productatt0_.IsCurrentIndicator=1) および productatt0_.ProductId in (select this_.ProductID FROM Product this_ inner join ProductType producttyp2_ on this_.ProductTypeId=producttyp2_.ProductTypeID)ConstraintText as Constrain5_1_0_、attribute1_.ConstraintMin as Constrai6_1_0_、attribute1_.ConstraintMax as Constrai7_1_0_、attribute1_.ValuesMin as ValuesMin1_0_、attribute1_.ValuesMax as ValuesMax1_0_、attribute1_.Precision1_0_ FROM ProductAttribute productatt0_ inner join Attribute attribute1_ on productatt0_.AttributeID=attribute1_ (AttributeId=attribute1_. productatt0_.IsCurrentIndicator=1) および productatt0_.ProductId in (select this_.ProductID FROM Product this_ inner join ProductType producttyp2_ on this_.ProductTypeId=producttyp2_.ProductTypeID)Precision1_0_ FROM ProductAttribute productatt0_ inner join Attribute1_ on productatt0_.AttributeID=attribute1_.AttributeId WHERE (productatt0_.IsCurrentIndicator=1) および productatt0_.ProductId in (select this_.ProductID FROM Product this_ inner join ProductType producttyp2_ on this_.ProductTypeId=producttyp2_.製品タイプ ID)Precision1_0_ FROM ProductAttribute productatt0_ inner join Attribute1_ on productatt0_.AttributeID=attribute1_.AttributeId WHERE (productatt0_.IsCurrentIndicator=1) および productatt0_.ProductId in (select this_.ProductID FROM Product this_ inner join ProductType producttyp2_ on this_.ProductTypeId=producttyp2_.製品タイプ ID)
productgro0_.ProductId を ProductId1_ として、productgro0_.ProductGroupId を ProductG1_1_ として、productgro0_.ProductGroupId を ProductG1_3_0_ として、productgro0_.ProductId を ProductId3_0_ として、productgro0_.GroupId を GroupId3_0_ として選択します。 ProductID FROM Product this_ inner join ProductType producttyp2_ on this_.ProductTypeId=producttyp2_.ProductTypeID)