简体   繁体   中英

Stored procedure with multiple result sets very slow

I use EF6 with ObjectContext + edmx model. There is a stored procedure with multiple result sets. It turned out every time we call .GetNextResult it dynamically compiles new method making huge overhead and performance hit. I used Perfview and JIT compilation statistics to figure that out.

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>(); 
}

Are there ways to eliminate dynamic compilation and still use .GetNextResult? So far I have to revert back to old ADO.net data reader like showed in MSDN

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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