【问题标题】:.NET Linq to SQL Performance Issue with Inherited Discriminators.NET Linq to SQL 性能问题与继承的鉴别器
【发布时间】:2011-06-27 03:48:17
【问题描述】:

我在使用具有大量继承类的 LINQ to SQL 模型时遇到性能问题。

我隔离了这个问题,它似乎是 LINQ to SQL 生成的代码本身的某种问题。

我使用 Northwind 创建了一个示例程序来充实这个问题。它在两种情况下都返回 Customer 的第一行。数据库查询很简单,运行时间

在“小”场景中,它有 4 种类型,一种是基本客户类型,三种派生类型使用 LINQ 继承。

“大型”场景,它有超过 60 种类型,一个基本客户和超过 60 种使用 LINQ 继承和 INT 列鉴别器的派生类型。

与第一个(超过 2 秒)相比,LARGE 数据上下文的性能很糟糕,即使它是相同的查询和返回的相同数据。

这是输出。

Performance Comparison of LINQ to SQL
Run each 3 times in a row for cache
With 4 Inherited Tables
00:00:00.2340004 of Small Test for First Customer:A Bike Store
00:00:00 of Small Test for First Customer:A Bike Store
00:00:00 of Small Test for First Customer:A Bike Store
With >60 Inherited Tables
00:00:01.7004030 of Large TestFirst Customer:A Bike Store
00:00:00.0156000 of Large TestFirst Customer:A Bike Store
00:00:00.0156001 of Large TestFirst Customer:A Bike Store
With 4 Inherited Tables
00:00:00 of Small Test for First Customer:A Bike Store
00:00:00 of Small Test for First Customer:A Bike Store
00:00:00 of Small Test for First Customer:A Bike Store
With >60 Inherited Tables
00:00:00.0156000 of Large TestFirst Customer:A Bike Store
00:00:00.0156000 of Large TestFirst Customer:A Bike Store
00:00:00.0156001 of Large TestFirst Customer:A Bike Store
Press Any Key

请注意,第一次运行 LARGE 数据集时,需要 1.7 秒,而 0.23 秒(慢 7 倍)

即使是第二组运行速度也较慢,但性能可以接受。

这里是完整的应用程序下载http://cid-02bee16e84f5c99f.office.live.com/self.aspx/Public/TestDalLinqPerformance.zip

从我们的调试来看,这与生成的代码有关:

[global::System.Data.Linq.Mapping.InheritanceMappingAttribute(Code="2", Type=typeof(Customer2))]
    [global::System.Data.Linq.Mapping.InheritanceMappingAttribute(Code="67", Type=typeof(Customer67))]
    [global::System.Data.Linq.Mapping.InheritanceMappingAttribute(Code="65", Type=typeof(Customer65))]
    [global::System.Data.Linq.Mapping.InheritanceMappingAttribute(Code="615", Type=typeof(Customer615))]
    [global::System.Data.Linq.Mapping.InheritanceMappingAttribute(Code="513", Type=typeof(Customer613))]
    public partial class Customer : INotifyPropertyChanging, INotifyPropertyChanged

当使用 40 多个表继承生成时,这对于 LINQ 2 SQL 类来说很大并且速度慢。我们还验证了它与“InheritanceDiscriminator”有关,因为如果您将其移除,性能会很好。

随后的调用速度很快,并且似乎在线程本地存储中缓存了一些东西。有没有办法跨多个线程持久化?

【问题讨论】:

  • 能否贴出代码,以及LINQ to SQL生成的SQL?
  • 这不是 SQL 语句,而是关于构建对象的一些东西。整个示例项目可以从我的实时分享中获得,cid-02bee16e84f5c99f.office.live.com/self.aspx/Public/…
  • 先生,您解决了吗?我也遇到过同样的问题!!我现在有 36 个子类 - 性能变得非常缓慢。

标签: .net linq performance inheritance


【解决方案1】:

我可以从您的代码中看到您缩进为每个客户添加一个新的子类。不幸的是,由于内部 L2S 性能问题,这不起作用。你迟早不得不放弃这种方法。您还可以通过在负载下运行应用程序(按住 F5)并停止调试器几次来轻松找出导致问题的功能。看看它最常停在哪里。

【讨论】:

  • 谢谢。它在 Microsoft LINQ 代码库中停止,并且下载和调试 LINQ 代码并不容易。后续调用速度很快,并缓存在 Thread Local Storage 中。有没有办法保留这些信息?
  • 也许堆栈跟踪和反射器就足够了?
  • 即使在调试时我们也很难过,因为 LINQ 代码很繁琐。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2010-09-09
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-03-26
相关资源
最近更新 更多