简体   繁体   English

从.net调用的sql存储过程

[英]sql stored procedure calling from .net

HI, 嗨,

I have a sql stored procedure, which i am calling from my asp.net (2.0) code by using sqlCom.ExecuteNonQuery(), but it tooks around 60 to 100 second to complete, and while i execute the same SP from sql query tool runs in 3 to 4 seconds. 我有一个sql存储过程,我正在使用sqlCom.ExecuteNonQuery()从asp.net(2.0)代码中调用,但是要花大约60到100秒才能完成,而我从sql查询工具执行相同的SP在3到4秒内运行。

Please help on this. 请帮忙。


Edit - code from comments: 编辑-注释中的代码:

public int ExecuteNonQuery(string strSpName, DbParameter[] parameterValues) 
{ 
    CreateConnection(); 
    SqlCommand sqlCom = new SqlCommand(); 
    if (strSpName == null || strSpName.Length == 0) 
       throw new ArgumentNullException("strSpName"); 
    int i = 0; 
    sqlCom.Connection = _sqlConn;
    if (_blnIsTransEnabld == true) 
       sqlCom.Transaction = _sqlT; 
    sqlCom.CommandType = System.Data.CommandType.StoredProcedure; 
    sqlCom.CommandText = strSpName; 
    sqlCom.CommandTimeout = _sqlConn.ConnectionTimeout; 
    return sqlCom.ExecuteNonQuery(); 

}

This is often a symptom of having out of date statistics. 这通常是统计数据过时的症状。 Suggest you rebuild statistics: 建议您重建统计信息:

exec sp_updatestats 

I would suggest not updating statistics or making any alterations to the stored procedure until you have gathered the execution plans as this will drop the execution plan in the cache and foil any attempts to investigate further whether that was the actual issue. 我建议您在收集执行计划之前 不要更新统计信息或对存储过程进行任何更改,因为这会将执行计划丢弃在缓存中,并阻止进一步调查这是否是实际问题的任何尝试。

The drop of the plan may lead to you thinking the issue is solved but if the problem is parameter sniffing it will likely reappear. 计划的取消可能会导致您认为问题已解决,但是如果问题是参数嗅探,则很可能会再次出现。

First get the execution plans for the SSMS and C# ones by following the advice in this answer. 首先按照此答案中的建议获取SSMS和C#的执行计划。

Very different execution times of SQL query in C# and SQL Server Management Studio C#和SQL Server Management Studio中SQL查询的执行时间完全不同

Once you've got those saved can you update your question with the XML for both of them? 保存完这些文件后,您可以使用XML来更新它们的问题吗?

Edit: Actually probably better would be to use SQL profiler to capture Showplan XML Statistics Profile events and get the actual execution plan for the C# one. 编辑:实际上可能更好的方法是使用SQL事件探查器捕获Showplan XML Statistics Profile事件并获取C#的实际执行计划。 Don't leave the profiler trace running for any longer than the bare minimum if it is a production server. 如果它是生产服务器,则不要使探查器跟踪的运行时间超过最低要求。

First of all, looking at the code I hope you have something in place to close the connection you are using here. 首先,看一下代码,希望您能在此处关闭正在使用的连接。

As for your problem, any chance there is another query pending on a transaction that has issued locks on the data you are accessing? 至于您的问题,是否有可能在事务中又有另一个查询未决,而该事务已对您访问的数据发出了锁定? That would explain the long execution time; 那将解释执行时间长; if the data you are accessing is locked by some other query, execution will wait for the locks to be released or timeout. 如果您正在访问的数据被其他查询锁定,则执行将等待释放锁或超时。

To determine if there is a locking issue you can use SQL Server Management Studio. 要确定是否存在锁定问题,可以使用SQL Server Management Studio。 Open Management / Activiy Monitor to get a list of pending locks. 打开管理/活动监视器以获取挂起的锁的列表。

To prevent locking issues make sure you commit or rollback all pending transactions as soon as possible. 为防止锁定问题,请确保尽快提交或回滚所有未决的事务。 There are several ways to resolve deadlocking. 有几种方法可以解决死锁。 Usually you can prevent it all together for example by reordering queries in a transaction. 通常,您可以通过例如对事务中的查询重新排序来防止全部发生。 If that is not an option you may want to change the isolation level of the transactions or use table hints . 如果不是这样,则可能需要更改事务的隔离级别或使用表提示

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM