Monday, April 12, 2010

High CPU Usage due to recompilation

/*

High CPU Pressure can also happen due to queries/batches
that are being recompiled frequently
plan_genration_num indicates multiple plans .
If  plan_genration_num  > 0 , then there is recompilation
*/

select top 50 plan_generation_num, execution_count,
db_name(depamain.dbid) as dbname,object_name(depamain.objectid,depamain.dbid) as objname,
(
select substring(text, statement_start_offset/2 + 1,
 (
  case when statement_end_offset = -1 then len(convert(nvarchar(max),text)) * 2
  else statement_end_offset
  end - statement_start_offset)/2
 )
from sys.dm_exec_sql_text(sql_handle)
) as query_text
from sys.dm_exec_query_stats
outer apply
(
 select dbid,objectid from

  (
    select attribute,cast(value as int) as attvalue
    from sys.dm_exec_plan_attributes(plan_handle)
    where attribute in ('dbid','objectid')
) as depa
pivot
 (
  max(depa.attvalue) for depa.attribute in ("dbid", "objectid")
 ) as depapvt
) as depamain
where plan_generation_num >1
order by plan_generation_num desc;

----------------------------------------------------------------------

No comments: