SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


Query to Find Top CPU Queries


Query to Find Top CPU Queries

Author
Message
akhamoshioke
akhamoshioke
SSC-Enthusiastic
SSC-Enthusiastic (168 reputation)SSC-Enthusiastic (168 reputation)SSC-Enthusiastic (168 reputation)SSC-Enthusiastic (168 reputation)SSC-Enthusiastic (168 reputation)SSC-Enthusiastic (168 reputation)SSC-Enthusiastic (168 reputation)SSC-Enthusiastic (168 reputation)

Group: General Forum Members
Points: 168 Visits: 468
Comments posted to this topic are about the item Query to Find Top CPU Queries
Summer90
Summer90
SSCertifiable
SSCertifiable (6.8K reputation)SSCertifiable (6.8K reputation)SSCertifiable (6.8K reputation)SSCertifiable (6.8K reputation)SSCertifiable (6.8K reputation)SSCertifiable (6.8K reputation)SSCertifiable (6.8K reputation)SSCertifiable (6.8K reputation)

Group: General Forum Members
Points: 6833 Visits: 3831
Thanks, very interesting. I will play around with it in our environment and see what I see with this.
akhamoshioke
akhamoshioke
SSC-Enthusiastic
SSC-Enthusiastic (168 reputation)SSC-Enthusiastic (168 reputation)SSC-Enthusiastic (168 reputation)SSC-Enthusiastic (168 reputation)SSC-Enthusiastic (168 reputation)SSC-Enthusiastic (168 reputation)SSC-Enthusiastic (168 reputation)SSC-Enthusiastic (168 reputation)

Group: General Forum Members
Points: 168 Visits: 468
Please do and post your feedback on the flaws with it and possible fixes so i can keep improving it.
webrunner
webrunner
SSCertifiable
SSCertifiable (7.6K reputation)SSCertifiable (7.6K reputation)SSCertifiable (7.6K reputation)SSCertifiable (7.6K reputation)SSCertifiable (7.6K reputation)SSCertifiable (7.6K reputation)SSCertifiable (7.6K reputation)SSCertifiable (7.6K reputation)

Group: General Forum Members
Points: 7605 Visits: 3998
akhamoshioke (6/3/2014)
Please do and post your feedback on the flaws with it and possible fixes so i can keep improving it.


Thanks for this script!

It works great on SQL Server 2008 R2 Enterprise Edition.

On SQL Server 2005 Standard Edition, however I got these errors when I tried to run it.


Msg 139, Level 15, State 1, Line 0
Cannot assign a default value to a local variable.
Msg 137, Level 15, State 2, Line 10
Must declare the scalar variable "@debugFlg".
Msg 137, Level 15, State 2, Line 11
Must declare the scalar variable "@ServerTime".
Msg 137, Level 15, State 2, Line 20
Must declare the scalar variable "@ServerTime".
Msg 137, Level 15, State 2, Line 43
Must declare the scalar variable "@FilterHours".
Msg 137, Level 15, State 2, Line 51
Must declare the scalar variable "@debugFlg".
Msg 137, Level 15, State 2, Line 66
Must declare the scalar variable "@ConvertMiliSeconds".



Any thoughts on how to fix this? Or does the code need to be modified for SQL 2005?

Thanks again,
webrunner

-------------------
"I love spending twice as long and working twice as hard to get half as much done!" – Nobody ever.
Ref.: http://www.adminarsenal.com/admin-arsenal-blog/powershell-how-to-write-your-first-powershell-script

"Operator! Give me the number for 911!" - Homer Simpson

"A SQL query walks into a bar and sees two tables. He walks up to them and says 'Can I join you?'"
Ref.: http://tkyte.blogspot.com/2009/02/sql-joke.html
akhamoshioke
akhamoshioke
SSC-Enthusiastic
SSC-Enthusiastic (168 reputation)SSC-Enthusiastic (168 reputation)SSC-Enthusiastic (168 reputation)SSC-Enthusiastic (168 reputation)SSC-Enthusiastic (168 reputation)SSC-Enthusiastic (168 reputation)SSC-Enthusiastic (168 reputation)SSC-Enthusiastic (168 reputation)

Group: General Forum Members
Points: 168 Visits: 468
Thanks for sharing your findings. The code was written to work with SQL Server 2008/2008R2 and will have to be modified for sql 2005. I do have a link from databasejournal.com tha should help you. Please do test it before using it in production;
Smile

http://www.databasejournal.com/features/mssql/article.php/3737936/Top-Queries-in-SQL-Server-2005.htm

Hope this helps. Remember to mark it as a solution if it does help you
SQL Galaxy
SQL Galaxy
SSCertifiable
SSCertifiable (6.5K reputation)SSCertifiable (6.5K reputation)SSCertifiable (6.5K reputation)SSCertifiable (6.5K reputation)SSCertifiable (6.5K reputation)SSCertifiable (6.5K reputation)SSCertifiable (6.5K reputation)SSCertifiable (6.5K reputation)

Group: General Forum Members
Points: 6457 Visits: 3501
Hi, Thanks for sharing script for finding top CPU queries.

select
servername,runtime,max(DBName) as DBName,max(QueryExecuted) as QueryExecuted,(sum(total_worker_time)/sum(execution_count))/@ConvertMiliSeconds as AvgCPUTime
,sum(execution_count) as execution_count,query_hash, max(ObjectName) as ObjectName
into ##FindTopCPUQueries_set2
from ##FindTopCPUQueries_set1
group by query_hash,servername,runtime
order by AvgCPUTime desc

could you explain me? AvgCPUTime

As per output, there is one query taking too much time as below

AvgCPUTime = 136722
execution_count = 25,

So, what is the per execution time?
per execution = 136722/25 = 5468 (is it milliseconds or seconds)

Thanks
ananda
akhamoshioke
akhamoshioke
SSC-Enthusiastic
SSC-Enthusiastic (168 reputation)SSC-Enthusiastic (168 reputation)SSC-Enthusiastic (168 reputation)SSC-Enthusiastic (168 reputation)SSC-Enthusiastic (168 reputation)SSC-Enthusiastic (168 reputation)SSC-Enthusiastic (168 reputation)SSC-Enthusiastic (168 reputation)

Group: General Forum Members
Points: 168 Visits: 468
AvgCPUTime = 136722 -- Mathematical average cpu time (in miliseconds) it took per query execution.
execution_count = 25, -- Total count of executions of the query.

I bet when you look at the dmvs for your wait tasks and waiting tasks, you'll see a lot of CXPACKET waits (indicative of CPU waits) and you'll also notice a lot of query timeouts et all with that query during peak period.
adzuanamir
adzuanamir
SSC-Enthusiastic
SSC-Enthusiastic (101 reputation)SSC-Enthusiastic (101 reputation)SSC-Enthusiastic (101 reputation)SSC-Enthusiastic (101 reputation)SSC-Enthusiastic (101 reputation)SSC-Enthusiastic (101 reputation)SSC-Enthusiastic (101 reputation)SSC-Enthusiastic (101 reputation)

Group: General Forum Members
Points: 101 Visits: 39
Hi
I have this output after run this script.

servername runtime DBName
KLAS-HDC-SQLPRD\APPSPRD 50:14.1 PreparedSQL

Why it is returning PreparedSQL? I dont have database name as PreparedSQL.
One more thing, what is query_hash column for?
SQL Galaxy
SQL Galaxy
SSCertifiable
SSCertifiable (6.5K reputation)SSCertifiable (6.5K reputation)SSCertifiable (6.5K reputation)SSCertifiable (6.5K reputation)SSCertifiable (6.5K reputation)SSCertifiable (6.5K reputation)SSCertifiable (6.5K reputation)SSCertifiable (6.5K reputation)

Group: General Forum Members
Points: 6457 Visits: 3501
"PreparedSQL" means Ad Hoc query witten in that application side for DML operation, also it is not refer any stored procedure..
Iwas Bornready
Iwas Bornready
SSC-Insane
SSC-Insane (21K reputation)SSC-Insane (21K reputation)SSC-Insane (21K reputation)SSC-Insane (21K reputation)SSC-Insane (21K reputation)SSC-Insane (21K reputation)SSC-Insane (21K reputation)SSC-Insane (21K reputation)

Group: General Forum Members
Points: 21838 Visits: 885
It looks interesting, I'll have to give it a try. Thanks.
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search