親オブジェクトに子オブジェクトを追加するのが非常に遅いという問題があります。何万もの子オブジェクト(この場合は33kレコード)がありますが、それらのどれも問題の親オブジェクトの子ではありません。速度が必要な場合のエンティティフレームワークの遅さ
私はそれが完了するまでに一分以上かかり、親への最初の子を追加します。
public class ParentEntity // POCO generated by EF TT4 templates
{
public virtual int Id { get; set; }
public virtual ICollection<ChildEntity> ChildEntities {}
}
public class ChildEntity // POCO generated by EF TT4 templates
{
public virtual int Id { get; set; }
public virtual int ParentEntityId { get; set; }
public virtual ParentEntity ParentEntity { get; set; }
public virtual Warehouse Warehouse { get; set; }
public virtual WarehouseLocation WarehouseLocation { get; set; }
}
public class Warehouse { // etc } // another POCO class
public class WarehouseLocation { // etc } // another POCO class
// somewhere in a controller action method...
var parent = _parentEntityService.GetBy(id);
var child = new ChildEntity{ ParentEntityId = id,
WarehouseId = id2, WarehouseLocationId = id3 };
// ChildEntities.Add() takes more than one minute to add the
// first and only child to this parent
// why would this be so incredibly slow?
parent.ChildEntities.Add(child);
EntityFrameworkでの速度の問題を発見アプローチする最良の方法は何ですか?
更新:EFProfが、それは3つのSQLクエリを発行することを示しています
SELECT * FROM ChildEntities where ParentId = id
SELECT * FROM ChildEntities where WarehouseId = id2
SELECT * FROM ChildEntities where WarehouseLocation = id3
それだけで唯一の現在の子のためにそれらをロードする必要があるときに、なぜそれは、すべての単一のChildEntityのためにこれらをロードしていますか?
編集2:@LadislavMrnkaによると、追加のクエリはテンプレートのFixupメソッドによって発生します。しかし、私がこれらのメソッドをコメントアウトし、Fixupへの呼び出しをコメントアウトすると、それはまだ遅いです。これは、(それが私に削除されるように見える)フィックスアップを除去するための正しい方法ではありません:
public class ChildEntity {
public virtual Warehouse Warehouse
{
get { return _warehouse; }
set
{
if (!ReferenceEquals(_warehouse, value))
{
var previousValue = _warehouse;
_warehouse = value;
//FixupWarehouse(previousValue); // commented out
}
}
}
このように変更してみてください。 'var parent = _parentEntityService.GetBy(id); var child = new ChildEntity {Foo = ""、Bar = ""、Parent = parent}; _ entityService.Save(子) ' –
EFプロファイラ(http://efprof.com/)を使用してエンティティの内部を確認しますフレームワーク。 –
この 'parent'エンティティには' ChildEntities'が多数ありますか? – Eranga