SQL Server - sys.dm_exec_sql_text.sql_handle显示常量CREATE PROCEDURE - 如何证明它

时间:2017-05-12 05:02:48

标签: sql-server stored-procedures

我质疑了我们某个供应商的代码。运行此基本脚本时:

SELECT  top 20
   qt.text,
   qs.creation_time,
   qs.plan_generation_num,
qs.execution_count, 
qs.last_execution_time,
qp.query_plan
FROM 
    sys.dm_exec_query_stats qs
CROSS APPLY 
    sys.dm_exec_sql_text(qs.sql_handle) qt
CROSS APPLY 
    sys.dm_exec_query_plan(qs.plan_handle) qp
WHERE 
    qt.encrypted = 0
    and qt.text like '%CREATE PROCEDURE%'
ORDER BY 
    qs.execution_count DESC

我看到以下结果:

sql query output - top 20

但是,当我运行SQL分析器,寻找SP:重新编译时,我根本没有看到任何东西。看起来SP每分钟重新编译数百次,但我只有我的脚本证明。

在研究这个问题时,今天下午,我发现一些SQL页面说SET SET_NULLS ON和SET QUOTED_IDENTIFIER ON可以触发重新编译,但我找不到足够的方法。

我还在输出中包含来自其中一个SP的代码。

我在这里遗漏了什么吗?毫无疑问,SP每分钟执行数百次,但它是否也在重新编译?

谢谢!

存储过程代码:

/****** Object:  StoredProcedure [dbo].[stMessageQueueDequeue]    Script 
Date: 5/11/2017 4:14:56 PM ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO


ALTER PROCEDURE [dbo].[stMessageQueueDequeue]
(
  @pDestination varchar(100)
)
AS

BEGIN TRANSACTION

-- Select du message a envoyer selon la Priority et DateAdded
declare 
@TopMessageId int,
@priority int

select @priority = min(priority) from messagequeue
where  Retry < 5 AND Destination = @pDestination AND DateNextRetry <= 
GetUtcDate()

select top 1 @TopMessageId = id from messagequeue
where  Retry < 5 AND Destination = @pDestination AND DateNextRetry <= 
GetUtcDate() AND priority = @priority
ORDER BY Retry,  DateNextRetry,DateAdded 

IF @@ERROR <> 0
  BEGIN
        ROLLBACK TRANSACTION
        RETURN -1
  END

IF @TopMessageId IS NOT NULL
  BEGIN

        UPDATE MessageQueue 
        SET DateProcessed = GetUtcDate(), 
        Retry = Retry + 1,
        DateNextRetry=
              CASE 
                    WHEN Retry=0 THEN    DATEADD(minute, 5 , GetUtcDate())
              when Retry=1   THEN DATEADD(minute, 20 ,  GetUtcDate())
              when Retry=2   THEN DATEADD(minute, 60 ,  GetUtcDate())
              when Retry=3   THEN DATEADD(minute, 80 ,  GetUtcDate())
              when Retry=4   THEN DATEADD(minute, 720 ,  GetUtcDate())
              ELSE   DATEADD(minute, 1440 ,  GetUtcDate())
              END 
        WHERE HashCode = (select top 1 HashCode from MessageQueue where   id 
        = @TopMessageId)

        IF @@ERROR <> 0
              BEGIN
                    ROLLBACK TRANSACTION
                    RETURN -1
              END

        SELECT id, Source, Destination, Message, HashCode, DateAdded, 
        Priority, Retry, DateProcessed, DateNextRetry from messagequeue
        where id = @TopMessageId
        IF @@ERROR <> 0
              BEGIN
                    ROLLBACK TRANSACTION
                    RETURN -1
              END

        END




 declare @vLastDate as datetime 

 SELECT @vLastDate  = Value from STatus where [key] = 
 'MessagequeueHistoryLastPurge'
 IF @vLastDate is null 
 BEGIN
 INSERT INTO STATUS ([Key] , value ) 
 values ( 'MessagequeueHistoryLastPurge' ,'2000-01-01' ) 
 SET @vLastDate  = '2000-01-01'
 END 


 IF DATEDIFF(d ,  @vLastDate,GETUTCDATE() ) > 1  
 BEGIN 
 DELETE MessageQueueHistory WHERE DateProcessed <  DATEADD(d ,  -7, 
 GETUTCDATE() ) 
 UPDATE STATUS set Value = GETUTCDATE() where [key] = 
 'MessagequeueHistoryLastPurge'
 EXEC sp_MSforeachtable 'IF ''?'' LIKE ''%Audit%'' BEGIN truncate table ? 
 END'
 END



COMMIT TRANSACTION
RETURN 0

0 个答案:

没有答案