谜团:存储过程以每分钟1条记录插入记录

时间:2017-01-26 22:56:42

标签: sql sql-server tsql stored-procedures

我们手上有一个谜。

情况如下:

  1. 我们有一个顶级存储过程,它会生成游标的日期列表
  2. 在游标中,调用另一个存储过程,并以日期作为参数 此存储过程执行INSERT INTO,其中date参数用于查询
  3. 中的BETWEEN语句
  4. 第二个存储过程通过多个日期执行,这很好,但是由于参数的值,没有找到记录
  5. 日期参数为'2016-01-01 23:59:59'后,第二个查询开始,但不执行查询
  6. 活动监视器不显示任何活动,既不阻止也不显示任何等待状态。我们等了几分钟,但是第二个存储过程一直没有做任何事情(FYI:第二个存储过程的每次运行一直需要大约40秒)。
  7. 这种情况以前已经发生好几次了。
  8. 调查行动:

    1. 当使用此日期参数手动执行第二个存储过程时,它已启动,但什么也没做(没有阻塞,没有等待)。
    2. 执行参数值为“2016-01-01 23:59:59”的INSERT INTO块代码时,执行成功。
    3. 在之前的场合,我们可以使用参数值'2016-01-01 23:59:59'成功调试并执行顶级存储过程和第二级过程。在调试模式下单击“继续”按钮时,它也成功运行了所有剩余日期。
    4. 修正:

      在那个场合,我们还设法通过重命名第二个存储过程来修复这个奇怪的行为,并再次创建它。之后,执行顶级存储过程成功调用并执行具有相同参数值的第二个存储过程(所有这些)。

      今天:

      今天我们又遇到了这个问题;我们启动了顶级存储过程,它生成了一个日期列表,称为每个日期的第二个存储过程,当它到达日期'2016-01-01 23:59:59'时,它再次卡住(没有阻止,没有等待)。 当使用参数“2016-01-01 23:59:59”运行INSERT INTO查询的代码时,我们成功了。 当我们调试顶级存储过程并获得参数值'2016-01-01 23:59:59'时,调试器进入第二个存储过程,进入INSERT INTO部分,然后退出。它没有执行INSERT INTO查询。在调试器中,您看到参数/变量值的窗口中的参数值变为空,包括所有其他调试窗口。 我们重新启动了SQL Server,但无济于事。

      所以,这是我们的谜。我们在Server 2008 R2上运行SQL Server 2021 SP1 CU3。

      如果有人可以揭开这个谜团,我们将非常感激。

      非常感谢您的协助。

      Jan Willem Borrius

      更新:我们现在发现实际上第二个存储过程正在插入记录,但速度很慢:每条记录1分钟!

      我们从日志记录表中删除了此存储过程的日志记录,重新启动了SQL Server,重新启动了进程:现在第二个存储过程正在以正常速度插入记录。 但是,我们仍然不明白为什么会这样。

      解决!参数嗅探,所以我们在第二个存储过程中添加了INSERT INTO查询:OPTION(OPTIMIZE FOR UNKNOWN)

      代码第二个存储过程:

      CREATE PROCEDURE XXXXXXXXX
         /* PARAMETER 1 */ @FIELD_2      DATETIME2(0)
        ,/* PARAMETER 2 */ @FIELD_1           BIGINT
        ,/* PARAMETER 3 */ @M_COD_SAT_ENTITY    NVARCHAR(100)
        ,/* PARAMETER 4 */ @FIELD_3       BIGINT
        ,/* PARAMETER 5 */ @M_COD_INSTANCE      BIGINT
      
      AS
      DECLARE @LOG    VARCHAR(200)
      -- BEGIN: BEGIN TRY
      BEGIN TRY
      
      PRINT @FIELD_2
      
      
      INSERT INTO XXXXXXXX
             (
             [FIELD_1],
             [FIELD_2],
             [FIELD_3],
             [FIELD_4],
            [FIELD_5],
             [FIELD_6]      ,              -- [DT_ID]
             [FIELD_7]      ,                  -- [DP_ID]
             [FIELD_8]      ,                  -- [KOA_ID]
             [FIELD_9]      ,                      -- [KO_ID]
             [FIELD_10]      ,                 -- [PDI_ID]
             [FIELD_11]      ,                 -- [LBI_ID]
             [FIELD_12],
             [FIELD_13],
             [FIELD_14],
             [FIELD_15],
             [FIELD_16],
             [FIELD_17],
             [FIELD_18],
             [FIELD_19],
             [FIELD_20],
             [FIELD_21],
             [FIELD_22],
             [FIELD_23],
             [FIELD_24],
             [FIELD_25],
             [FIELD_26],
             [FIELD_27],
             [FIELD_28],
             [FIELD_29],
             [FIELD_30],
             [FIELD_31],
             [FIELD_32],
             [FIELD_33],
             [FIELD_34],
             [FIELD_35],
             [FIELD_36],
             [FIELD_37],
             [FIELD_38],
             [FIELD_39],
             [FIELD_40],
             [FIELD_41],
             [FIELD_42],
             [FIELD_43],
             [FIELD_44],
             [FIELD_45],
             [FIELD_46]
             )
             SELECT 
                    @FIELD_1 [FIELD_1],
                    @FIELD_2 [FIELD_2],
                    @FIELD_3 [FIELD_3],
                    BINARY_CHECKSUM(HASHBYTES('MD5', CONVERT( VARCHAR(MAX), [KC].[FIELD_13]))                 --FIELD_4
                    , HASHBYTES('MD5', CONVERT(VARCHAR(MAX), [KC].[FIELD_14]))
                  , HASHBYTES('MD5', CONVERT(VARCHAR(MAX), [KC].[FIELD_15]))
                  , HASHBYTES('MD5', CONVERT(VARCHAR(MAX), [KC].[FIELD_22]))
                  , HASHBYTES('MD5', CONVERT(VARCHAR(MAX), [KC].[FIELD_23]))
                  , HASHBYTES('MD5', CONVERT(VARCHAR(MAX), [KC].[FIELD_24]))
                  , HASHBYTES('MD5', CONVERT(VARCHAR(MAX), [KC].[FIELD_25]))
                  , HASHBYTES('MD5', CONVERT(VARCHAR(MAX), [KC].[FIELD_26]))
                  , HASHBYTES('MD5', CONVERT(VARCHAR(MAX), [KC].[FIELD_27]))
                  , HASHBYTES('MD5', CONVERT(VARCHAR(MAX), [KC].[FIELD_28]))
                  , HASHBYTES('MD5', CONVERT(VARCHAR(MAX), [KC].[FIELD_29]))
                  , HASHBYTES('MD5', CONVERT(VARCHAR(MAX), [KC].[FIELD_30]))
                  , HASHBYTES('MD5', CONVERT(VARCHAR(MAX), [KC].[FIELD_31]))
                  , HASHBYTES('MD5', CONVERT(VARCHAR(MAX), [KC].[FIELD_32]))
                  , HASHBYTES('MD5', CONVERT(VARCHAR(MAX), [KC].[FIELD_33]))
                  , HASHBYTES('MD5', CONVERT(VARCHAR(MAX), [KC].[FIELD_34]))
                  , HASHBYTES('MD5', CONVERT(VARCHAR(MAX), [KC].[FIELD_35]))
                  , HASHBYTES('MD5', CONVERT(VARCHAR(MAX), [KC].[FIELD_36]))
                  , HASHBYTES('MD5', CONVERT(VARCHAR(MAX), [KC].[FIELD_37]))
                  , HASHBYTES('MD5', CONVERT(VARCHAR(MAX), [KC].[FIELD_38]))
                  , HASHBYTES('MD5', CONVERT(VARCHAR(MAX), [KC].[FIELD_39]))
                  , HASHBYTES('MD5', CONVERT(VARCHAR(MAX), [KC].[FIELD_40]))
                  , HASHBYTES('MD5', CONVERT(VARCHAR(MAX), [KC].[FIELD_41]))
                  , HASHBYTES('MD5', CONVERT(VARCHAR(MAX), [KC].[FIELD_42]))
                  ) [FIELD_4],
                  [KC].[FIELD_5],
                    [KC].[FIELD_6]      ,             -- [DT_ID]
                    [SCDD].[FIELD_9] AS [FIELD_7]      , -- [DP_ID]
                    [KC].[FIELD_9] AS [FIELD_8]      ,  -- [KOA_ID]
                    [KC].[FIELD_9]      ,                 -- [KO_ID]
                    [LPDI].[M_FIELD_10]      ,            -- [PDI_ID]
                    [HVM].[M_FIELD_11]      ,         -- [LBI_ID]
                    1 AS [FIELD_12]      ,
      
                    -- Feiten voor FIELD_4
                    [KC].[FIELD_13],
                    [KC].[FIELD_14],
                    CASE
                        WHEN [KC].[FIELD_15] > 0
                        THEN [KC].[FIELD_15]
                        ELSE 0
                    END AS [FIELD_15]      ,
      
      
                    CASE
                        WHEN [KC].[FIELD_15] < 0
                        THEN-1 * [KC].[FIELD_15]
                        ELSE 0
                    END AS [FIELD_16],
                    0 [FIELD_17]      ,
      
                    CASE
                        WHEN [KC].[FIELD_15] > 0
                        THEN 1
                        ELSE 0
                    END AS [FIELD_18],
                    CASE
                        WHEN [KC].[FIELD_22] NOT IN('T', 'S', 'W')
                        THEN DATEDIFF([D], [KC].[FIELD_6], @FIELD_2)
                        ELSE NULL
                    END AS [FIELD_19],
                    CASE
                        WHEN [KC].[FIELD_22] NOT IN('T', 'S', 'W')
                        THEN DATEDIFF([M], [KC].[FIELD_6], @FIELD_2)
                        ELSE NULL
                    END AS [FIELD_20],
                    CASE
                        WHEN [KC].[FIELD_14] > 0
                        THEN 1
                        ELSE 0
                    END AS [FIELD_21]      ,
                    [KC].[FIELD_22],
                    [KC].[FIELD_23],
                    [KC].[FIELD_24],
                    [KC].[FIELD_25],
                    [KC].[FIELD_26],
                    [KC].[FIELD_27],
                    [KC].[FIELD_28],
                    [KC].[FIELD_29],
                    [KC].[FIELD_30],
                    [KC].[FIELD_31],
                    [KC].[FIELD_32],
                    [KC].[FIELD_33],
                    [KC].[FIELD_34],
                    [KC].[FIELD_35],
                    [KC].[FIELD_36],
                    [KC].[FIELD_37],
                    [KC].[FIELD_38],
                    [KC].[FIELD_39],
                    [KC].[FIELD_40],
                    [KC].[FIELD_41],
                    [KC].[FIELD_42],
                    NULL AS [FIELD_43]    ,
                    CONVERT(         NUMERIC(6, 1), [IAPR]) AS [FIELD_44]    ,
                    CONVERT(         NUMERIC(6, 3), (POWER(1.000000 + ([IAPR] / 100), 1.00000000 / 12.000000) - 1) * 100) AS [FIELD_45]    ,
                    CONVERT(         NUMERIC(6, 1), ((POWER(1.000000 + ([IAPR] / 100), 1.00000000 / 12.000000) - 1) * 100) * 12) AS [FIELD_46]
      
      
             FROM [SCHEMA_1].[TABLE_1] [KC] 
                  LEFT JOIN [DATABASE_1].[SCHEMA_1].[TABLE_2] [HCD]
                  ON [HCD].[FIELD_47] = [KC].[FIELD_47]
                     AND [HCD].[FIELD_48] = 3001
                     AND FIELD_2 BETWEEN [HCD].[FIELD_52] AND [HCD].[FIELD_53] 
                  LEFT JOIN [DATABASE_1].[SCHEMA_2].[TABLE_3] [LCC]
                  ON [HCD].[FIELD_50] = [LCC].[FIELD_50]
                     AND [LCC].[FIELD_51] = 186
                     AND FIELD_2 BETWEEN [LCC].[FIELD_52] AND [LCC].[FIELD_53] 
                  LEFT JOIN [DATABASE_1].[SCHEMA_2].[TABLE_4] [HCD2]
                  ON [HCD2].[FIELD_50] = [LCC].[FIELD_50_2]
                     AND [HCD2].[FIELD_48] = 3005
                     AND FIELD_2 BETWEEN [HCD2].[FIELD_52] AND [HCD2].[FIELD_53] 
                  LEFT JOIN [SCHEMA_1].[TABLE_5] [SCDD]
                  ON [SCDD].[FIELD_50] = [HCD].[FIELD_50]
                     AND FIELD_2 BETWEEN [SCDD].[FIELD_52] AND [SCDD].[FIELD_53] 
                  LEFT JOIN [SCHEMA_2].[TABLE_6] [LPDI]
                  ON [LPDI].[FIELD_50] = [KC].[FIELD_50]
                     AND FIELD_2 BETWEEN [LPDI].[FIELD_52] AND [LPDI].[FIELD_53]
                     AND [LPDI].[FIELD_51] = 184 
                  LEFT JOIN [SCHEMA_2].[TABLE_7] [LVMS]
                  ON [LVMS].[FIELD_50] = [KC].[FIELD_50]
                     AND FIELD_2 BETWEEN [LVMS].[FIELD_52] AND [LVMS].[FIELD_53]
                     AND [LVMS].[FIELD_51] = 192 
                  LEFT JOIN [SCHEMA_2].[TABLE_8] [HVMS]
                  ON [HVMS].[FIELD_55] = [LVMS].[FIELD_55]
                     AND FIELD_2 BETWEEN [HVMS].[FIELD_52] AND [HVMS].[FIELD_53]
                     AND [HVMS].[FIELD_48] = 1100 
                  LEFT JOIN [SCHEMA_2].[TABLE_9] [LVM]
                  ON [LVM].[FIELD_55_1] = [LVMS].[FIELD_55]
                     AND FIELD_2 BETWEEN [LVM].[FIELD_52] AND [LVM].[FIELD_53]
                     AND [LVM].[FIELD_51] = 181 
                  LEFT JOIN [SCHEMA_2].[TABLE_10] [HVM]
                  ON [HVM].[FIELD_55] = [LVM].[FIELD_55_2]
                     AND FIELD_2 BETWEEN [HVM].[FIELD_52] AND [HVM].[FIELD_53]
                     AND [HVM].[FIELD_48] = 1110 
                  LEFT JOIN [SCHEMA_3].[TABLE_11] [I]
                  ON [I].[ISERL] = [KC].[FIELD_56]
                     AND FIELD_2 BETWEEN [I].[FIELD_52] AND [I].[FIELD_53]
             WHERE 
             FIELD_2 BETWEEN [KC].[FIELD_52] AND [KC].[FIELD_53];
      
      SET @LOG = 'SF_PROCESS_SNAPSHOT(' + CONVERT(VARCHAR(19),@FIELD_2) + ') inserted :'  + convert(varchar,@@ROWCOUNT)  
      
      
      EXECUTE XXX.XXX 
      /* PAR 1 */  @FIELD_3
      /* PAR 2 */ ,@M_COD_INSTANCE
      /* PAR 3 */ ,'INFORMATION'
      /* PAR 4 */ ,@LOG
      -- END INSERT LOG
      
      END TRY
      
      BEGIN CATCH
        SELECT ERROR_MESSAGE()
        SET @LOG = ERROR_MESSAGE()
        EXECUTE XXX.XXX
      /* PAR 1 */  @FIELD_3
      /* PAR 2 */ ,@M_COD_INSTANCE
      /* PAR 3 */ ,'ERROR'
      /* PAR 4 */ ,@LOG
      END CATCH 
      
      
      
      GO
      

0 个答案:

没有答案