Database Research & Development

  • Home
  • NoSQL
    • NoSQL
    • Cassandra
  • Databases
    • Database Theory
    • Database Designing
    • SQL Server Coding Standards
    • SQL Server
    • PostgreSQL
    • MySQL
    • Greenplum
    • Linux
  • Interviews
    • SQL Server Interviews
    • MySQL Interviews
    • SQL Puzzles
  • DBA Scripts
    • SQL Server DBA Scripts
    • PostgreSQL DBA Scripts
    • MySQL DBA Scripts
    • Greenplum DBA Scripts
  • Home
  • Blog Archives !
  • (: Laugh@dbrnd :)
  • Contact Me !
sqlserverinterviews
Home 2015 June SQL Server CPU usage per Different Objects

SQL Server CPU usage per Different Objects

This article is half-done without your Comment! *** Please share your thoughts via Comment ***

In this post, I am sharing a script find the usage of CPU for all different SQL Server Objects.

Using this script, DBA can monitor the CPU usages.

Below is a script:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
SELECT
DB_NAME(st.dbid) AS DatabaseName
,OBJECT_SCHEMA_NAME(st.objectid,dbid) AS SchemaName
,cp.objtype AS ObjectType
,OBJECT_NAME(st.objectid,dbid) AS Objects
,MAX(cp.usecounts)AS Total_Execution_count
,SUM(qs.total_worker_time) AS Total_CPU_Time
,SUM(qs.total_worker_time) / (max(cp.usecounts) * 1.0) AS Avg_CPU_Time
FROM sys.dm_exec_cached_plans cp
INNER JOIN sys.dm_exec_query_stats qs
ON cp.plan_handle = qs.plan_handle
CROSS APPLY sys.dm_exec_sql_text(cp.plan_handle) st
WHERE DB_NAME(st.dbid) IS NOT NULL
GROUP BY DB_NAME(st.dbid),OBJECT_SCHEMA_NAME(objectid,st.dbid),cp.objtype,OBJECT_NAME(objectid,st.dbid)
ORDER BY sum(qs.total_worker_time) desc
Jun 21, 2015Anvesh Patel
Script to Find Slowest Running Query in SQL ServerFind out most recently modified Stored Procedure and Table in SQL Server
Comments: 4
  1. Amarnath Sankaranarayanan
    August 13, 2015 at 7:50 am

    I would like to find(using SQL DMVs) whether a database is actually in use and if so then how much of that servers resources it is taking… and need some ideas on how we can properly collect usage statistics on all of these databases across all of these servers and then create a simple report to understand resources usage.
    The purpose of this report is to determine what are the critical counters that we need and what is the simplest and least impact method to collect them.
    We don’t need minute to minute, I’m only looking for summary information once per day or so.
    Can you please advise on this?

    • Anvesh Patel
      Anvesh Patel
      August 13, 2015 at 9:46 am

      Yes, You can find all database which are currently in use.
      Please use this script:

      SELECT request_session_id,resource_database_id
      FROM sys.dm_tran_locks
      WHERE resource_database_id = DB_ID('database_name')

      After execute this, if you get records for your database then your database is in use.
      And just remove filter so you can get all list of databases which is in use.
      You have to join this dbid with sql query written in post.

      Thank You.

  2. sdorttuii plmnr
    December 10, 2015 at 4:17 pm

    As soon as I noticed this web site I went on reddit to share some of the love with them.

  3. gill
    March 31, 2016 at 9:50 am

    Major thanks for the blog.Much thanks again. Great.

Anvesh Patel
Anvesh Patel

Database Engineer

June 21, 2015 SQL Server, SQL Server DBA ScriptAnvesh Patel, cpu usage, database, database research and development, dm_exec_cached_plans, dm_exec_query_stats, OBJECT_SCHEMA_NAME, SQL Query, SQL Server, SQL Server Administrator, SQL Server Monitoring, SQL Server Performance Tunning, SQL Server Tips and Tricks, TSQL
About Me!

I'm Anvesh Patel, a Database Engineer certified by Oracle and IBM. I'm working as a Database Architect, Database Optimizer, Database Administrator, Database Developer. Providing the best articles and solutions for different problems in the best manner through my blogs is my passion. I have more than six years of experience with various RDBMS products like MSSQL Server, PostgreSQL, MySQL, Greenplum and currently learning and doing research on BIGData and NoSQL technology. -- Hyderabad, India.

About DBRND !

dbrnd

This is a personal blog (www.dbrnd.com).

Any views or opinions represented in this blog are personal and belong solely to the blog owner and do not represent those of people, institutions or organizations that the owner may or may not be associated with in professional or personal capacity, unless explicitly stated.

Feel free to challenge me, disagree with me, or tell me I’m completely nuts in the comments section of each blog entry, but I reserve the right to delete any comment for any reason whatsoever (abusive, profane, rude, or anonymous comments) - so keep it polite.

The content of this website is protected by copyright. No portion of this website may be copied or replicated in any form without the written consent of the website owner.

Recent Comments !
  • Anvesh Patel { Sure will do... } – May 27, 12:43 PM
  • Anvesh Patel { Great... } – May 27, 12:41 PM
  • Anvesh Patel { Great... } – May 27, 12:39 PM
  • Anvesh Patel { Great... } – May 27, 12:36 PM
  • Anvesh Patel { Great... } – May 27, 12:28 PM
  • Anvesh Patel { Great... } – May 27, 12:27 PM
  • Anvesh Patel { Great... } – May 27, 12:16 PM
  • Older »
Follow Me !
  • facebook
  • linkedin
  • twitter
  • youtube
  • google
  • flickr
© 2015 – 2019 All rights reserved. Database Research & Development (dbrnd.com)
Posting....