SubSonic LINQ 查询比 SubSonic.Query.Select 慢 3 倍
我开发的模块进行了大量小的选择、插入和更新。由 SubSonic.Query
命名空间中的命令进行的修改 (ActiveRecord 是不是我选择的武器)似乎比用LINQ
编写的按ID选择查询要快得多。
执行以下 LINQ
查询 1000 次需要 7.15 秒
long a = (
from u in UserCollection
where u.UserId == value
select u.UserId
).FirstOrDefault<long>();
,而运行 1000 次 Select
查询只需要 2.38 秒
long a = new SubSonic.Query.Select(provider, "UserId").From<User>()
.Where<User>(x => x.UserId == value).ExecuteScalar<long>();
我花了一些时间在 SubSonic 中深入了解 LINQ 的原理。探查器表明,DbQueryProvider.Execute
调用的大部分处理器时间都花在 DbQueryProvider.GetExecutionPlan
方法上 - 64%。 22% 的时间用于 System.Linq.Expressions.Complie,而 DbQueryProvider.Execute 仅使用 6% 的时间。
我对 SubSonic LINQ 查询的解析和编译方式非常满意。不过,如果能够像 Linq2Sql
中的 System.Data.Linq.CompiledQuery
一样,拥有用于重复 SubSonic LINQ 查询的编译工具,那就太好了。
The module I develop makes lots of small selects, inserts, and updates. Modifications made by commands in SubSonic.Query
namespace (ActiveRecord is not my weapon of choice) appear to be much faster than object-by-id select queries written in LINQ
.
It takes 7.15s to execute the following LINQ
query 1000 times
long a = (
from u in UserCollection
where u.UserId == value
select u.UserId
).FirstOrDefault<long>();
While only 2.38s for the thousand runs of Select
query
long a = new SubSonic.Query.Select(provider, "UserId").From<User>()
.Where<User>(x => x.UserId == value).ExecuteScalar<long>();
I took a time to look under the hood of LINQ in SubSonic. The profiler tells that much of processor time of DbQueryProvider.Execute
calls is spent in DbQueryProvider.GetExecutionPlan
method - 64 %. 22 % is spent in System.Linq.Expressions.Complie
, when DbQueryProvider.Execute
uses only 6 % of time.
I'm totally satisfied of how SubSonic LINQ queries are parsed and compiled. However it would be great to have Compilation facility for repeting SubSonic LINQ queries just like System.Data.Linq.CompiledQuery
in Linq2Sql
.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我们也对此进行了一些分析,发现 SubSonic 的 record.SingleOrDefault(x=>x.id=someval) 比通过 CodingHorror 执行的相同查询慢 20 倍。在这里记录:https://github.com/subsonic/SubSonic-3.0/issues/258 。
探查器在 ExecutionBuilder.cs 中指出了这一点:
令人失望,因为我真的很喜欢 SubSonic/Linq。
最后我们放弃了,我写了这个 - http://www.toptensoftware.com/petapoco。移植后,我们的负载测试显示每秒请求数上升,CPU 负载从大约 80% 下降到 5%。
We did some profiling on this too and found SubSonic's record.SingleOrDefault(x=>x.id=someval) to be up to 20x slower than the same query done through CodingHorror. Logged it here: https://github.com/subsonic/SubSonic-3.0/issues/258.
The profiler pointed at this in ExecutionBuilder.cs:
Disappointing because I really like SubSonic/Linq.
In the end we gave up and I wrote this - http://www.toptensoftware.com/petapoco. After porting, our load test showed requests per second went up and CPU load dropped from about 80% to 5%.