LinQ to SQL 及 non-LinQ方式实现Group的Performance对比
拥有476550数据的一张数据表。使用其中的某个字段分组,然后按该字段进行排序。该需求分别使用LinQ to SQL和non-LinQ的方式实现,然后来看一下performance对比。
LinQ way
from p in context.Part_Part
group p by p.FunctionGroup into groupedPs
orderby groupedPs.Key
select groupedPs
LinQ way for group
non-LinQ way
var results = new SortedDictionary<long?, IList<Part_Part>>();
foreach (var p in context.Part_Part)
{
IList<Part_Part> groupedValue = null; if (!results.TryGetValue(p.FunctionGroup, out groupedValue))
{
groupedValue = new List<Part_Part>();
results[p.FunctionGroup] = groupedValue;
} groupedValue.Add(p);
}
non-LinQ way for group
从
var results = new SortedDictionary<long?, IList<Part_Part>>();
可以看出,用来排序的字段类型为long?。先看一下执行时间。
| LinQ Way | non-LinQ Way | |
| first time | 1:6.698 | 6.707 |
| second time | 1:7.404 | 1.426 |
| third time | 1:7.127 | 1.486 |
| forth time | 1:6.952 | 1.425 |
明显可以看出在这个scenario下,LinQ的performance极低。调整代码,这次使用类型为string的PartDescription分组。测试,结果如下。
| LinQ Way | non-LinQ way | |
| first time | >30min | 8.738 |
| second time | >30min | 4.201 |
| third time | >30min | 4.173 |
| forth time | >30min | 4.176 |
这个scenario下,non-LinQ way耗时有所增加,而LinQ way更是惨不忍睹。甚至在苦苦的等了30分钟不见结果后,提早结束了测试程序。
可见,LinQ在带来简洁风和极佳可读性的同时,也带来了性能的损耗。看一段来自于《LinQ in Action》中,关于LinQ性能问题的描述。
There are no surprises. LINQ does not come for free. LINQ queries cause additional work, object creations, and pressure on the garbage collector. The additional cost of using LINQ can vary a lot depending on the query. It can be as low as 5 percent, but can sometimes be around 500 percent.
既如此,以后还能不能愉快的使用LinQ呢?再来看一段《LinQ in Action》中的描述。
Do not be afraid to use LINQ, but use it wisely. For simple operations that are executed extensively in your code, you may consider using the traditional alternatives. For simple filter or search operations, you can stick to the methods offered by List<T> and arrays, such as FindAll, ForEach, Find, ConvertAll, or TrueForAll. Of course, you can continue to use the classic for and foreach statements wherever LINQ would be overkill. For queries that are not executed several times per second, you can probably use LINQ to Objects safely. A query that is executed only once in a non-time-critical context won't make a big difference if it takes 60 milliseconds to execute instead of 10.
LinQ to SQL 及 non-LinQ方式实现Group的Performance对比的更多相关文章
- Linq to Sql : 三种事务处理方式
原文:Linq to Sql : 三种事务处理方式 Linq to SQL支持三种事务处理模型:显式本地事务.显式可分发事务.隐式事务.(from MSDN: 事务 (LINQ to SQL)).M ...
- [Silverlight][linq to sql]不能找到linq to sql自动生成类型
最近在做Silverlight项目,结合使用了WCF RIA service,通过linq to sql自动生成model类型,使用起来非常方便.具体可见linq to sql之silverlight ...
- .NET面试题系列[14] - LINQ to SQL与IQueryable
.NET面试题系列目录 名言警句 "理解IQueryable的最简单方式就是,把它看作一个查询,在执行的时候,将会生成结果序列." - Jon Skeet LINQ to Obje ...
- LINQ To SQL
议程 1.LINQ To SQL概述 2.LINQ To SQL对象模型 3.LINQ To SQL查询 用到的数据库 SQL Server 2005,数据库名为Test. 两张表,分别为Studen ...
- LINQ to SQL大全
LINQ to SQL语句 (1)之Where Where操作 适用场景:实现过滤,查询等功能. 说明:与SQL命令中的Where作用相似,都是起到范围限定也就是过滤作用的,而判断条件就是它后面所接的 ...
- [转]LINQ To SQL 语法及实例大全
转载自:http://blog.csdn.net/pan_junbiao/article/details/7015633 LINQ to SQL语句(1)之Where Where操作 适用场景:实现过 ...
- LINQ to SQL语句非常详细(原文来自于网络)
LINQ to SQL语句(1)之Where Where操作 适用场景:实现过滤,查询等功能. 说明:与SQL命令中的Where作用相似,都是起到范围限定也就是过滤作用的,而判断条件就是它后面所接的子 ...
- linq世界走一走(LINQ TO SQL)
前言:作为linq的一个组件,同时作为ADO.NET的一个组成部分,LINQ TO SQL提供了将关系数据映射为对象的运行时基础结构. LINQ TO SQL是通过将关系数据库对象的数据模型(如一个数 ...
- LINQ To SQL 语法及实例大全
http://blog.csdn.net/pan_junbiao/article/details/7015633 http://blog.csdn.net/pan_junbiao/article/de ...
随机推荐
- DB数据源之SpringBoot+MyBatis踏坑过程(七)手动使用Tomcat连接池
DB数据源之SpringBoot+MyBatis踏坑过程(七)手动使用Tomcat连接池 liuyuhang原创,未经允许禁止转载 系列目录连接 DB数据源之SpringBoot+Mybatis踏坑 ...
- MySQL高可用架构故障自动转移插件MHA
mha高可用架构是目前mysql高可用故障转移比较成熟的解决方案.MHA插件复杂监控mysql主节点的健康情况.在主节点宕机后,MHA把binlog通过ssh传到从节点进行重做补齐.并提升其中一个从节 ...
- [异常笔记] zookeeper集群启动异常: Cannot open channel to 2 at election address ……
- ::, [myid:] - WARN [WorkerSender[myid=]:QuorumCnxManager@] - Cannot open channel to at election ad ...
- PHP基础3--文件加载-错误处理
主要: 1-文件加载 2-错误处理 文件加载 文件加载语句 1) 4个文件加载语句:include, require, include_once, require_once 2) 使用形式 ...
- Python学习:9.模块的安装以及调用模块
什么是模块 在Python中,模块其实也就是包含python代码的文件,我们为什么要使用模块?在我们以后写代码的时候,我们会发现有很多功能需要经常使用,那我们想要使用这些功能怎么办,要再把那些代码在敲 ...
- python matplotlibmat 包mplot3d工具 三维视图透视取消
https://stackoverflow.com/questions/23840756/how-to-disable-perspective-in-mplot3d 简单的解决方法是 ax = fig ...
- autocomplete.jquery 点击或进入默认显示所有结果
注意使用的是autocomplete.jquery,官网地址是:https://github.com/devbridge/jQuery-Autocomplete.而不是JqueryUI的autocom ...
- 用ext_skel,实现一个PHP扩展,添加到PHP并调用
1 创建函数定义文件 #mkdir /home/phpext #vi mydefined.skel string get_text(string str) 2 根据README所提供的信息创建预定义文 ...
- unity3d 计时功能舒爽解决方案
上次也写了一篇计时功能的博客 今天这篇文章和上次的文章实现思路不一样,结果一样 上篇文章地址:http://www.cnblogs.com/shenggege/p/4251123.html 思路决定一 ...
- linux 安装 node.js
wget http://nodejs.org/dist/v0.10.26/node-v0.10.26.tar.gztar zxvf node-v0.10.26.tar.gzcd node-v0.10. ...