2

I have SQL Server 2008 with multiple databases on it. CPU get's up to 100% on that server quite a lot. I would like to figure out which database might be causing it. What is the easies way to do that?

My "magic" idal tool would show me:

  1. CPU, RAM and IO for SQL Server
  2. Drill down and show the same for each DB
  3. Drill down and show what queries causing issues, etc..
katit
  • 213
  • 4
  • 9
  • Been a while I cannot remember, is there a "Query Analyzer" tool that comes with that version? – Tim Dec 09 '11 at 16:41
  • Yes, there is Query analyzer. But how do I use it? Is it going to give me breakdown of CPU per database? – katit Dec 09 '11 at 16:43
  • Query Analyzer allows you to breaks down problem queries to see where they are spending their time...if you know the queries in question. SQL Server 2008 does have an Activity Monitor which will help you determine where the problem is when it is happening. – Top__Hat Dec 09 '11 at 17:02

2 Answers2

1

I think you will find your answer in one of these two posts: 1, and 2.

Khaled
  • 36,533
  • 8
  • 72
  • 99
0

I prefer to use information from system view sys.dm_exec_query_stats. Activity monitor is inconvenient for me.

To get information from sys.dm_exec_query_stats I use following script which I found here (unfortunatly it is in Russian). Works for SQL Server 2005+.

Using this script you can filter or group queries by the fields you want and, what is more important, get information about last execution time and object (SP, trigger) to which the query belong (Activity Monitor does not provide this information).

The only disadvantage of the script is that it returns aggregate performance statistics for cached query plans only. If a plan is removed from cache by any reason (shortage of memory, recompilation) you will not see statistics for that query.

set transaction isolation level read uncommitted
select 
    top 100
    creation_time,
    last_execution_time,
    execution_count,
    total_worker_time/1000 as CPU,
    convert(money, (total_worker_time))/(execution_count*1000)as [AvgCPUTime],
    qs.total_elapsed_time/1000 as TotDuration,
    convert(money, (qs.total_elapsed_time))/(execution_count*1000)as [AvgDur],
    total_logical_reads as [Reads],
    total_logical_writes as [Writes],
    total_logical_reads+total_logical_writes as [AggIO],
    convert(money, (total_logical_reads+total_logical_writes)/(execution_count + 0.0))as [AvgIO],
    case 
        when sql_handle IS NULL then ' '
        else(substring(st.text,(qs.statement_start_offset+2)/2,(
            case
                when qs.statement_end_offset =-1 then len(convert(nvarchar(MAX),st.text))*2      
                else qs.statement_end_offset    
            end - qs.statement_start_offset)/2  ))
    end as query_text,
    db_name(st.dbid)as database_name,
    object_schema_name(st.objectid, st.dbid)+'.'+object_name(st.objectid, st.dbid) as object_name
from sys.dm_exec_query_stats  qs
cross apply sys.dm_exec_sql_text(sql_handle) st
where total_logical_reads > 0
order by AvgDur desc