1

使用StackExchange.Profiling.MiniProfiler该类以将 Linq-To-Sql 用作 ORM 来分析 ASP.NET MVC 应用程序。

我正在尝试将一项操作减少为一个 SQL,以便不再有任何重复项。所以我相应地更改了我的 linq-to-sql 代码,但它对速度没有任何积极影响。

然后我检查了 SQL 所需的时间。

这显示了 MiniProfiler:

在此处输入图像描述

当我在 Management Studio 中启动完全相同的 SQL 时,它的速度非常快:

在此处输入图像描述

这是代码:

from t in type
let tDoc = (from d in context.documents
            where d.Key == t.No
            && d.RType == (int)RType.Art
            && d.AType == (int)AType.Doc
            select d).FirstOrDefault(d => d.UseForThumb)
select new Time
{
    Id = t.Id,
    //... more simple mappings here
    // then a complex one:
    DocsCount = context.documents.Count(d =>
        (d.Key == t.Id.ToString()
        && d.RType == (int)RType.Type
        && d.AType == (int)AType.Doc)
        ||
        (d.Key == t.No
        && d.RType == (int)RType.Art
        && d.AType == (int)AType.Doc)),

    // and another one
    ThumbId = (tDoc != null && tDoc.FRKey.HasValue) ? tDoc.FRKey.Value : 0
};

造成巨大差异的原因是什么?-编辑:没有区别,我只是误解了 SSMS :(

无论如何,我的问题仍然存在。我可以改变什么以使其更快?

我有时读到 Linq-To-Sql 的映射存在性能问题。有没有办法解决这个问题?

4

1 回答 1

0

我做了一些试验和错误,并将 Linq-To-Sql 代码更改为:

from t in types
let docs = context.documents.Where(d => (d.RKey == t.Id.ToString()
                                     && d.RType == (int)RType.Type
                                     && d.AType == (int)AType.Doc)
                                   ||
                                        (d.RKey == t.No 
                                     && d.RType == (int)RType.Art 
                                     && d.AType == (int)AType.Doc))
let tDoc = docs.FirstOrDefault(d => d.RType == (int)RType.Art && d.UseForThumb)
let docsCount = docs.Count()
select new Time
{                                               
  Id = t.Id,
  //... more simple mappings here
  DocsCount = docsCount,
  ThumbId = (tDoc != null && tDoc.FRKey.HasValue) ? tDoc.FRKey.Value : 0,
}

这使得查询快得多。

于 2013-04-29T07:54:31.897 回答