Is the stored procedure slower than the LINQ query?

I ran some tests, and direct LINQ-to-SQL queries ran at least 80% faster than when calling stored procedures using a LINQ query

In SQL Server Profiler, general LINQ query

 var results = from m in _dataContext.Members
 select m;

it took only 19 milliseconds unlike the stored procedure

 var results = from m in _dataContext.GetMember(userName)
 select m;

( GetMemberis a stored procedure), executing the same request that took 100 milliseconds

Why is this?

Edit:

Direct LINQ looks like this in Profiler

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

The stored procedure is

SELECT Members.*
FROM Members 
WHERE dbo.Members.EmailAddr = @Username

So you see that requesting a stored procedure is much simpler ... but still slower ... it makes no sense to me.

+3
source share
6 answers

1) , . ​​ , , .

2) - , .

3) (, .NET SQL), , .

+3

, , - *. , , , , LINQ , , select *.

+1

, proc .

+1

A, , , . , .

, ..

, LinqPad, , SQL , .

0

* , . , SQL LINQ, , ([]) - LINQ.

0

Can I add to John Skeet the answer that when you run the code several times remember how to clear any query cache.

I can suggest using "EXPLAIN" with both queries: it seems that MySQL creates a query execution plan for a query and SP in different ways. For SP, he answers before replacing the parameters with his own values, and therefore he does not use indexes, which are used in the case of a hard-coded or replaced parameter. Here's another question about the different runtimes for the SP and the direct request from SO with the query plan data given for both cases.

0
source

Source: https://habr.com/ru/post/1699131/


All Articles