SubSonic LINQ 查询比 SubSonic.Query.Select 慢 3 倍

发布于 2024-10-09 10:16:43 字数 1039 浏览 0 评论 0原文

我开发的模块进行了大量小的选择、插入和更新。由 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

我们只是彼此的过ke 2024-10-16 10:16:43

我们也对此进行了一些分析,发现 SubSonic 的 record.SingleOrDefault(x=>x.id=someval) 比通过 CodingHorror 执行的相同查询慢 20 倍。在这里记录:https://github.com/subsonic/SubSonic-3.0/issues/258

探查器在 ExecutionBuilder.cs 中指出了这一点:

// this sucks, but since we don't track true SQL types through the query, and ADO throws exception if you
// call the wrong accessor, the best we can do is call GetValue and Convert.ChangeType
Expression value = Expression.Convert(
    Expression.Call(typeof (Convert), "ChangeType", null,
                    Expression.Call(reader, "GetValue", null, Expression.Constant(iOrdinal)),
                    Expression.Constant(TypeHelper.GetNonNullableType(column.Type), typeof(Type))
        ),
    column.Type
    );

令人失望,因为我真的很喜欢 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:

// this sucks, but since we don't track true SQL types through the query, and ADO throws exception if you
// call the wrong accessor, the best we can do is call GetValue and Convert.ChangeType
Expression value = Expression.Convert(
    Expression.Call(typeof (Convert), "ChangeType", null,
                    Expression.Call(reader, "GetValue", null, Expression.Constant(iOrdinal)),
                    Expression.Constant(TypeHelper.GetNonNullableType(column.Type), typeof(Type))
        ),
    column.Type
    );

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%.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文