- Java 双重比较
- java - 比较器与 Apache BeanComparator
- Objective-C 完成 block 导致额外的方法调用?
- database - RESTful URI 是否应该公开数据库主键?
我为我们的新项目评估了几种 .NET 数据库访问技术,并发现在使用 Entity Framework 查询远程数据库时性能不佳。 Entity Framework 比 LinqToSql 或 SqlClient 慢 10 倍。也许你可以帮我解释或解决这个问题?
测试参数:
数据库:
表结构:
[dbo].[Master](
[Id] [int] IDENTITY(1,1) NOT NULL,
[Value_Bit] [bit] NOT NULL,
[Value_Float] [float] NOT NULL,
[Value_DateTime] [datetime2](7) NOT NULL,
[Value_Uniqueidentifier] [uniqueidentifier] NOT NULL,
[Value_NVarchar100] [nvarchar](100) NOT NULL,
[Value_NVarchar1000] [nvarchar](1000) NOT NULL,
[InsertDate] [datetime] NOT NULL,
[UpdateDate] [datetime] NOT NULL,
[Version] [timestamp] NOT NULL)
基准应用:
数据库访问技术:
计算机(客户端/服务器):
我在作为客户端或服务器的不同计算机上使用每种数据库访问技术直接执行了 sql 查询select * from Master
。平均时间是 1000 次迭代的结果。
测试场景 1:
服务器:A
Entity Framework :平均时间:17 毫秒
测试场景 2:
服务器:B
Entity Framework :平均时间:144 毫秒
测试场景 3:
服务器:C
Entity Framework :平均时间:2145 毫秒
测试场景 4:
服务器:C
Entity Framework :平均时间:2060 毫秒
为什么 Entity Framework 在测试场景 3 和 4 中比 LinqToSQL 或 SqlClient 慢 10 倍?
我使用 Entity Framework 4.3.1、5 (RC) 以及 .NET Framework 4 和 4.5 对其进行了测试,每次都是相同的结果。我禁用延迟加载和跟踪,使用编译查询和预生成 View ,但没有区别。
我使用 SQL Profiler 调查执行的 SQL 查询,发现 Entity Framework 的查询在 SQL Server 中已经花费了两秒钟(测试场景 3)。如果我从计算机 A 上的 Management Studio 执行查询,只需要 100 毫秒。
我使用 dotTrace (http://www.jetbrains.com) 分析了我的基准应用程序,发现大部分执行时间都被 ToList
方法消耗了。如果我深入调用堆栈,我会看到方法 System.Data.SqlClient.SqlDataReader.GetString(Int32)
和最后的 SNINativeMethodWrapper.SNIReadSyncOverAsync(SafeHandle, IntPtr&, Int32)
一直在消耗时间。 LinqToSql 也使用 SqlClient 并且具有几乎相同的调用堆栈,但执行时间快 10 倍。
我不知道幕后发生了什么。也许它与计算机名称解析有关,但我可以通过 IP 地址及其计算机名称 ping 计算机 C。有没有人可以对此进行解释或建议如何加快执行速度?
提前致谢
马蒂亚斯
最佳答案
如果没有看到您的代码,很难给出任何有根据的猜测,但是您可以大致了解一下 EF 的一些典型内容。
通常你可以通过使用 Compiled Queries 来显着加快速度。 .
在查看代码时,EF 查询的延迟执行可能是一个不明显的陷阱。
一个常见的错误是执行返回 IEnumerable 或 IQueryable 集合的查询,然后在循环中使用它:
// Execution will be deferred:
IEnumerable<person> peopleList = objectContext.People.Where(item => item.ID > 100);
foreach (person somePerson in peopleList)
{
// do something here
}
此代码将对数据库进行多次往返,这可能会导致严重的性能问题。由于延迟执行和延迟加载,这将导致人员列表中每个项目 的代码再次查询数据库。根据通过网络传输的数据量,仅此一项就可能对性能造成严重损害。
您可以通过简单地对集合调用 ToList() 方法来减少这种开销。这将在一次往返中获取所有结果对象:
// Execution will be deferred:
List<person> peopleList = objectContext.People.Where(item => item.ID > 100)
.ToList(); // Fetch objects NOW!
MSDN 提供了一篇带有一些建议的文章,Performance Considerations (Entity Framework) :
Strategies for Improving Performance You can improve the overall performance of queries in the Entity Framework by using the following strategies.
Pre-generate views
Generating views based on an entity model is a significant cost the first time that an application executes a query. Use the EdmGen.exe utility to pre-generate views as a Visual Basic or C# code file that can be added to the project during design. You could also use the Text Template Transformation Toolkit to generate pre-compiled views. Pre-generated views are validated at runtime to ensure that they are consistent with the current version of the specified entity model. For more information, see How to: Pre-Generate Views to Improve Query Performance (Entity Framework) and Isolating Performance with Precompiled/Pre-generated Views in the Entity Framework 4. When working with very large models, the following consideration applies: The .NET metadata format limits the number of user string characters in a given binary to 16,777,215 (0xFFFFFF). If you are generating views for a very large model and the view file reaches this size limit, you will get the "No logical space left to create more user strings." compile error. This size limitation applies to all managed binaries. For more information see the blog that demonstrates how to avoid the error when working with large and complex models.
Consider using the NoTracking merge option for queries
There is a cost required to track returned objects in the object context. Detecting changes to objects and ensuring that multiple requests for the same logical entity return the same object instance requires that objects be attached to an ObjectContext instance. If you do not plan to make updates or deletes to objects and do not require identity management , consider using the NoTracking merge options when you execute queries.
Return the correct amount of data
In some scenarios, specifying a query path using the Include method is much faster because it requires fewer round trips to the database. However, in other scenarios, additional round trips to the database to load related objects may be faster because the simpler queries with fewer joins result in less redundancy of data. Because of this, we recommend that you test the performance of various ways to retrieve related objects. For more information, see Shaping Query Results (Entity Framework). To avoid returning too much data in a single query, consider paging the results of the query into more manageable groups. For more information, see How to: Page Through Query Results (Entity Framework).
Limit the scope of the ObjectContext
In most cases, you should create an ObjectContext instance within a using statement (Using…End Using in Visual Basic). This can increase performance by ensuring that the resources associated with the object context are disposed automatically when the code exits the statement block. However, when controls are bound to objects managed by the object context, the ObjectContext instance should be maintained as long as the binding is needed and disposed of manually. For more information, see Managing Resources in Object Services (Entity Framework).
Consider opening the database connection manually
When your application executes a series of object queries or frequently calls SaveChanges to persist create, update, and delete operations to the data source, the Entity Framework must continuously open and close the connection to the data source. In these situations, consider manually opening the connection at the start of these operations and either closing or disposing of the connection when the operations are complete. For more information, see Managing Connections and Transactions in the Entity Framework.
关于database - Entity Framework - 查询远程数据库时性能不佳,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12273058/
这个问题在这里已经有了答案: 10年前关闭。 Possible Duplicates: What is a framework? What does it do? Why do we need a f
我在按照 http://msdn.microsoft.com/en-us/data/jj591621.aspx 处的 Microsoft Data Developer 过程启用代码优先迁移时遇到了一些
我正在从 迁移项目 Entity Framework 4.3 在 .net 4 上运行到 Entity Framework 5 在 .net 4.5 上运行。在不做任何更改的情况下,当我尝试运行该项目
我正在使用 Entity Framework 6 并使用 EntityFramework Extended 来执行一些批量更新和批量删除。批量更新和批量删除工作正常,但我还需要知道更新/删除的实体(即
我在实体上添加了一个列,然后从模型中生成数据库或构建解决方案,然后收到一条消息,提示我刚添加的新列未映射。该数据库以前是从模型创建的,没有错误。 当我右键单击Entity并选择Table Mappin
每次我尝试运行我的代码时都会崩溃,因为我尝试启动函数以调用 SDK 的任何部分。 我在构建过程中包含了 FoundationSDK: 并且我在头文件中包含了对 SDK 的引用: 但是每次我运行这个,我
我以前能够毫无问题地提交我的申请。我的工作流程中唯一改变的部分是使用 Sourcetree。在对以下框架进行更新后,我在提交到 iOS App Store 时收到此警告。我还收到一封电子邮件,其中包含
假设我为 Asp.NET Web 应用程序安装了 .NET Framework 2.0、3.0、3.5。 我意识到 Framework 3.0 和 3.5 只是 Framework 2 的扩展,不太清
是否有 SaveChanges 事件在保存更改后但在更新更改跟踪器之前触发? 我正在使用 EF 6。 我需要在某个实体的状态发生变化时执行任务。 我已经覆盖了 SaveChanges 来设置它。我可以
我正在使用一个现有的数据库,并且我已经将其中一个表映射为一个实体(因为我需要映射一个外键)。 因此,在初始化此数据库时,我希望 EF 忽略此实体,因为它已经存在。 我该怎么做? 最佳答案 您应该使用
我有 3 个表需要与 Entity Framework 进行映射,但我不确定解决此问题的正确方法。这是我的 3 个实体: public class User { [Key] public
我首先使用 VS 2010 和 Entity Framework 代码(版本 6)。我有两个实体,每个实体都在自己的上下文中,我想在它们之间创建一对多关系。 上下文 1 具有以下实体: public
我知道 EF 在 CodePlex 上是开源的,但我没有看到当前发布的 5.0 版本的分支。我在哪里可以得到这个源代码? 最佳答案 没有。他们只开源了 post 5 版本。第一次签到可能足够接近,但再
我们目前有一个数据库很大的系统,存储过程既用于CUD又用于查询。数据集用于从 SP 查询中检索结果。 现在我们正在研究使用 Entity Framework 针对同一个数据库开发另一个项目。在查询数据
我有一个每 10 秒运行一次的 Windows 服务......每次运行时,它都会获取一些测试数据,对其进行修改并使用 EntityFramework 将其保存到数据库中。但是,在每一秒运行时,当我尝
我对在我们的场景中仅将 Entity Framework 与存储过程一起使用的合理性有疑问。 我们计划拥有一个 N 层架构,包括 UI、BusinessLayer (BLL)、DataAccessLa
当我使用 Entity Framework 时,我想在上下文中查询出一条记录并将其添加到具有相同架构的另一个上下文中,在查询出记录后,我将其从上下文中分离出来,但是相关实体都没有了,是吗?有什么办法解
我正在使用 Entity Framework 5 构建 ASP.Net MVC4 Web 应用程序。我必须使用现有的 sql server 数据库,但也想使用 Code First,所以我遵循了本教程
在 Entity Framework 4.0 中使用 T4 模板创建 POCO 会丢失什么?为什么使用 Entity Framework 4.0 时的默认行为不创建 POCO? 最佳答案 你会失去很多
我在网上使用 Repository Pattern 和 EF 看了很多例子。但他们都没有真正谈到与相关实体的合作。 就像说用户可以有多个地址。 IUserRepository User CreateU
我是一名优秀的程序员,十分优秀!