tho*_*ulb 21 t-sql stored-procedures sql-server-2008
我们有一个存储过程,运行良好,直到10分钟前,然后它只是挂起后调用它.
观察:
还有什么可以继续?
UPDATE: I'm guessing it had to do with parameter sniffing. I used Adam Machanic's routine to find out which subquery was hanging. I found things wrong with the query plan thanks to the hint by Martin Smith. I learned about EXEC ... WITH RECOMPILE, OPTION(RECOMPILE) for subqueries within the SP, and OPTION (OPTIMIZE FOR (@parameter = 1)) in order to attack parameter sniffing. I still don't know what was wrong in this particular case but I came out of this battle seasoned and much better armed. I know what to do next time. So here's the points!
小智 16
我认为这与参数嗅探有关,需要将输入参数参数化为SP中的本地参数.通过重新编译添加会导致重新创建执行计划,并消除了拥有SP的许多好处.我们在许多报告中使用With Recompile试图消除这个悬而未决的问题,偶尔会导致挂起可能与同时访问同一个表的其他锁和/或事务有关的SP.有关详细信息,请参阅此链接 SQL Server中的参数嗅探(或欺骗),并将您的SP更改为以下内容以解决此问题:
CREATE PROCEDURE [dbo].[SPNAME] @ p1 int,@ p2 int AS
DECLARE @ localp1 int,@ localp2 int
SET @ localp1 = @ p1 SET @ localp2 = @ p2
Bre*_*zar 13
在查询运行时运行Adam Machanic的优秀sp_WhoIsActive存储过程.它将为您提供等待信息 - 意味着,存储过程正在等待的内容 - 以及执行计划等内容:
http://www.brentozar.com/archive/2010/09/sql-server-dba-scripts-how-to-find-slow-sql-server-queries/