2014-01-20 64 views
0

我注意到,相比於在Visual Studio 2012年性能改進

同樣的查詢的查詢窗口中執行相同的查詢執行MVC控制器內部LINQ查詢之間的巨大性能差異在VSC控制器中需要26秒,從 VS 2012查詢窗口開始3秒!

我用miniprofiler從LINQ查詢中得到結果SQL命令。這個命令我從VS 2012執行,所以查詢完全一樣。如何解釋執行時間的差異,以及如何改進?

這是執行查詢的miniprofiler輸出:

http://localhost:49858/TrackingEntry/TrackingEntries_Read 
T+28.9 ms 
Reader 
25558.2 ms 

ExecuteStoreCommands Execute GetResults System.Collections.Generic.IEnumerable<T>.GetEnumerator GetEnumerator System.Collections.Generic.IEnumerable<TResult>.GetEnumerator 

DECLARE @p__linq__0 DateTime2 = '2014-01-03T00:00:00', 
     @p__linq__1 DateTime2 = '2014-01-20T23:59:59', 
     @p__linq__2 int = 8; 

SELECT 
[Extent1].[TrackingEntryId] AS [TrackingEntryId], 
[Extent1].[DeviceType] AS [DeviceType], 
[Extent1].[MobileIdNr] AS [MobileIdNr], 
[Extent1].[Speed] AS [Speed], 
[Extent1].[Direction] AS [Direction], 
[Extent1].[Latitude] AS [Latitude], 
[Extent1].[Longitude] AS [Longitude], 
[Extent1].[LocationName] AS [LocationName], 
[Extent1].[Zipcode] AS [Zipcode], 
[Extent1].[Street] AS [Street], 
[Extent1].[City] AS [City], 
[Extent1].[CountryId] AS [CountryId], 
[Extent1].[Odometer] AS [Odometer], 
[Extent1].[IgnitionIsOn] AS [IgnitionIsOn], 
[Extent1].[MessageType] AS [MessageType], 
[Extent1].[SatelliteCount] AS [SatelliteCount], 
[Extent1].[IsBusiness] AS [IsBusiness], 
[Extent1].[Timestamp] AS [Timestamp], 
[Extent1].[TimestampUTC] AS [TimestampUTC], 
[Extent1].[ActivityTypeId] AS [ActivityTypeId], 
[Extent1].[EmployeeId] AS [EmployeeId], 
[Extent1].[TrailerId] AS [TrailerId], 
[Extent1].[CompanyId] AS [CompanyId], 
[Extent1].[StatusCode1Id] AS [StatusCode1Id], 
[Extent1].[StatusCode2Id] AS [StatusCode2Id], 
[Extent1].[StatusCode3Id] AS [StatusCode3Id], 
[Extent1].[StatusCode4Id] AS [StatusCode4Id], 
[Extent1].[StatusCode5Id] AS [StatusCode5Id], 
[Extent1].[PoiId] AS [PoiId], 
[Extent1].[MessageFormat] AS [MessageFormat], 
[Extent1].[MessageActivityTypeId] AS [MessageActivityTypeId], 
[Extent1].[TrackingMessageId] AS [TrackingMessageId], 
[Extent1].[TrackedObjectId] AS [TrackedObjectId], 
[Extent3].[ActivityTypeId] AS [ActivityTypeId1], 
[Extent3].[ActivityTypeName] AS [ActivityTypeName], 
[Extent3].[Icon] AS [Icon], 
[Extent3].[IsLocked] AS [IsLocked], 
[Extent3].[IsDeleted] AS [IsDeleted], 
[Extent4].[FullName] AS [FullName], 
[Extent5].[Name] AS [Name], 
[Extent2].[Identification] AS [Identification], 
[Extent2].[Licenseplate] AS [Licenseplate], 
[Extent2].[TrackedObjectId] AS [TrackedObjectId1] 
FROM  [dbo].[TrackingEntries] AS [Extent1] 
INNER JOIN [dbo].[TrackedObjects] AS [Extent2] ON ([Extent1].[MobileIdNr] = [Extent2].[DeviceId]) OR (([Extent1].[MobileIdNr] IS NULL) AND ([Extent2].[DeviceId] IS NULL)) 
LEFT OUTER JOIN [dbo].[ActivityTypes] AS [Extent3] ON [Extent1].[ActivityTypeId] = [Extent3].[ActivityTypeId] 
LEFT OUTER JOIN [dbo].[Employees] AS [Extent4] ON [Extent1].[EmployeeId] = [Extent4].[EmployeeId] 
LEFT OUTER JOIN [dbo].[Trailers] AS [Extent5] ON [Extent1].[TrailerId] = [Extent5].[TrailerId] 
WHERE ([Extent1].[Timestamp] >= @p__linq__0) AND ([Extent1].[Timestamp] <= @p__linq__1) AND ([Extent1].[EmployeeId] = @p__linq__2) 
+1

您是否嘗試過在你的代碼的發佈版本運行的代碼,不只是調試?我最近有一個查詢(使用NHibernate),花了近一分鐘的時間在Debug模式下運行,但在Release模式下花費的時間不到1秒! – XN16

+0

在這種情況下,如果它在Release或Debug版本中運行,它不會有明顯的不同。 – Rene

回答

0

我建議你執行查詢,並期待在生成的執行PALN。它可以幫助你理解問題。 Usualy,LINQ2SQL生成查詢並使用sp_executesql執行它。而且執行「清除」查詢的時間可能會更長(包含sp_executesql)。我有這個問題,因爲缺少索引。

Execution plan

+0

我不明白你的建議。從查詢窗口運行命令時,我可以獲得執行計劃。在這種情況下,執行需要3秒鐘,在這種情況下可以接受。我假設從控制器內執行命令時執行計劃是相同的。 – Rene

+0

@Rene,我認爲,你的sql查詢用SP'sp_executesql'執行。它可能會導致性能問題(缺少索引)。所以,我建議你檢查執行計劃 – Backs