具有多个结果集的存储过程非常慢

时间:2015-10-22 21:25:23

标签: c# sql-server performance entity-framework stored-procedures

我使用EF6和ObjectContext + edmx模型。存在具有多个结果集的存储过程。事实证明,每当我们调用.GetNextResult时,它动态编译新方法,从而产生巨大的开销和性能。我使用了Perfview和JIT编译统计来计算出来。

using (var db = new SomeEntities()) 
{ 
    var resultSet1 = db.GetAllData(); 
    // handle result set 1 data

    // This causes new dynamically emitted method to be JIT-compiled.
    var resultSet2 = resultSet1.GetNextResult<Class2>(); 
}

有没有办法消除动态编译并仍然使用.GetNextResult? 到目前为止,我必须恢复到MSDN

中显示的旧ADO.net数据阅读器

1 个答案:

答案 0 :(得分:0)

I found solution that works much faster, than .GetNextResult and at the same time less "manual" comparing to ADO.Net DataReader. It is .Translate method. There is MSDN article describing how to use it. Basically

var cmd = db.Database.Connection.CreateCommand();
cmd.CommandText = "[dbo].[GetAllBlogsAndPosts]";
db.Database.Connection.Open();
var reader = cmd.ExecuteReader();

// Read first result set
var blogs = ((IObjectContextAdapter)db).ObjectContext.Translate<EFStoredProcJit.Blog>(reader, "Blogs", MergeOption.AppendOnly);

// Read 2nd result set
reader.NextResult();
var posts = ((IObjectContextAdapter)db).ObjectContext.Translate<EFStoredProcJit.Post>(reader, "Posts", MergeOption.AppendOnly);

I wrote blog post with more details on this and sample app.