There are many ways to find out why your SQL server is performing slow. Suppose you know that your server is responding slowly while executing queries and you want to find out the query that takes a lot of time to execute. If you want to find out slow queries, you can use dynamic management views from version 2005 onwards. However you can still use the old system stored procedure sp_who2 along with management views, to find this info.
Run the following code
EXEC sp_who2
and see the result shown below. Find out rows with runnable status with highest CPUTime.
Sp_who2 will not display the text of the query, so we need to find out the text using dm_exec_requests and dm_exec_sql_text management views. The view dm_exec_requests will show the text of the query. All we need to do is to find out spid from the result of sp_who2 and filter it in dm_exec_requests using session_id column. The following code displays the text of the query.
SELECT
sql.text AS statement_text
FROM
sys.dm_exec_requests AS req
CROSS APPLY sys.dm_exec_sql_text(req.sql_handle) as sql
WHERE
req.session_id=52
sql.text AS statement_text
FROM
sys.dm_exec_requests AS req
CROSS APPLY sys.dm_exec_sql_text(req.sql_handle) as sql
WHERE
req.session_id=52
Similarly you can also use dynamic management views to find out various root causes for performance issues.
1 comment:
The community written SP_WHO3 can be your friend here as well :)
Post a Comment