2011-02-14 27 views
1

我遇到了很多繼承類的LINQ to SQL模型的性能問題。.NET Linq到繼承鑑別器的SQL性能問題

我孤立了這個問題,它似乎是LINQ to SQL生成的代碼本身的某種問題。

我使用Northwind創建了一個示例程序來充實這個問題。它在兩種情況下返回客戶的第一行。數據庫查詢是微不足道的,並運行1分鐘1ms。

在'小'情況下,它有4種類型,一個基本Customer和三個派生類型使用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 

注意,在第一次大量的數據可能運行時,它需要1.7秒與0.23秒(7X較慢)

即使第二組運行較慢的,但可接受的性能。

這裏是下載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'有關,因爲如果刪除它,性能會很好。

後續調用速度很快,似乎在線程本地存儲中緩存了一些內容。有沒有辦法在多個線程中堅持?

+0

你可以發佈代碼以及LINQ to SQL生成的SQL嗎? – 2011-02-14 19:45:01

+0

這不是SQL語句,而是關於構建對象的事情。整個樣本項目可從我的直播分享http://cid-02bee16e84f5c99f.office.live.com/self.aspx/Public/TestDalLinqPerformance.zip – 2011-02-14 20:39:45

回答

1

我可以從您的代碼中看到您縮進爲每個客戶添加新的子類。由於內部的L2S性能問題,這很不幸地不起作用。早晚你放棄這種做法。通過在加載狀態下運行應用程序(按住F5)並停止調試器幾次,您也可以輕鬆找出導致問題的功能。看看它最經常停在哪裏。