.NET Linq到SQL性能问题与继承的判别器

时间:2011-02-14 19:41:55

标签: .net linq performance inheritance

我遇到了LINQ to SQL模型的性能问题,该模型有很多继承的类。

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

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

在“小”场景中,它有4种类型,一个基本Customer和三个使用LINQ继承的派生类型。

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

与第一个(超过2秒)相比,LARGE datacontext的性能非常糟糕,即使它是相同的查询并返回相同的数据。

这是输出。

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秒而非.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 plus表继承生成时,这很大,并且从LINQ 2 SQL类开始变慢。我们还验证了它与'InheritanceDiscriminator'有关,因为如果删除它,性能就可以了。

后续调用很快,并且似乎在线程本地存储中缓存了一些东西。有没有办法在多个线程中保留它?

1 个答案:

答案 0 :(得分:1)

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

相关问题