sp_BlitzCache doesn't fully hide itself anymore
I see statements like the ones below still in the cache. I can work around this for demos, not a huge problem, just wanted to alert you.
INSERT INTO ##bou_BlitzCacheProcs (QueryType INSERT INTO #p (SqlHandle, TotalCPU, TotalReads, TotalDuration, TotalWrites
Can you grab the full text from the plan cache? The INSERT INTO #p is marked for recompile: https://github.com/BrentOzarULTD/PublicTools/blob/develop/sp_BlitzCache/sp_BlitzCache.sql#L1080
Jeremiah Peschka - Managing Director, Brent Ozar Unlimited MCSE: Data Platform, MVP Cloudera Certified Developer for Apache Hadoop
On Thu, Feb 26, 2015 at 9:34 AM, Kendra Little [email protected] wrote:
I see statements like the ones below still in the cache. I can work around this for demos, not a huge problem, just wanted to alert you.
INSERT INTO ##bou_BlitzCacheProcs (QueryType INSERT INTO #p (SqlHandle, TotalCPU, TotalReads, TotalDuration, TotalWrites
— Reply to this email directly or view it on GitHub https://github.com/BrentOzarULTD/PublicTools/issues/123.
Sure. I tested against master and develop, is there one i should use more than the other to get the output?
Founder, Brent Ozar Unlimited® Microsoft Certified Master (SQL Server 2008) Microsoft Certified Solutions Expert, Data Platform (SQL Server 2012)
On Thu, Feb 26, 2015 at 10:15 AM, Jeremiah Peschka <[email protected]
wrote:
Can you grab the full text from the plan cache? The INSERT INTO #p is marked for recompile:
https://github.com/BrentOzarULTD/PublicTools/blob/develop/sp_BlitzCache/sp_BlitzCache.sql#L1080
Jeremiah Peschka - Managing Director, Brent Ozar Unlimited MCSE: Data Platform, MVP Cloudera Certified Developer for Apache Hadoop
On Thu, Feb 26, 2015 at 9:34 AM, Kendra Little [email protected] wrote:
I see statements like the ones below still in the cache. I can work around this for demos, not a huge problem, just wanted to alert you.
INSERT INTO ##bou_BlitzCacheProcs (QueryType INSERT INTO #p (SqlHandle, TotalCPU, TotalReads, TotalDuration, TotalWrites
— Reply to this email directly or view it on GitHub https://github.com/BrentOzarULTD/PublicTools/issues/123.
— Reply to this email directly or view it on GitHub https://github.com/BrentOzarULTD/PublicTools/issues/123#issuecomment-76233132 .
Unable to duplicate on sp_BlitzCache v2.4.4 and SQL Server 2014 12.0.2000.8 x64

Tried this using just EXEC sp_BlitzCache - let me know if there are other parameters that you used.
More info. I am on v2.4.4 - 2015-01-09.
Here is the repro query sequence.
DBCC FREEPROCCACHE; GO
SELECT TOP 10 PostTypeId, Body FROM dbo.Posts WHERE Id=100 /* I am a comment /; GO SELECT TOP 10 PostTypeId, Body FROM dbo.Posts WHERE Id=2 / I am ALSO a comment! */; GO
exec sp_BlitzCache @top=10, @hide_summary=1, @results='expert'; GO
exec sp_BlitzCache @top=10, @hide_summary=1, @results='expert'; GO
/* And here are the queries that get cached */
INSERT INTO ##bou_BlitzCacheProcs (QueryType, DatabaseName, AverageCPU, TotalCPU, AverageCPUPerMinute, PercentCPUByType, PercentDurationByType, PercentReadsByType, PercentExecutionsByType, AverageDuration, TotalDuration, AverageReads, TotalReads, ExecutionCount, ExecutionsPerMinute, TotalWrites, AverageWrites, PercentWritesByType, WritesPerMinute, PlanCreationTime, LastExecutionTime, StatementStartOffset, StatementEndOffset, MinReturnedRows, MaxReturnedRows, AverageReturnedRows, TotalReturnedRows, LastReturnedRows, QueryText, QueryPlan, TotalWorkerTimeForType, TotalElapsedTimeForType, TotalReadsForType, TotalExecutionCountForType, TotalWritesForType, SqlHandle, PlanHandle, QueryHash, QueryPlanHash, min_worker_time, max_worker_time, is_parallel, min_elapsed_time, max_elapsed_time, age_minutes, age_minutes_lifetime) SELECT TOP (@top) 'Procedure: ' + COALESCE(OBJECT_NAME(qs.object_id, qs.database_id),'') AS QueryType, COALESCE(DB_NAME(database_id), CAST(pa.value AS sysname), '-- N/A --') AS DatabaseName, (total_worker_time / 1000.0) / execution_count AS AvgCPU , (total_worker_time / 1000.0) AS TotalCPU , CASE WHEN total_worker_time = 0 THEN 0 WHEN COALESCE(age_minutes, DATEDIFF(mi, qs.cached_time, qs.last_execution_time), 0) = 0 THEN 0 ELSE CAST((total_worker_time / 1000.0) / COALESCE(age_minutes, DATEDIFF(mi, qs.cached_time, qs.last_execution_time)) AS MONEY) END AS AverageCPUPerMinute , CASE WHEN t.t_TotalWorker = 0 THEN 0 ELSE CAST(ROUND(100.00 * (total_worker_time / 1000.0) / t.t_TotalWorker, 2) AS MONEY) END AS PercentCPUByType, CASE WHEN t.t_TotalElapsed = 0 THEN 0 ELSE CAST(ROUND(100.00 * (total_elapsed_time / 1000.0) / t.t_TotalElapsed, 2) AS MONEY) END AS PercentDurationByType, CASE WHEN t.t_TotalReads = 0 THEN 0 ELSE CAST(ROUND(100.00 * total_logical_reads / t.t_TotalReads, 2) AS MONEY) END AS PercentReadsByType, CASE WHEN t.t_TotalExecs = 0 THEN 0 ELSE CAST(ROUND(100.00 * execution_count / t.t_TotalExecs, 2) AS MONEY) END AS PercentExecutionsByType, (total_elapsed_time / 1000.0) / execution_count AS AvgDuration , (total_elapsed_time / 1000.0) AS TotalDuration , total_logical_reads / execution_count AS AvgReads , total_logical_reads AS TotalReads , execution_count AS ExecutionCount , CASE WHEN execution_count = 0 THEN 0 WHEN COALESCE(age_minutes, DATEDIFF(mi, qs.cached_time, qs.last_execution_time), 0) = 0 THEN 0 ELSE CAST((1.00 * execution_count / COALESCE(age_minutes, DATEDIFF(mi, qs.cached_time, qs.last_execution_time))) AS money) END AS ExecutionsPerMinute , total_logical_writes AS TotalWrites , total_logical_writes / execution_count AS AverageWrites , CASE WHEN t.t_TotalWrites = 0 THEN 0 ELSE CAST(ROUND(100.00 * total_logical_writes / t.t_TotalWrites, 2) AS MONEY) END AS PercentWritesByType, CASE WHEN total_logical_writes = 0 THEN 0 WHEN COALESCE(age_minutes, DATEDIFF(mi, qs.cached_time, qs.last_execution_time), 0) = 0 THEN 0 ELSE CAST((1.00 * total_logical_writes / COALESCE(age_minutes, DATEDIFF(mi, qs.cached_time, qs.last_execution_time), 0)) AS money) END AS WritesPerMinute, qs.cached_time AS PlanCreationTime, qs.last_execution_time AS LastExecutionTime, NULL AS StatementStartOffset, NULL AS StatementEndOffset, NULL AS MinReturnedRows, NULL AS MaxReturnedRows, NULL AS AvgReturnedRows, NULL AS TotalReturnedRows, NULL AS LastReturnedRows, st.text AS QueryText , query_plan AS QueryPlan, t.t_TotalWorker, t.t_TotalElapsed, t.t_TotalReads, t.t_TotalExecs, t.t_TotalWrites, qs.sql_handle AS SqlHandle, qs.plan_handle AS PlanHandle, NULL AS QueryHash, NULL AS QueryPlanHash, qs.min_worker_time / 1000.0, qs.max_worker_time / 1000.0, CASE WHEN qp.query_plan.value('declare namespace p="http://schemas.microsoft.com/sqlserver/2004/07/showplan";max(//p:RelOp/@Parallel)', 'float') > 0 THEN 1 ELSE 0 END, qs.min_elapsed_time / 1000.0, qs.max_elapsed_time / 1000.0, age_minutes, age_minutes_lifetime FROM (SELECT *, CAST((CASE WHEN DATEDIFF(second, cached_time, GETDATE()) > 0 AND execution_count > 1 THEN DATEDIFF(second, cached_time, GETDATE()) / 60.0 ELSE NULL END) as MONEY) as age_minutes, CAST((CASE WHEN DATEDIFF(second, cached_time, last_execution_time) > 0 AND execution_count > 1 THEN DATEDIFF(second, cached_time, last_execution_time) / 60.0 ELSE Null END) as MONEY) as age_minutes_lifetime FROM sys.dm_exec_procedure_stats x ) AS qs CROSS JOIN(SELECT SUM(execution_count) AS t_TotalExecs, SUM(total_elapsed_time) / 1000.0 AS t_TotalElapsed, SUM(total_worker_time) / 1000.0 AS t_TotalWorker, SUM(total_logical_reads) AS t_TotalReads, SUM(total_logical_writes) AS t_TotalWrites FROM sys.dm_exec_procedure_stats) AS t CROSS APPLY sys.dm_exec_plan_attributes(qs.plan_handle) AS pa CROSS APPLY sys.dm_exec_sql_text(qs.sql_handle) AS st CROSS APPLY sys.dm_exec_query_plan(qs.plan_handle) AS qp WHERE pa.attribute = 'dbid' AND COALESCE(DB_NAME(database_id), CAST(pa.value AS sysname), '') NOT IN ('master', 'model', 'msdb', 'tempdb', '32767') ORDER BY total_worker_time DESC OPTION (RECOMPILE)
INSERT INTO ##bou_BlitzCacheProcs (QueryType, DatabaseName, AverageCPU, TotalCPU, AverageCPUPerMinute, PercentCPUByType, PercentDurationByType, PercentReadsByType, PercentExecutionsByType, AverageDuration, TotalDuration, AverageReads, TotalReads, ExecutionCount, ExecutionsPerMinute, TotalWrites, AverageWrites, PercentWritesByType, WritesPerMinute, PlanCreationTime, LastExecutionTime, StatementStartOffset, StatementEndOffset, MinReturnedRows, MaxReturnedRows, AverageReturnedRows, TotalReturnedRows, LastReturnedRows, QueryText, QueryPlan, TotalWorkerTimeForType, TotalElapsedTimeForType, TotalReadsForType, TotalExecutionCountForType, TotalWritesForType, SqlHandle, PlanHandle, QueryHash, QueryPlanHash, min_worker_time, max_worker_time, is_parallel, min_elapsed_time, max_elapsed_time, age_minutes, age_minutes_lifetime) SELECT TOP (@top) 'Procedure: ' + COALESCE(OBJECT_NAME(qs.object_id, qs.database_id),'') AS QueryType, COALESCE(DB_NAME(database_id), CAST(pa.value AS sysname), '-- N/A --') AS DatabaseName, (total_worker_time / 1000.0) / execution_count AS AvgCPU , (total_worker_time / 1000.0) AS TotalCPU , CASE WHEN total_worker_time = 0 THEN 0 WHEN COALESCE(age_minutes, DATEDIFF(mi, qs.cached_time, qs.last_execution_time), 0) = 0 THEN 0 ELSE CAST((total_worker_time / 1000.0) / COALESCE(age_minutes, DATEDIFF(mi, qs.cached_time, qs.last_execution_time)) AS MONEY) END AS AverageCPUPerMinute , CASE WHEN t.t_TotalWorker = 0 THEN 0 ELSE CAST(ROUND(100.00 * (total_worker_time / 1000.0) / t.t_TotalWorker, 2) AS MONEY) END AS PercentCPUByType, CASE WHEN t.t_TotalElapsed = 0 THEN 0 ELSE CAST(ROUND(100.00 * (total_elapsed_time / 1000.0) / t.t_TotalElapsed, 2) AS MONEY) END AS PercentDurationByType, CASE WHEN t.t_TotalReads = 0 THEN 0 ELSE CAST(ROUND(100.00 * total_logical_reads / t.t_TotalReads, 2) AS MONEY) END AS PercentReadsByType, CASE WHEN t.t_TotalExecs = 0 THEN 0 ELSE CAST(ROUND(100.00 * execution_count / t.t_TotalExecs, 2) AS MONEY) END AS PercentExecutionsByType, (total_elapsed_time / 1000.0) / execution_count AS AvgDuration , (total_elapsed_time / 1000.0) AS TotalDuration , total_logical_reads / execution_count AS AvgReads , total_logical_reads AS TotalReads , execution_count AS ExecutionCount , CASE WHEN execution_count = 0 THEN 0 WHEN COALESCE(age_minutes, DATEDIFF(mi, qs.cached_time, qs.last_execution_time), 0) = 0 THEN 0 ELSE CAST((1.00 * execution_count / COALESCE(age_minutes, DATEDIFF(mi, qs.cached_time, qs.last_execution_time))) AS money) END AS ExecutionsPerMinute , total_logical_writes AS TotalWrites , total_logical_writes / execution_count AS AverageWrites , CASE WHEN t.t_TotalWrites = 0 THEN 0 ELSE CAST(ROUND(100.00 * total_logical_writes / t.t_TotalWrites, 2) AS MONEY) END AS PercentWritesByType, CASE WHEN total_logical_writes = 0 THEN 0 WHEN COALESCE(age_minutes, DATEDIFF(mi, qs.cached_time, qs.last_execution_time), 0) = 0 THEN 0 ELSE CAST((1.00 * total_logical_writes / COALESCE(age_minutes, DATEDIFF(mi, qs.cached_time, qs.last_execution_time), 0)) AS money) END AS WritesPerMinute, qs.cached_time AS PlanCreationTime, qs.last_execution_time AS LastExecutionTime, NULL AS StatementStartOffset, NULL AS StatementEndOffset, NULL AS MinReturnedRows, NULL AS MaxReturnedRows, NULL AS AvgReturnedRows, NULL AS TotalReturnedRows, NULL AS LastReturnedRows, st.text AS QueryText , query_plan AS QueryPlan, t.t_TotalWorker, t.t_TotalElapsed, t.t_TotalReads, t.t_TotalExecs, t.t_TotalWrites, qs.sql_handle AS SqlHandle, qs.plan_handle AS PlanHandle, NULL AS QueryHash, NULL AS QueryPlanHash, qs.min_worker_time / 1000.0, qs.max_worker_time / 1000.0, CASE WHEN qp.query_plan.value('declare namespace p="http://schemas.microsoft.com/sqlserver/2004/07/showplan";max(//p:RelOp/@Parallel)', 'float') > 0 THEN 1 ELSE 0 END, qs.min_elapsed_time / 1000.0, qs.max_elapsed_time / 1000.0, age_minutes, age_minutes_lifetime FROM (SELECT *, CAST((CASE WHEN DATEDIFF(second, cached_time, GETDATE()) > 0 AND execution_count > 1 THEN DATEDIFF(second, cached_time, GETDATE()) / 60.0 ELSE NULL END) as MONEY) as age_minutes, CAST((CASE WHEN DATEDIFF(second, cached_time, last_execution_time) > 0 AND execution_count > 1 THEN DATEDIFF(second, cached_time, last_execution_time) / 60.0 ELSE Null END) as MONEY) as age_minutes_lifetime FROM sys.dm_exec_trigger_stats x ) AS qs CROSS JOIN(SELECT SUM(execution_count) AS t_TotalExecs, SUM(total_elapsed_time) / 1000.0 AS t_TotalElapsed, SUM(total_worker_time) / 1000.0 AS t_TotalWorker, SUM(total_logical_reads) AS t_TotalReads, SUM(total_logical_writes) AS t_TotalWrites FROM sys.dm_exec_trigger_stats) AS t CROSS APPLY sys.dm_exec_plan_attributes(qs.plan_handle) AS pa CROSS APPLY sys.dm_exec_sql_text(qs.sql_handle) AS st CROSS APPLY sys.dm_exec_query_plan(qs.plan_handle) AS qp WHERE pa.attribute = 'dbid' AND COALESCE(DB_NAME(database_id), CAST(pa.value AS sysname), '') NOT IN ('master', 'model', 'msdb', 'tempdb', '32767')
INSERT INTO #p (SqlHandle, TotalCPU, TotalReads, TotalDuration, TotalWrites, ExecutionCount) SELECT SqlHandle, TotalCPU, TotalReads, TotalDuration, TotalWrites, ExecutionCount FROM (SELECT SqlHandle, TotalCPU, TotalReads, TotalDuration, TotalWrites, ExecutionCount, ROW_NUMBER() OVER (PARTITION BY SqlHandle ORDER BY TotalCPU DESC) AS rn FROM ##bou_BlitzCacheProcs) AS x WHERE x.rn = 1 OPTION (RECOMPILE)