简体   繁体   English

EF快速调用并从存储过程返回,但是将其转换为列表需要20秒才能完成1000条记录

[英]EF calls and returns from stored procedure fast but then converting it to list takes 20 seconds for 1000 records

I am debugging a piece of code which is using EF as ORM. 我正在调试使用EF作为ORM的一段代码。 Now, I am seeing somewhat interesting behavior from the application: 现在,我从应用程序中看到了一些有趣的行为:

This is the code where I'm calling a stored procedure: 这是我调用存储过程的代码:

List<RequestListEntity> results = new List<RequestListEntity>();
var temp = System.Data.Object.ObjectContext.ExecuteFunction<T>("storedProcedure", param);

foreach (var item in temp)
{
    results.Add(item);
}

Observations: 观察结果:

  1. When I run the stored procedure on the server, it is very fast. 当我在服务器上运行存储过程时,它非常快。 It has joins with tables, but it returns 1000 records within a second 它具有与表的联接,但在一秒钟内返回1000条记录
  2. When I call the stored procedure from C# using the code shown above, it also returns within a second and returns objectResult<T> with a total of 1000 entries. 当我使用上面显示的代码从C#调用存储过程时,它还会在一秒钟内返回并返回带有总共1000个条目的objectResult<T>
  3. Now when I try to iterate through the result OR try to convert the result to a List, it is hell of a lot slower. 现在,当我尝试遍历结果尝试将结果转换为List时,它慢得多。

Now this raises a lot of questions: 现在,这引发了很多问题:

  1. If it returning from DB so fast then why mere conversion of 1000 records is taking so much time? 如果它从DB返回的速度如此之快,那么为什么仅转换1000条记录就需要那么多时间呢? Or may be it is still going back to DB for conversion? 还是可能仍要返回数据库进行转换?
  2. Is there anything I can do to make it fast? 有什么我可以做的来加快速度吗? When it calls the function, it returns very fast. 当调用函数时,它返回非常快。

The performance issue is because of lazy loading and object tracking. 性能问题是由于延迟加载和对象跟踪。 when this method called result maps to an entity type, two things happen that don't happen when context.Database.SqlQuery is executed : 当此名为result的方法映射到实体类型时,执行context.Database.SqlQuery时不会发生两件事:

  • The entities are tracked by the context's change tracker. 实体由上下文的更改跟踪器跟踪。
  • The entities perform lazy loading. 实体执行延迟加载。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 EF5从存储过程中收集数据,该存储过程返回列表或不返回任何内容 - EF5 collecting data from stored procedure that returns a list or nothing nHibernate从存储过程中返回零条记录 - nHibernate returns zero records from a stored procedure 使用存储过程从EF 6中的SQL视图获取记录 - Using Stored Procedure to get records from an SQL View in EF 6 安全性:将存储过程转换为EF6 - Security: Converting Stored Procedure into EF6 如何从 EF Core 中的存储过程获取对象列表? - How to get a list of objects from a stored procedure in EF Core? EF6从存储过程中获取具有列表属性的实体 - EF6 Get entity with list property from stored procedure 从存储过程MVC-4获取的视图中显示记录列表 - Display Records List in View Obtained from Stored Procedure MVC-4 从EF 6.0 Code First调用存储过程不会插入记录 - Calling Stored Procedure from EF 6.0 Code First doesn't insert records 从EF调用存储过程返回错误SqlParameter已被另一个SqlParameterCollection包含 - Calling stored procedure from EF returns error The SqlParameter is already contained by another SqlParameterCollection 无法使用EF中的存储过程获取所有记录 - Can't Get ALl Records with Stored Procedure in EF
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM