实体框架和LINQ to实体业绩 [英] Entity framework and linq to entity performance

查看:153
本文介绍了实体框架和LINQ to实体业绩的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在研究使用微软的实体框架在工作中的一个项目。我创建了一个简单的小数据层解决方案,它有一个IDataService接口,这样我可以写一个标准的ADO.Net实施和使用实体框架版本的LINQ到实体。

I've been investigating using Microsoft's entity framework for a project at work. I created a simple little data layer solution which has an IDataService interface so I could write a standard ADO.Net implementation and an Entity Framework version using Linq-to-Entity.

我创建了两个测试,其中要求完全相同的数据,但使用不同的实现。的查询简单,便检索数据从一个表中,使用分层信息生成DTO在一个层次结构中的数据。

I've created two tests which request exactly the same data, but use the different implementations. The queries are simple, they retrieve data from a table, and using hierarchical information generate a DTO with the data in a hierarchy.

在数据库中的数据是沿行

The data in the database is along the lines of

------------------------
ID  | Description
----|-------------------
1   | Item 1
2   | Item 2
3   | Item 3
4   | Item 4
5   | Item 5

----------------
Parent | Child  
-------|--------
1      | 2
1      | 3
3      | 4
1      | 5

Desired Output
--------------
Item 1
|-Item 2
|-Item 3
| |-Item 4 
|-Item 5

这样一来查询当前采取的形式是:

And so the queries currently take the form of:

from a in tableA
join b in tableB on b.Parent equals a.ID
where b.Parent == root.ID
select new DTO.Entry {
  Id = a.ID
  ...
}

含有该查询的方法运行递归直到没有更多的子元素留待处理。

The method containing this query is run recursively until there are no more child elements left to process.

使用LINQ到实体测试大约需要320ms才能完成,使用ADO.Net测试大约需要8毫秒!

Using Linq-to-entity the test takes about 320ms to complete, using ADO.Net the tests takes about 8ms!

这是只是我不得不忍受/考虑还是应该的性能大约看齐?此外,作为底层数据结构没有引用完整性(我知道!),所以我在我的ADO.Net东西补偿这一点,但我不能使用实体,这是有可能产生影响?

Is this just something I have to live with/consider or should the performance be about on par? Also, as the underlying data structure has no referential integrity (I know!), so I am compensating for this in my ADO.Net stuff, but I can't with Entities, is this likely to have an impact?

目前看来,如果你想表现,那么你应该用ADO.Net坚持

At the moment it seems that if you want performance then you should stick with ADO.Net

推荐答案

问一个客户:你想更多的性能和更低的错误

Ask a customer: do you want more performance or less bugs?

当然,纯ADO.Net性能比同样使用ADO.Net但在此之前,并且做了很多之后的数据层更好。此外,你选择一个地区,EF是不是一个很好的竞争者,当涉及到效率:递归查询。但一般来说,当谈到写正确的,稳定的code,它执行的可接受的,EF(或任何经验丰富的OR映射器)上升的头部和肩膀以上的ADO.Net。它是手写的SQL和数据驱动的编程与LINQ和面向对象编程。

Of course plain ADO.Net performs better than a data layer that also uses ADO.Net but before and after that does a lot more. Moreover, you chose an area where EF is not a good competitor when it comes to efficiency: recursive queries. But generally, when it comes to writing correct, stable code that performs acceptably, EF (or any seasoned OR mapper) rises head and shoulders above ADO.Net. It is hand-written SQL and data driven programming vs. linq and object oriented programming.

不过,你权当你说

目前看来,如果你想表现,那么你应该用ADO.Net坚持

At the moment it seems that if you want performance then you should stick with ADO.Net

当然,对于性能要求较高的操作或映射器往往有太多的内部架构,以适应该法案。并返回到递归查询:没有什么比递归查询与CTE的数据库

Sure, for performance-critical operations OR mappers tend to have too much internal overhead to fit the bill. And to return to recursive queries: nothing beats recursive queries with CTE's in the database.

这篇关于实体框架和LINQ to实体业绩的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆