This article gives an overview of the KILL SPID command and how to monitor the rollback progress.

本文概述了KILL SPID命令以及如何监视回滚进度。

介绍 (Introduction)

Once we connect to a SQL Server instance, it assigns a unique identification number to each connection. It is the Server Process ID and commonly known as SPID. SPID 1 to 50 is reserved for SQL Server internal processes, and the user process starts from SPID 51.

一旦连接到SQL Server实例,它将为每个连接分配一个唯一的标识号。 它是服务器进程ID,通常称为SPID。 SPID 1到50保留用于SQL Server内部进程,而用户进程从SPID 51开始。

在SQL Server中检查SPID的不同方法 (Different ways to check the SPID in SQL Server)

You can check all processes in SQL Server using the following ways.

您可以使用以下方法检查SQL Server中的所有进程。

使用SP_who2命令: (Using SP_who2 command:)

Execute the sp_who2 command, and it lists out all the SPID’s including the system and user processes.

执行sp_who2命令,它列出了所有SPID,包括系统进程和用户进程。

使用动态管理视图: (Using Dynamic Management View:)

We can use the DMV sys.dm_exec_sessions to get the session details.

我们可以使用DMV sys.dm_exec_sessions来获取会话详细信息。

SELECT *FROM sys.dm_exec_sessions;By default, it shows all processes in SQL Server. We might not be interested in the system processes. We can filter the results using the following query.SELECT *FROM sys.dm_exec_sessionsWHERE is_user_process = 1;

使用活动监视器: (Using Activity Monitor:)

Connect to a SQL instance in SSMS and, right-click on SQL instance. Launch Activity Monitor. It opens the following Activity Monitor that contains Overview, Processes tabs.

连接到SSMS中SQL实例,然后右键单击SQL实例。 启动活动监视器。 它将打开以下活动监视器,其中包含“概述”,“进程”选项卡。

In the Processes tab, we can view the SP ID and details such as login, database, application, hostname. By default, Activity Monitor displays the User processes as shown in the following image.

在“进程”选项卡中,我们可以查看SP ID和详细信息,例如登录名,数据库,应用程序,主机名。 默认情况下,活动监视器显示用户进程,如下图所示。

If we want to view all SPID’s, set the User Process column value from the drop-down to All (1 – User Process, 0- System process).

如果要查看所有SPID,请从下拉列表中将“用户进程”列值设置为“全部”(1 –用户进程,0 –系统进程)。

使用SSMS信息栏: (Using SSMS information bar:)

Once we open a new session in SSMS, it shows the following information in the information bar.

在SSMS中打开新会话后,它将在信息栏中显示以下信息。

  • Connection Status连接状态
  • Instance name in which we are connected我们连接的实例名称
  • User name (SPID). In the following image, we can see that we are connected with Kashish\Test(84). In bracket, it is a SPID. It is 84 in our case

    用户名(SPID)。 在下图中,我们可以看到我们已与Kashish \ Test(84)连接。 方括号中是SPID。 在我们的情况下是84

@@ SPID全局变量: (The @@SPID global variable:)

We can use the global variable to know about the session id of the currently connected session. It is useful if we connect to SQL Server using the command line tools such as SQLCMD.

我们可以使用全局变量来了解当前连接的会话的会话ID。 如果我们使用命令行工具(例如SQLCMD)连接到SQL Server,这将非常有用。

SELECT @@SPID AS CurrentSPID;

SQL Server中的KILL SPID命令概述 (Overview of KILL SPID command in SQL Server)

Suppose you receive a call that the particular SQL instance is running slow. You start troubleshooting and identify a SPID causing blocking for other SPID’s, and it is taking many system resources. You require terminating this query to release the high consuming resources a remove blocking due to this session.

假设您收到一个调用,说明特定SQL实例运行缓慢。 您开始进行故障排除并确定一个SPID导致其他SPID的阻塞,并且占用大量系统资源。 您需要终止此查询以释放高消耗资源,并且由于此会话而导致移除阻塞。

You can use the KILL SPID command to kill a particular user session. You can only KILL the user processes. Once we kill a session, it undergoes through the rollback process, and it might take time and resources as well to perform a rollback.

您可以使用KILL SPID命令杀死特定的用户会话。 您只能杀死用户进程。 一旦我们终止了会话,它将经历回滚过程,执行回滚可能还需要时间和资源。

Before we look more on KILL SPID, we need to understand that Microsoft SQL Server follows the ACID properties. All transactions should meet the ACID criteria.

在进一步了解KILL SPID之前,我们需要了解Microsoft SQL Server遵循ACID属性。 所有交易均应符合ACID标准。

  • Atomic: Transaction should be either complete or fail. There should not be any transaction in a status other than this 原子:事务应该完成或失败。 除此以外的任何状态都不应进行任何交易
  • Consistent: The database should be consistent before and after the transactions 一致:交易前后数据库应一致
  • Isolated: Multiple transactions should run, but it should not put the transactions in inconsistent mode 隔离:应该运行多个事务,但不应使事务处于不一致模式
  • Durability: All transactions should be durable. Once a record is committed, it should remain committed regardless of the system failure. SQL Server needs to maintain a failed or uncompleted transaction to roll back in case of any failure 耐用性:所有交易都应持久。 提交记录后,无论系统故障如何,记录都应保持提交状态。 SQL Server需要维护失败或未完成的事务以在发生任何失败时回滚

Once we KILL SPID in SQL Server, it becomes an uncompleted transaction. SQL Server must undo all changes performed by the uncompleted transaction and database objects should be returned to the original state before these transactions. Due to this reason, you can see the status of SPID as KILLED\ROLLBACK once the rollback is in progress. Rollback might take less time in most of the case; however, it entirely depends upon the changes it needs to rollback.

一旦我们在SQL Server中杀死SPID,它便成为未完成的事务。 SQL Server必须撤消由未完成的事务执行的所有更改,并且在执行这些事务之前,数据库对象应返回到原始状态。 由于这个原因,一旦进行回滚,您就可以看到SPID的状态为KILLED \ ROLLBACK。 在大多数情况下,回滚可能会花费较少的时间。 但是,这完全取决于它需要回滚的更改。

Let start an active transaction for inserting a large number of rows in the SQL table.

让我们开始一个活动事务,以便在SQL表中插入大量行。

CREATE TABLE [dbo].[tblSQLShackDemo]([S.No.] [int] IDENTITY(0,1) NOT NULL,[value] [uniqueidentifier] NULL,[Date] [datetime] NULL) ON [PRIMARY]GOALTER TABLE [dbo].[tblSQLShackDemo] ADD  DEFAULT (getdate()) FOR [Date]GOBegin transactionDeclare @Id intSet @Id = 1While @Id <= 1000000Begin Insert Into tblSQLShackDemo(value) values (newid())Set @Id = @Id + 1End

Check the status of the query using the SP_who2 command.

使用SP_who2命令检查查询的状态。

After some time, use the KILL command to KILL SPID using the following command. Execute this command in a new query window.

一段时间后,使用KILL命令通过以下命令杀死SPID。 在新的查询窗口中执行此命令。

KILL 84

Once we execute the KILL SPID command, SQL Server starts the ROLLBACK process for this query. You can see the Status as ROLLBACK in the following image.

一旦执行KILL SPID命令,SQL Server就会为该查询启动ROLLBACK进程。 下图可以看到状态为ROLLBACK。

We might be interested to know the rollback time. We can check the estimated roll back time using the following command:

我们可能想知道回滚时间。 我们可以使用以下命令检查估计的回滚时间:

KILL SPID with STATUSONLY

定期杀死SPID

We want to check the ROLLBACK status for the SPID 84, therefore, execute the query.

我们要检查SPID 84的ROLLBACK状态,因此,执行查询。

KILL 84 with STATUSONLY

杀死84人

The KILL command with STATUSONLY does not kill any process. It returns the estimated return time for the rollback in seconds.

STATUSONLY的KILL命令不会终止任何进程。 它返回回滚的估计返回时间(以秒为单位)。

In the following screenshot, you can estimate completion time is 34 seconds. You need to execute this query again to get the updated time.

在以下屏幕截图中,您可以估计完成时间为34秒。 您需要再次执行此查询以获取更新时间。

In another test, I inserted more records in the table and KILL the session before completion. In the following screenshot, we can see the estimated roll back time 3567 seconds (approx. 1 hour).

在另一个测试中,我在表中插入了更多记录,并在完成之前杀死了该会话。 在下面的屏幕截图中,我们可以看到估计的回滚时间3567秒(约1小时)。

使用KILL SPID命令消除SQL Server中的阻塞 (Use KILL SPID command to eliminate blocking in SQL Server )

We might face blocking in SQL Server, and sometimes we require KILLING the blocker SPID. In SQL Server, we get blocking issues when a SPID holds a lock on a specific resource and another SPID tries to acquire a conflicting lock on the same resource.

我们可能会在SQL Server中遇到阻塞,有时我们需要杀死阻塞者SPID。 在SQL Server中,当一个SPID持有特定资源的锁,而另一个SPID试图获取同一资源的冲突锁时,就会遇到阻塞问题。

Firstly, let’s generate a blocking scenario. Execute the following query in SSMS session. The SPID for this session is 60.

首先,让我们生成一个阻塞方案。 在SSMS会话中执行以下查询。 此会话的SPID为60。

Begin transactionUpdate tblSQLShackDemo set value=newid()

In another session, it tries to get records of the table. The SPID for this session is 83.

在另一个会话中,它尝试获取表的记录。 该会话的SPID为83。

Select * from tblSQLShackDemo

The select statement keeps running and does not return any rows. You can see the status as Executing Query in the following screenshot.

select语句保持运行状态,并且不返回任何行。 您可以在以下屏幕截图中看到状态为“正在执行查询 ”。

Let’s wait for more time, but still, the query shows the status as Executing Query.

让我们等待更多时间,但是查询仍然将状态显示为“正在执行查询”

We can check the blocking using the sp_who2 command or using the DMV sys.dm_exec_requests.

我们可以使用sp_who2命令或DMV sys.dm_exec_requests检查阻塞。

Execute the following DMV in another query window of SSMS.

在SSMS的另一个查询窗口中执行以下DMV。

SELECT blocking_session_id ,* FROM sys.dm_exec_requestsWHERE blocking_session_id <> 0;GO

In the output, we can see that session id 83 (Select statement) is blocked due to session id 60 ( Update statement).

在输出中,我们可以看到由于会话ID 60(更新语句)而阻止了会话ID 83(选择语句)。

Due to blocking, we do not get any result for the Select statement. We need to terminate SPID that is causing the blocking using the KILL SPID command. In my example, SPID 60 is the culprit, so we need to kill it. Execute the following query.

由于阻塞,Select语句不会得到任何结果。 我们需要使用KILL SPID命令终止导致阻塞的SPID。 在我的示例中,SPID 60是元凶,因此我们需要将其杀死。 执行以下查询。

KILL 60

杀60

Once we KILL the blocking session id, it returns the result for the Select statement immediately.

一旦我们杀死了阻塞的会话ID,它将立即返回Select语句的结果。

  • Note: KILL SPID should be a temporary solution; we need to look at the queries causing blocking and tune the queries to fix performance issues
  • 注意: KILL SPID应该是一个临时解决方案。 我们需要查看引起阻塞的查询并调整查询以解决性能问题

使用KILL SPID命令终止正在运行的数据库备份 (Terminate running database backup using a KILL SPID command)

Suppose a Junior DBA started a full database backup for a large database in TB’s during business hours. It is taking system resources, and you do not want to continue with it.

假设初级DBA在工作时间内为TB中的大型数据库启动了完整的数据库备份。 它正在占用系统资源,并且您不想继续使用它。

For this demonstration, start database backup using the following script.

对于此演示,请使用以下脚本启动数据库备份。

BACKUP DATABASE [AdventureWorks2017] TO DISK = N'C:\SQLShack\AdventureWorks2017_2019_06_01_07_39.bak'WITH NOFORMAT, NOINIT, NAME = N'AdventureWorks2017-Full Database Backup', SKIP, NOREWIND, NOUNLOAD, COMPRESSION, STATS = 10;GO

While the backup is in progress, execute the sp_who2 command to see the backup database process and note down the SPID for it.

进行备份时,执行sp_who2命令以查看备份数据库进程并记下该备份的SPID。

Let’s assume we want to cancel the backup. Execute the KILL SPID command, and it immediately kills the backup process.

假设我们要取消备份。 执行KILL SPID命令,它将立即终止备份过程。

You get the following message in the backup database query window. It terminates the backup process, and you can see that the session is also in the disconnected state.

您在备份数据库查询窗口中收到以下消息。 它终止了备份过程,您可以看到该会话也处于断开状态。

结论 (Conclusion)

In this article, we explored the KILL SPID command to terminate a service process (SPID) and monitor the rollback status. You should use this command carefully in the production instance.

在本文中,我们探讨了KILL SPID命令以终止服务进程(SPID)并监视回滚状态。 您应该在生产实例中谨慎使用此命令。

翻译自: https://www.sqlshack.com/kill-spid-command-in-sql-server/

SQL Server中的KILL SPID命令相关推荐

  1. SQL Server中的T-SQL RegEx命令

    This article explores T-SQL RegEx commands in SQL Server for performing data search using various co ...

  2. sql ddl中key_SQL DDL:SQL Server中SQL DDL命令入门

    sql ddl中key This article explains SQL DDL commands in Microsoft SQL Server using a few simple exampl ...

  3. sas rename重命名_如何使用sp_rename命令在SQL Server中重命名表

    sas rename重命名 In this article, we will learn to rename tables in SQL Server. sp_rename is a built-in ...

  4. SQL Server中的事务与锁

    了解事务和锁 事务:保持逻辑数据一致性与可恢复性,必不可少的利器. 锁:多用户访问同一数据库资源时,对访问的先后次序权限管理的一种机制,没有他事务或许将会一塌糊涂,不能保证数据的安全正确读写. 死锁: ...

  5. Microsoft SQL Server中的事务(转载)

    1.1 事务的概念 事务是作为单个工作单元而执行的一系列操作,比如查询和修改数据等. 事务是数据库并发控制的基本单位,一条或者一组语句要么全部成功,对数据库中的某些数据成功修改; 要么全部不成功,数据 ...

  6. SQL Server中关于跟踪(Trace)那点事(转载)

    前言 一提到跟踪俩字,很多人想到警匪片中的场景,同样在我们的SQL Server数据库中"跟踪"也是无处不在的,如果我们利用好了跟踪技巧,就可以针对某些特定的场景做定向分析,找出充 ...

  7. SQL Server中的快照隔离

    介绍 (Introduction) In this article, I'll explore the use of snapshot isolation using row versioning a ...

  8. 在SQL Server中使用sp_WhoIsActive监视活动

    In this article, we will talk about sp_WhoIsActive stored procedure and how we can use it to monitor ...

  9. sql server重命名_在Linux上SQL Server中重命名逻辑和物理文件名

    sql server重命名 Each database in SQL Server contains at least two files i.e. Data file (*.mdf) and log ...

最新文章

  1. crx header invalid_解决Chrome插件安装时程序包无效【CRX_HEADER_INVALID】(转载+xPath插件)...
  2. 远离这样的管理层:靠加班、靠团建、靠个人
  3. 关于 SAP Spartacus feature library 里的 _index.scss 文件,和神奇的下划线省略行为
  4. Blazor WebAssembly 3.2.0 Preview 4 如期发布
  5. 异步编程中的最佳做法(Async/Await) --转
  6. leetcode python3 简单题219. Contains Duplicate II
  7. App设计之路——WebApp浅谈
  8. vue-admin-template修改登录功能
  9. win10cmd重置系统_命令提示符修复系统win10 系统还原
  10. 微信H5分享、复制链接遇到的坑
  11. 给初学日语者的几点建议——词汇篇
  12. 【JS】match() 方法
  13. Windows自带的【远程桌面连接】操作方法及常见问题
  14. 微信支付V3之投诉回调API封装
  15. 浴血黑帮第一季/全集Peaky Blinders迅雷下载
  16. 最新版本的Google Chrome浏览器如何设置网页编码?
  17. Mac下手动安装Tex/Latex Package
  18. java实时获取汇率
  19. 【Unity】Firebase-Google登录身份验证功能接入流程
  20. 硅谷来信丨一招阻击恶意软件攻击:沙箱集成MITRE ATTCK攻击框架

热门文章

  1. mysql 1130本地连接_mysql ERROR 1130 问题解决方案
  2. java和c语言的区别_都说C语言不会过时,但你是否还需要掌握其他语言?
  3. Linux磁盘系统——管理磁盘的命令
  4. C++变量作用域、生存期、存储类别
  5. Motan:目录结构
  6. 如何在symfony 控制器里面创建soap web service
  7. JAVA中Math类的random()方法使用
  8. 存储过程中while循环
  9. React Native中一些常用组件用法
  10. js中短路运算符 ||