LINQ to SQL的执行时间是50倍比SSMS SQL更长 [英] LINQ to SQL execution time is 50x longer than SSMS SQL

查看:102
本文介绍了LINQ to SQL的执行时间是50倍比SSMS SQL更长的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个使用LINQ to SQL的一个asp.net应用程序。

从应用程序的一个查询是超时(超过30秒执行)。我增加了在DataContext的的CommandTimeout,并在45秒内没有完成。

我复制了LINQ到SQL从Profiler来SSMS SQL生成的SQL和使用,因为我用我的应用程序相同的用户凭据执行它,我得到一分一秒的执行时间。

我使用的是新的DataContext执行查询,所以查询在其自身的事务中运行。

我的连接字符串是这样的:

 数据源= \\ SQLEX $ P $干燥综合征;初始目录= MYDB;集成安全性= FALSE;用户ID = MYUSER;密码= @#$%^&放大器; *

任何人有任何想法,为什么我可能会得到这样的不同的结果?

下面是SQL:

  EXEC sp_executesql的N'SELECT [T1]。[MatchCount],[T1]。[请求ID],[T1] [网站],[T1]。[客户]
FROM(
    选择ROW_NUMBER)OVER(ORDER BY [T0]。[请求ID])(AS [ROW_NUMBER],[T0]。[MatchCount],[T0]。[请求ID],[T0] [网站],[T0]。[客户]
    FROM [DBO]。[ReportingRequestsSubsetWithMatches] AS [T0]
    WHERE([T0] [完成]< @ P0)AND([T0] [完成]方式> @ P1)
    )AS [T1]
WHERE [T1]。[ROW_NUMBER] @之间P2 + 1和@ P2 + P3 @
ORDER BY [T1]。[ROW_NUMBER]',N'@ P0日期时间,@ P1日期时间,@ p2的INT,@ P3诠释',@ P0 ='2013年2月2日00:00:00,@ P1 =' 2013年2月1日00:00:00,@ P2 = 10,@ P3 = 10


解决方案

对于比较SSMS的SET选项和您的LINQ会话:

  SELECT * FROM sys.dm_exec_sessions
WHERE在SESSION_ID(@@ SPID,@LINQSESS)

(与你的LINQ的会话ID替换@LINQSESS)


ARITHABORT 往往是culpit,看看是否能解决您的问题

 新的SqlCommand(SET ARITHABORT ON,连接).ExecuteNonQuery();

I have an asp.net app that uses Linq to SQL.

One query from the app was timing out (over 30 seconds to execute). I increased the CommandTimeout of the DataContext, and it did complete in 45 seconds.

I copied the Linq to SQL generated SQL from SQL Profiler to SSMS and executed it using the same user credentials as I am using in my app, and I got a sub-second execution time.

I am using a new DataContext to execute the query, so the query is running in its own transaction.

My connection string looks like this:

Data Source=.\SQLEXPRESS;Initial Catalog=MyDB;Integrated Security=False; user id=MyUser; password=@#$%^&*

Anyone have any ideas why I could be getting such different results?

Here is the SQL:

exec sp_executesql N'SELECT [t1].[MatchCount], [t1].[RequestId], [t1].[Site], [t1].[Client]
FROM (
    SELECT ROW_NUMBER() OVER (ORDER BY [t0].[RequestId]) AS [ROW_NUMBER], [t0].[MatchCount], [t0].[RequestId], [t0].[Site], [t0].[Client]
    FROM [dbo].[ReportingRequestsSubsetWithMatches] AS [t0]
    WHERE ([t0].[Finish] < @p0) AND ([t0].[Finish] > @p1)
    ) AS [t1]
WHERE [t1].[ROW_NUMBER] BETWEEN @p2 + 1 AND @p2 + @p3
ORDER BY [t1].[ROW_NUMBER]',N'@p0 datetime,@p1 datetime,@p2 int,@p3 int',@p0='2013-02-02 00:00:00',@p1='2013-02-01 00:00:00',@p2=10,@p3=10

解决方案

Compare the SET options for SSMS and your LINQ sessions:

SELECT * FROM sys.dm_exec_sessions
WHERE session_id in (@@SPID, @LINQSESS)

(replace @LINQSESS with your Linq session id)


ARITHABORT is often the culpit, see if this solves your issue:

new SqlCommand("SET ARITHABORT ON", connection).ExecuteNonQuery();

这篇关于LINQ to SQL的执行时间是50倍比SSMS SQL更长的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆