Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to find current long running queries in SQL Server and how to kill them instantly?

Tags:

sql-server

Sometimes my application runs slow. The major problem is that some expensive reports are running. How can I find these reports and how to kill these instantly?

like image 394
Prudhviraj kamineni Avatar asked Jul 24 '17 09:07

Prudhviraj kamineni


People also ask

How to find which queries are running from a long time?

You can find which queries are running from a long time and utilizing CPU. To run this query, start SQL Server Management Studio, Open New Querywindow and copy below query in it. Now click on Executebutton to run this query.

How to find currently running SQL queries on SQL Server?

This will help you find currently running SQL queries on SQL Server. You can find which queries are running from a long time and utilizing CPU. To run this query, start SQL Server Management Studio, Open New Query window and copy below query in it. Now click on Execute button to run this query. SELECT sqltext.TEXT, req.session_id, req.status, ...

How to kill/stop a query immediately?

You can use a keyboard shortcut ALT + Break to stop the query execution. However, this may not succeed in all cases. Show activity on this post. Find Session-Id and Description for respective all running queries and then copy specific query's Session-Id which you want to kill/stop immediately. Show activity on this post.

How to run a query in SQL Server management studio?

To run this query, start SQL Server Management Studio, Open New Querywindow and copy below query in it. Now click on Executebutton to run this query.


1 Answers

You can use the following command to get the long running queries.

SELECT r.session_id,
       st.TEXT AS batch_text,
       qp.query_plan AS 'XML Plan',
       r.start_time,
       r.status,
       r.total_elapsed_time
FROM sys.dm_exec_requests AS r
     CROSS APPLY sys.dm_exec_sql_text(r.sql_handle) AS st
     CROSS APPLY sys.dm_exec_query_plan(r.plan_handle) AS qp
WHERE DB_NAME(r.database_id) = '{db_name}'
ORDER BY cpu_time DESC;

Then you can use

KILL 60 

to kill session_id 60 for example.

like image 166
Sonu K Avatar answered Oct 05 '22 09:10

Sonu K