未生成SP缓存计划

时间:2011-02-02 06:10:49

标签: database sql-server-2005 coalesce sqlcachedependency

这句话有些奇怪 COALESCE(@param_ids +',','')

@param_ids作为param传入,它是@param_ids VARCHAR(MAX)

知道为什么SQL-Server无法为以下场景生成缓存计划。 这个问题已被问到微软,他们仍在研究它。

不工作方案 - 未生成缓存计划 第1步

改变SP

 IF (LTRIM(RTRIM(ISNULL(@param_ids,''))) = '')  
  BEGIN  
   SELECT @param_ids = COALESCE(@param_ids + ',', '') + CONVERT(VARCHAR(50),ID)  
   FROM [dbo].Content_Type WITH (NOLOCK)  
  END  

第2步 Exec Sp with params

第3步(无缓存计划)

SELECT *
FROM sys.dm_exec_cached_plans 
CROSS APPLY sys.dm_exec_sql_text(plan_handle) 
WHERE [dbid] = DB_ID('databasename')
AND [objectid] = OBJECT_ID('databasename.dbo.us_spname')
GO

工作方案 - 生成缓存计划 第1步

改变SP

 IF (LTRIM(RTRIM(ISNULL(@param_ids,''))) = '')  
  BEGIN  
   --SELECT @param_ids = COALESCE(@param_ids + ',', '') + CONVERT(VARCHAR(50),ID)  
   --FROM [dbo].Content_Type WITH (NOLOCK)  

    SELECT @param_ids = COALESCE('aaaaaaa' + ',', '') + CONVERT(VARCHAR(50),ID)  
    FROM [dbo].Content_Type WITH (NOLOCK)   
  END  

第2步 Exec Sp with params

第3步(缓存计划退出)

SELECT *
FROM sys.dm_exec_cached_plans 
CROSS APPLY sys.dm_exec_sql_text(plan_handle) 
WHERE [dbid] = DB_ID('databasename')
AND [objectid] = OBJECT_ID('databasename.dbo.us_spname')
GO

Thankx

1 个答案:

答案 0 :(得分:0)

我添加了一个WAITFOR,我一直都有计划。这应该独立于参数

CREATE PROC dbo.testplan @param_ids varchar(max) OUTPUT
AS
IF (LTRIM(RTRIM(ISNULL(@param_ids,''))) = '')  
  BEGIN  
   SELECT @param_ids = COALESCE(@param_ids + ',', '') + CONVERT(VARCHAR(50),ID)  
   FROM [dbo].Content_Type WITH (NOLOCK)  
  END  
GO
CREATE PROC dbo.testplan2 @param_ids varchar(max) OUTPUT
AS
IF (LTRIM(RTRIM(ISNULL(@param_ids,''))) = '')  
  BEGIN  
   SELECT @param_ids = COALESCE(@param_ids + ',', '') + CONVERT(VARCHAR(50),ID)  
   FROM [dbo].Content_Type WITH (NOLOCK)  
  END  
GO

EXEC dbo.testplan 'bar'
GO
WAITFOR DELAY '00:00:02'
SELECT OBJECT_NAME([objectid]), *
FROM sys.dm_exec_cached_plans 
CROSS APPLY sys.dm_exec_sql_text(plan_handle) 
WHERE [dbid] = DB_ID()
AND [objectid] = OBJECT_ID('testplan')
GO

EXEC dbo.testplan2 ''
GO
WAITFOR DELAY '00:00:02'
SELECT OBJECT_NAME([objectid]), *
FROM sys.dm_exec_cached_plans 
CROSS APPLY sys.dm_exec_sql_text(plan_handle) 
WHERE [dbid] = DB_ID()
AND [objectid] = OBJECT_ID('testplan2')
GO