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
Old Hand
Old Hand (366 reputation)Old Hand (366 reputation)Old Hand (366 reputation)Old Hand (366 reputation)Old Hand (366 reputation)Old Hand (366 reputation)Old Hand (366 reputation)Old Hand (366 reputation)

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

Group: General Forum Members
Points: 17110 Visits: 3979
Thanks, very interesting. I will play around with it in our environment and see what I see with this.
akhamoshioke
akhamoshioke
Old Hand
Old Hand (366 reputation)Old Hand (366 reputation)Old Hand (366 reputation)Old Hand (366 reputation)Old Hand (366 reputation)Old Hand (366 reputation)Old Hand (366 reputation)Old Hand (366 reputation)

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

Group: General Forum Members
Points: 16836 Visits: 4141
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

-------------------
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
Old Hand
Old Hand (366 reputation)Old Hand (366 reputation)Old Hand (366 reputation)Old Hand (366 reputation)Old Hand (366 reputation)Old Hand (366 reputation)Old Hand (366 reputation)Old Hand (366 reputation)

Group: General Forum Members
Points: 366 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
SSCoach
SSCoach (17K reputation)SSCoach (17K reputation)SSCoach (17K reputation)SSCoach (17K reputation)SSCoach (17K reputation)SSCoach (17K reputation)SSCoach (17K reputation)SSCoach (17K reputation)

Group: General Forum Members
Points: 17483 Visits: 3517
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
Old Hand
Old Hand (366 reputation)Old Hand (366 reputation)Old Hand (366 reputation)Old Hand (366 reputation)Old Hand (366 reputation)Old Hand (366 reputation)Old Hand (366 reputation)Old Hand (366 reputation)

Group: General Forum Members
Points: 366 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
Old Hand
Old Hand (307 reputation)Old Hand (307 reputation)Old Hand (307 reputation)Old Hand (307 reputation)Old Hand (307 reputation)Old Hand (307 reputation)Old Hand (307 reputation)Old Hand (307 reputation)

Group: General Forum Members
Points: 307 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
SSCoach
SSCoach (17K reputation)SSCoach (17K reputation)SSCoach (17K reputation)SSCoach (17K reputation)SSCoach (17K reputation)SSCoach (17K reputation)SSCoach (17K reputation)SSCoach (17K reputation)

Group: General Forum Members
Points: 17483 Visits: 3517
"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-Dedicated
SSC-Dedicated (38K reputation)SSC-Dedicated (38K reputation)SSC-Dedicated (38K reputation)SSC-Dedicated (38K reputation)SSC-Dedicated (38K reputation)SSC-Dedicated (38K reputation)SSC-Dedicated (38K reputation)SSC-Dedicated (38K reputation)

Group: General Forum Members
Points: 38832 Visits: 886
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