This stored procedure displays the top worst performing queries based on CPU, Execution Count,
I/O and Elapsed_Time as identified using DMV information. This can be display the worst
performing queries from an instance, or database perspective. The number of records shown,
the database, and the sort order are identified by passing parameters.
Performance is vital to your SQL Reports or applications. With this easy to use procedure, you can find the worst performing SQL Queries.
Parameters
There are three different parameters that can be passed to this procedures: @DBNAME, @COUNT
and @ORDERBY. The @DBNAME is used to constraint the output to a specific database. If
when calling this SP this parameter is set to a specific database name then only statements
that are associated with that database will be displayed. If the @DBNAME parameter is not set
then this SP will return rows associated with any database. The @COUNT parameter allows you
to control the number of rows returned by this SP. If this parameter is used then only the
TOP x rows, where x is equal to @COUNT will be returned, based on the @ORDERBY parameter.
The @ORDERBY parameter identifies the sort order of the rows returned in descending order.
This @ORDERBY parameters supports the following type: CPU, AE, TE, EC or AIO, TIO, ALR, TLR, ALW, TLW, APR, and TPR
where “ACPU” represents Average CPU Usage
“TCPU” represents Total CPU usage
“AE” represents Average Elapsed Time
“TE” represents Total Elapsed Time
“EC” represents Execution Count
“AIO” represents Average IOs
“TIO” represents Total IOs
“ALR” represents Average Logical Reads
“TLR” represents Total Logical Reads
“ALW” represents Average Logical Writes
“TLW” represents Total Logical Writes
“APR” represents Average Physical Reads
“TPR” represents Total Physical Read
Typical execution calls
Top 6 statements in the AdventureWorks database base on Average CPU Usage
EXEC usp_Worst_TSQL @DBNAME='AdventureWorks',@COUNT=6,@ORDERBY='ACPU'Top 100 statements order by Average Logical Read
EXEC usp_Worst_TSQL @COUNT=100,@ORDERBY='ALR'Show top 100 statements by Average IO
EXEC usp_Worst_TSQL;Frequently asked questions
How do I find the slowest queries in SQL Server?
Use the stored procedure usp_Worst_TSQL. It shows the top worst performing queries based on CPU, execution count, I/O and elapsed time, using DMV information.
What parameters does usp_Worst_TSQL accept?
It takes three parameters: @DBNAME limits the output to one database, @COUNT sets the number of rows, and @ORDERBY sets the descending sort order.
Which @ORDERBY values does usp_Worst_TSQL support?
Values include ACPU and TCPU for CPU, AE and TE for elapsed time, EC for execution count, AIO and TIO for I/O, and ALR, TLR, ALW, TLW, APR and TPR for logical reads, writes and physical reads.
How do I show the top 6 queries by average CPU for one database?
Run EXEC usp_Worst_TSQL @DBNAME='AdventureWorks',@COUNT=6,@ORDERBY='ACPU'. This returns the six statements with the highest average CPU usage in AdventureWorks.
Can usp_Worst_TSQL look at a whole instance?
Yes. If you leave @DBNAME empty, the procedure returns rows for any database. You can view the worst queries from an instance or a database perspective.
Comments
Related blogs
Calculate Lost Customers using T-SQL
Calculate DateDiff With Only WeekDays or Workdays
T-TSQL - Create 4 week Moving Total