KILL

KILL is a command in SQL used to stop a running process. It sends a termination signal to a process in the system, identified by the process ID, bringing the execution of the process to an immediate halt. It is most commonly used to terminate processes that are unresponsive or are causing undesirable effects on the database's performance.

Example

The SQL code below demonstrates how to use the “KILL” command.

SHOW PROCESSLIST;
KILL 1234;

Output

The result of the “SHOW PROCESSLIST” command might look like this:

+-----+-----+-----------+----+---------+------+-------+------------------+
| Id | User| Host | db | Command | Time | State | Info |
+-----+-----+-----------+----+---------+------+-------+------------------+
| 1234| root| localhost | my_db | Query | 10 | updating | UPDATE table |
| 4321| root| localhost | my_db | Query | 2 | updating | SELECT * FROM table |
+-----+-----+-----------+----+---------+------+-------+------------------+

After the “KILL” command, you would get no output:

Empty set (0.00 sec)

Explanation

The “SHOW PROCESSLIST” statement lists the current MySQL server threads. Each row displays information about a thread. The “KILL” statement is used to terminate a specific thread. In this scenario, the thread with id 1234 is terminated. Note that only users with SUPER privilege can use this statement to kill threads.

Example

KILL 54;

Output

This command does not produce an output.

Explanation

The KILL statement in SQL Server is utilized to terminate a specific session or task. In the provided example, KILL 54; would terminate the session or task with the session ID of 54. Notice that there is no output from this command - if the session or task is successfully terminated, the command completes without error.

Example

SELECT sid, serial# FROM v$session WHERE username = 'SCOTT';
ALTER SYSTEM KILL SESSION 'sid,serial#';

Output

SID SERIAL#
--------- ----------
23 63423
System altered.

Explanation

This code first identifies the session ID (sid) and serial number (serial#) of a specific user (in this case, ‘SCOTT’). The ALTER SYSTEM KILL SESSION command is then used to terminate the session associated with these identifiers. The output displays the session ID and serial number, and confirms that the system command has successfully altered the system.

For in-depth explanations and examples SQL keywords where you write your SQL, install our extension.