简体   繁体   English

nvarchar(max)减慢存储过程

[英]nvarchar(max) slowing stored procedure

From yesterday, i'm facing a problem:when i call a stored proc from c#,it lasts >5 in, but when i execute it directly from SSMS (in the server machine) its lasts less than 30 seconds. 从昨天开始,我遇到了一个问题:当我从c#调用存储的proc时,它的持续时间大于5,但是当我直接从SSMS(在服务器计算机中)执行它时,其持续时间不到30秒。

I have searched in forums and went trough this great article http://www.sommarskog.se/query-plan-mysteries.html but no result. 我在论坛上进行了搜索,浏览了这篇很棒的文章http://www.sommarskog.se/query-plan-mysteries.html,但没有结果。

The script contained in my proc is retrieving 10 columns among them a column called "article" of type nvarchar(max). 我的proc中包含的脚本正在检索10列,其中nvarchar(max)类型的称为“文章”的列。

When i remove the article column from my Select ,my proc executes quickly. 当我从Select中删除article列时,我的proc快速执行。

To further my logic, i have created a new stored proc retrieving just Primary Key Column and nvarchar(max) column. 为了进一步说明我的逻辑,我创建了一个新的存储过程,仅检索主键列和nvarchar(max)列。

I'm reproducing the same behaviour.Here is my new proc=MyNewProc(lasts >5 min when called from c# and 0 Secondes in the server from SSMS) 我正在重现相同的行为。这是我的新proc = MyNewProc(从c#调用时为> 5分钟,从SSMS在服务器中为0秒)

  CREATE PROCEDURE Student.GetStudents
  AS
  BEGIN
 SET NOCOUNT ON
 -----------------
 SELECT StudentId,Article
 FROM Students
 WHERE Degree=1

 END 

MyNewProc returns just 2500 rows. MyNewProc仅返回2500行。

Is that normal? 那是正常的吗? How can i improve that. 我该如何改善。

SELECT SUM(DATALENGTH(Article)) FROM Students WHERE Degree=1 the result is 13885838 从学生中选择SELECT SUM(DATALENGTH(Article)),学位= 1,结果为13885838

You're probably transferring a lot of data over the network. 您可能正在通过网络传输大量数据。 That takes time. 这需要时间。

Instead of returning article try returning LEFT(article, 50) to see if its an issue with the volume of data or not. 而不是返回article尝试返回LEFT(article, 50)来查看其是否与数据量有关。

One thing to note is that SSMS will begin populating the results immediately while a C# application probably will not. 需要注意的一件事是,SSMS将立即开始填充结果,而C#应用程序可能不会。

In SSMS, go to the following: Tools -> Options 在SSMS中,转到以下位置:工具->选项

Then go to Query Execution -> SQL Server -> Advanced 然后转到查询执行-> SQL Server->高级

From here, look at what check boxes are checked and if there is something that is checked, SSMS will use this automatically when you execute a sproc from inside of it but when you execute it from C# (or whatever client you're using) it won't be used. 在这里,查看选中了哪些复选框,以及是否选中了某些复选框,当您从内部执行sproc但从C#(或您使用的任何客户端)执行sproc时,SSMS会自动使用它将不会被使用。

I had this same issue and found out that I needed to include the following line at the top of my sproc and it worked perfectly: 我遇到了同样的问题,发现我需要在存储过程的顶部添加以下行,并且它的运行非常完美:

SET ARITHABORT ON; 开启ARITHABORT;

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 存储过程参数中的SQL Server Nvarchar(MAX)变量长度问题 - SQL Server Nvarchar(MAX) variable length issue in stored procedure parameter 将NVARCHAR值传递给存储过程 - passing NVARCHAR value to stored procedure 将返回值错误数据类型为nvarchar(max)的存储过程错误调用为int - Calling stored procedure error on return value error data type nvarchar(max) to int SQL存储过程比较NVARCHAR不能按预期方式工作 - SQL Stored Procedure comparing NVARCHAR not working as expected 从代码将存储过程中的数据类型nvarchar转换为datetime时出错 - Error converting data type nvarchar to datetime in a Stored Procedure from code 无法在 EF 访问的存储过程中将 NVARCHAR 转换为 datetime2 - Cannot convert NVARCHAR to datetime2 in stored procedure accessed by EF 将数据类型nvarchar转换为int时出错 - 执行存储过程时 - Error converting data type nvarchar to int - while executing stored procedure 在存储过程中将nvarchar数据类型转换为datetime数据类型 - conversion of a nvarchar data type to a datetime data type in stored procedure 使用存储过程将数据类型nvarchar转换为int时出错 - Error converting data type nvarchar to int using stored procedure 在使用Entity Framework执行存储过程时,过程需要'ntext / nchar / nvarchar类型的参数'@statement' - Procedure expects parameter '@statement' of type 'ntext/nchar/nvarchar while executing a stored procedure with Entity Framework
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM