我在做一些測試和直LINQ到SQL查詢的運行速度比如果通過LINQ查詢存儲過程比LINQ查詢慢嗎?
調用存儲過程在SQL Server Profiler中一個通用的LINQ查詢
var results = from m in _dataContext.Members
select m;
了至少快80%只有19毫秒,而不是一個存儲過程
var results = from m in _dataContext.GetMember(userName)
select m;
(GetMember
被存儲的過程)做歷時100毫秒
這是爲什麼?
編輯:
直LINQ看起來像這樣在探查
SELECT
[t1].[MemberID], [t1].[Aspnetusername], [t1].[Aspnetpassword],
[t1].[EmailAddr], [t1].[DateCreated],
[t1].[Location], [t1].[DaimokuGoal], [t1].[PreviewImageID],
[t1].[value] AS [LastDaimoku],
[t1].[value2] AS [LastNotefied],
[t1].[value3] AS [LastActivityDate], [t1].[IsActivated]
FROM
(SELECT
[t0].[MemberID], [t0].[Aspnetusername], [t0].[Aspnetpassword],
[t0].[EmailAddr], [t0].[DateCreated], [t0].[Location],
[t0].[DaimokuGoal], [t0].[PreviewImageID],
[t0].[LastDaimoku] AS [value], [t0].[LastNotefied] AS [value2],
[t0].[LastActivityDate] AS [value3], [t0].[IsActivated]
FROM
[dbo].[Members] AS [t0]) AS [t1]
WHERE
[t1].[EmailAddr] = @p0
存儲過程是這樣的
SELECT Members.*
FROM Members
WHERE dbo.Members.EmailAddr = @Username
所以你看這個存儲過程的查詢要簡單得多。但是它的速度更慢......對我來說毫無意義。
它看起來不像你的兩個查詢做同樣的事情。第二個參數(大概是爲了限制結果),其中第一個顯然只是獲得所有行。對於初學者,你確定SQL是一樣的嗎? – 2008-11-04 19:21:33
存儲過程中的sql與linq查詢的功能相同...本質上與你是正確的參數...通過linq查詢返回所有行比在存儲過程中返回單個行更快。 – dswatik 2008-11-04 19:23:53