Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to kill/stop a long SQL query immediately?

I am using SQL server 2008 and its management studio. I executed a query that yields many rows. I tried to cancel it via the red cancel button, but it has not stopped for the past 10 minutes. It usually stops within 3 minutes.

What could the reason be and how do I stop it immediately ?

like image 282
sequel.learner Avatar asked Apr 10 '13 09:04

sequel.learner


People also ask

How do you kill a long running query in SQL?

Use KILL SPID command to eliminate blocking in SQL Server Execute the following query in SSMS session. The SPID for this session is 60. In another session, it tries to get records of the table. The SPID for this session is 83.

How do you force kill in SQL?

SQL Server Management Studio Activity Monitor Once Activity Monitor has loaded, expand the 'Processes' section. Scroll down to the SPID of the process you would like to kill. Right click on that line and select 'Kill Process'. A popup window will open for you to confirm that you want to kill the process.

How do I stop a long running query in SQL Developer?

In SQL Developer, click Tools, then Monitor Sessions. In the Select Connection dialog box, select a connection to SYSTEM (or another account with full DBA privileges) Right-click in the row for the session to be terminated, and select Kill Session.

How do you kill a long running job in SQL Server?

Expand SQL Server Agent, expand Jobs, right-click the job you want to stop, and then click Stop Job.


2 Answers

sp_who2 'active' 

Check values under CPUTime and DiskIO. Note the SPID of process having large value comparatively.

kill {SPID value} 
like image 128
Mudassir Hasan Avatar answered Sep 19 '22 14:09

Mudassir Hasan


What could the reason be

A query cancel is immediate, provided that your attention can reach the server and be processed. A query must be in a cancelable state, which is almost always true except if you do certain operations like calling a web service from SQLCLR. If your attention cannot reach the server it's usually due to scheduler overload.

But if your query is part of a transaction that must rollback, then rollback cannot be interrupted. If it takes 10 minutes then it needs 10 minutes and there's nothing you can do about it. Even restarting the server will not help, it will only make startup longer since recovery must finish the rollback.

To answer which specific reason applies to your case, you'll need to investigate yourself.

like image 39
Remus Rusanu Avatar answered Sep 18 '22 14:09

Remus Rusanu