ado.net entity framework使用不同的方法查询数据的不同性能
2012-06-27 09:44
816 查看
第一部分:重复查询单个实体
第一种:Linq To Entitiess
代码如下:
!--
Code highlighting produced by Actipro CodeHighlighter (freeware)
http://www.CodeHighlighter.com/
-->static void Main(string[] args)
{
DateTime time1;
DateTime time2;
time1 = DateTime.Now;
NorthwindEntities context = new NorthwindEntities();
for (int i = 0; i < 1000; i++)
{
var data = (from c in context.Customers where c.CustomerID == ALFKI select c).FirstOrDefault();
string addr = data.Address;
}
time2 = DateTime.Now;
Console.WriteLine((time2-time1).ToString());
}
查询使用时间为6.2秒左右
第二种:使用Entity SQL
!--
Code highlighting produced by Actipro CodeHighlighter (freeware)
http://www.CodeHighlighter.com/
-->static void Main(string[] args)
{
DateTime time1;
DateTime time2;
time1 = DateTime.Now;
NorthwindEntities context = new NorthwindEntities();
for (int i = 0; i < 1000; i++)
{
var data = context.Customers.Where(it.CustomerID=@Id, new System.Data.Objects.ObjectParameter(Id, ALFKI)).FirstOrDefault();
string addr = data.Address;
}
time2 = DateTime.Now;
Console.WriteLine((time2-time1).ToString());
}
查询使用时间为6.2秒左右
第三种:使用EntityKey 来查询
!--
Code highlighting produced by Actipro CodeHighlighter (freeware)
http://www.CodeHighlighter.com/
-->static void Main(string[] args)
{
DateTime time1;
DateTime time2;
time1 = DateTime.Now;
NorthwindEntities context = new NorthwindEntities();
for (int i = 0; i < 1000; i++)
{
var data = context.GetObjectByKey(new System.Data.EntityKey(NorthwindEntities.Customers, CustomerID, ALFKI)) as Customers;
string addr = data.Address;
}
time2 = DateTime.Now;
Console.WriteLine((time2-time1).ToString());
}
查询使用时间为1秒,没错是一秒
总结:
前两种方法查询所使用的时间都差不多为6.2秒,时间比较长,但是使用第三种方法查询数据仅仅使用了1秒,为什么会相差那么多,区别在于前两种方法每次查询都要从数据库中查找,1000次查询每次的查询时间都一样,但是第三种方法只有第一次是从数据库中查数据,后面的999次都是在context通过主键直接取数据,速度当然会快很多啦,EF4.0之前的实体当含有外键引用时是自动生成对象引用的,而EF4.0现在多了一个外键ID,这样我们可以很方便的通过外键来查询数据
第二部分:查询不同是实体
第一种:Linq To Entitiess
!--
Code highlighting produced by Actipro CodeHighlighter (freeware)
http://www.CodeHighlighter.com/
-->static void Main(string[] args)
{
DateTime time1;
DateTime time2;
NorthwindEntities context = new NorthwindEntities();
//先将Customers所有的主键查询出来
List keys = context.Customers.Select(c => c.CustomerID).Distinct().ToList();
//和上面的context以作区别
NorthwindEntities context_ = new NorthwindEntities();
time1 = DateTime.Now;
foreach (var key in keys)
{
var data = context_.Customers.Where(c => c.CustomerID == key).FirstOrDefault();
string addr = data.Address;
}
time2 = DateTime.Now;
Console.WriteLine((time2 - time1).ToString());
}
查询使用时间0.68秒左右
第二种:使用Entity SQL
!--
Code highlighting produced by Actipro CodeHighlighter (freeware)
http://www.CodeHighlighter.com/
-->static void Main(string[] args)
{
DateTime time1;
DateTime time2;
NorthwindEntities context = new NorthwindEntities();
//先将Customers所有的主键查询出来
List keys = context.Customers.Select(c => c.CustomerID).Distinct().ToList();
//和上面的context以作区别
NorthwindEntities context_ = new NorthwindEntities();
time1 = DateTime.Now;
foreach (var key in keys)
{
var data = context_.Customers.Where(it.CustomerID=@Id, new System.Data.Objects.ObjectParameter(Id, key)).FirstOrDefault();
string addr = data.Address;
}
time2 = DateTime.Now;
Console.WriteLine((time2 - time1).ToString());
}
查询使用时间0.5秒左右
第三种:使用EntityKey 来查询
!--
Code highlighting produced by Actipro CodeHighlighter (freeware)
http://www.CodeHighlighter.com/
-->static void Main(string[] args)
{
DateTime time1;
DateTime time2;
NorthwindEntities context = new NorthwindEntities();
//先将Customers所有的主键查询出来
List keys = context.Customers.Select(c => c.CustomerID).Distinct().ToList();
//和上面的context以作区别
NorthwindEntities context_ = new NorthwindEntities();
time1 = DateTime.Now;
foreach (var key in keys)
{
var data = context_.GetObjectByKey(new System.Data.EntityKey(NorthwindEntities.Customers, CustomerID, key)) as Customers;
string addr = data.Address;
}
time2 = DateTime.Now;
Console.WriteLine((time2 - time1).ToString());
}
查询时间为0.18秒左右
总结:
通过比较第三种方法仍然比前两种来的快,查询效率更高
使用EntityKey来查询数据比其他两种方法来的更快,而且不是快一点点,而是相差有好几倍,这里的比较并没有使用存储过程来比较查询,但是从这里我们可以看出,在平常的应用中如果知道实体的主键尽量用主键来查询
第一种:Linq To Entitiess
代码如下:
!--
Code highlighting produced by Actipro CodeHighlighter (freeware)
http://www.CodeHighlighter.com/
-->static void Main(string[] args)
{
DateTime time1;
DateTime time2;
time1 = DateTime.Now;
NorthwindEntities context = new NorthwindEntities();
for (int i = 0; i < 1000; i++)
{
var data = (from c in context.Customers where c.CustomerID == ALFKI select c).FirstOrDefault();
string addr = data.Address;
}
time2 = DateTime.Now;
Console.WriteLine((time2-time1).ToString());
}
查询使用时间为6.2秒左右
第二种:使用Entity SQL
!--
Code highlighting produced by Actipro CodeHighlighter (freeware)
http://www.CodeHighlighter.com/
-->static void Main(string[] args)
{
DateTime time1;
DateTime time2;
time1 = DateTime.Now;
NorthwindEntities context = new NorthwindEntities();
for (int i = 0; i < 1000; i++)
{
var data = context.Customers.Where(it.CustomerID=@Id, new System.Data.Objects.ObjectParameter(Id, ALFKI)).FirstOrDefault();
string addr = data.Address;
}
time2 = DateTime.Now;
Console.WriteLine((time2-time1).ToString());
}
查询使用时间为6.2秒左右
第三种:使用EntityKey 来查询
!--
Code highlighting produced by Actipro CodeHighlighter (freeware)
http://www.CodeHighlighter.com/
-->static void Main(string[] args)
{
DateTime time1;
DateTime time2;
time1 = DateTime.Now;
NorthwindEntities context = new NorthwindEntities();
for (int i = 0; i < 1000; i++)
{
var data = context.GetObjectByKey(new System.Data.EntityKey(NorthwindEntities.Customers, CustomerID, ALFKI)) as Customers;
string addr = data.Address;
}
time2 = DateTime.Now;
Console.WriteLine((time2-time1).ToString());
}
查询使用时间为1秒,没错是一秒
总结:
前两种方法查询所使用的时间都差不多为6.2秒,时间比较长,但是使用第三种方法查询数据仅仅使用了1秒,为什么会相差那么多,区别在于前两种方法每次查询都要从数据库中查找,1000次查询每次的查询时间都一样,但是第三种方法只有第一次是从数据库中查数据,后面的999次都是在context通过主键直接取数据,速度当然会快很多啦,EF4.0之前的实体当含有外键引用时是自动生成对象引用的,而EF4.0现在多了一个外键ID,这样我们可以很方便的通过外键来查询数据
第二部分:查询不同是实体
第一种:Linq To Entitiess
!--
Code highlighting produced by Actipro CodeHighlighter (freeware)
http://www.CodeHighlighter.com/
-->static void Main(string[] args)
{
DateTime time1;
DateTime time2;
NorthwindEntities context = new NorthwindEntities();
//先将Customers所有的主键查询出来
List keys = context.Customers.Select(c => c.CustomerID).Distinct().ToList();
//和上面的context以作区别
NorthwindEntities context_ = new NorthwindEntities();
time1 = DateTime.Now;
foreach (var key in keys)
{
var data = context_.Customers.Where(c => c.CustomerID == key).FirstOrDefault();
string addr = data.Address;
}
time2 = DateTime.Now;
Console.WriteLine((time2 - time1).ToString());
}
查询使用时间0.68秒左右
第二种:使用Entity SQL
!--
Code highlighting produced by Actipro CodeHighlighter (freeware)
http://www.CodeHighlighter.com/
-->static void Main(string[] args)
{
DateTime time1;
DateTime time2;
NorthwindEntities context = new NorthwindEntities();
//先将Customers所有的主键查询出来
List keys = context.Customers.Select(c => c.CustomerID).Distinct().ToList();
//和上面的context以作区别
NorthwindEntities context_ = new NorthwindEntities();
time1 = DateTime.Now;
foreach (var key in keys)
{
var data = context_.Customers.Where(it.CustomerID=@Id, new System.Data.Objects.ObjectParameter(Id, key)).FirstOrDefault();
string addr = data.Address;
}
time2 = DateTime.Now;
Console.WriteLine((time2 - time1).ToString());
}
查询使用时间0.5秒左右
第三种:使用EntityKey 来查询
!--
Code highlighting produced by Actipro CodeHighlighter (freeware)
http://www.CodeHighlighter.com/
-->static void Main(string[] args)
{
DateTime time1;
DateTime time2;
NorthwindEntities context = new NorthwindEntities();
//先将Customers所有的主键查询出来
List keys = context.Customers.Select(c => c.CustomerID).Distinct().ToList();
//和上面的context以作区别
NorthwindEntities context_ = new NorthwindEntities();
time1 = DateTime.Now;
foreach (var key in keys)
{
var data = context_.GetObjectByKey(new System.Data.EntityKey(NorthwindEntities.Customers, CustomerID, key)) as Customers;
string addr = data.Address;
}
time2 = DateTime.Now;
Console.WriteLine((time2 - time1).ToString());
}
查询时间为0.18秒左右
总结:
通过比较第三种方法仍然比前两种来的快,查询效率更高
使用EntityKey来查询数据比其他两种方法来的更快,而且不是快一点点,而是相差有好几倍,这里的比较并没有使用存储过程来比较查询,但是从这里我们可以看出,在平常的应用中如果知道实体的主键尽量用主键来查询
相关文章推荐
- [新手入门]快速学习 ADO.NET Entity Framework系列文章 #3 -- LINQ-to-SQL、EntitySQL、查询产生器方法(Query builder)三种语法
- [新手入门]快速学习 ADO.NET Entity Framework系列文章 #4 -- 数据新增、删除、修改(ObkectContext的 .SaveChange()方法)
- ADO.NET Entity Framework--不再查询直接更新数据
- ADO.NET Entity Framework如何:使用实体数据模型向导(实体框架)
- ADO.NET Entity Framework 学习初级篇5--ObjectQuery查询及方法
- ADO.NET Entity Framework使用实体数据
- ASP.NET Entity Framework 查询数据表多个"s"的解决方法。
- ado.net entity framework 查询性能比较
- ado.net entity framework 查询性能比较
- 在使用 ADO.NET Entity Framework 时生成的实体类个数少于数据表个数的几种情况及原因分析
- ADO.NET Entity Framework 使用实体数据模型工具
- 使用ADO.NET Entity Framework 4.1进行Code First模式的开发
- Jquery如何序列化form表单数据为JSON对象 C# ADO.NET中设置Like模糊查询的参数 从客户端出现小于等于公式符号引发检测到有潜在危险的Request.Form 值 jquery调用iframe里面的方法 Js根据Ip地址自动判断是哪个城市 【我们一起写框架】MVVM的WPF框架(三)—数据控件 设计模式之简单工厂模式(C#语言描述)
- ADO.NET 的几个示例二,带连接数据查询方法
- ADO.NET Entity Framework 如何:使用 EdmGen.exe 生成模型和映射文件
- 【C#】ADO .Net Entities Framework使用查询语句时遇到的错误
- 在 ADO.NET Entity Framework 中使用存储过程
- 转自:http://m.blog.csdn.net/article/details?id=6554168 在使用order by语句进行查询结果排序时,不同的数据库对于被排序字段数据行为null的情况
- Ado.net Entity Framework 中的多对多查询
- ADO.NET Entity Framework生成实体数据模型