我注意到,相比於在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)
您是否嘗試過在你的代碼的發佈版本運行的代碼,不只是調試?我最近有一個查詢(使用NHibernate),花了近一分鐘的時間在Debug模式下運行,但在Release模式下花費的時間不到1秒! – XN16
在這種情況下,如果它在Release或Debug版本中運行,它不會有明顯的不同。 – Rene