Table of Contents
前言:
准备工作:
步骤:
分析:
Home Database Mysql Tutorial 第十六章处理锁、阻塞和死锁(3)使用SQLServer Profile

第十六章处理锁、阻塞和死锁(3)使用SQLServer Profile

Jun 07, 2016 pm 03:18 PM
sqlserver use deal with deadlock block

前言: 作为DBA,可能经常会遇到有同事或者客户反映经常发生死锁,影响了系统的 使用 。此时,你需要尽快侦测和 处理 这类问题。 死锁是当两个或者以上的事务互相 阻塞 引起的。在这种情况下两个事务会无限期地等待对方释放资源以便操作。下面是死锁的示意图

前言:

作为DBA,可能经常会遇到有同事或者客户反映经常发生死锁,影响了系统的使用。此时,你需要尽快侦测和处理这类问题。

死锁是当两个或者以上的事务互相阻塞引起的。在这种情况下两个事务会无限期地等待对方释放资源以便操作。下面是死锁的示意图:

第十六章处理锁、阻塞和死锁(3)使用SQLServer Profile


本文将使用SQLServer Profiler来跟踪死锁。

 

准备工作:

为了侦测死锁,我们需要先模拟死锁。本例将使用两个不同的会话创建两个事务。

 

步骤:

1、 打开SQLServer Profiler

2、 选择【新建跟踪】,连到实例。

3、 然后选择【空白】模版:

第十六章处理锁、阻塞和死锁(3)使用SQLServer Profile

4、 在【事件选择】页中,展开Locks事件,并选择以下事件:

1、 Deadlock graph

2、 Lock:Deadlock

3、 Lock:Deadlock Chain

第十六章处理锁、阻塞和死锁(3)使用SQLServer Profile

5、 然后打开TSQL事件,并选择以下事件:

1、 SQL:StmtCompleted

2、 SQL:StmtStarting

第十六章处理锁、阻塞和死锁(3)使用SQLServer Profile

6、 点击【列筛选器】,在跟踪属性中,选择数据库名为需要侦测的数据库,这里使用AdventureWorks。

第十六章处理锁、阻塞和死锁(3)使用SQLServer Profile

7、 在【组织列】中,调整顺序,如下:

第十六章处理锁、阻塞和死锁(3)使用SQLServer Profile

8、 点击运行。

9、 然后打开SQLServer,并打开两个连接。

10、 在第一个窗口中输入并执行下面脚本:


USE AdventureWorks 
GO
SET TRANSACTION ISOLATION LEVEL REPEATABLE READ
GO
BEGIN TRANSACTION
SELECT  *
FROM    Sales.SalesOrderDetail
WHERE   SalesOrderDetailID = 121316
Copy after login



11、 然后在第二个窗口中输入并执行下面脚本: 


USE AdventureWorks
GO
SET TRANSACTION ISOLATION LEVEL REPEATABLE READ
BEGIN TRANSACTION
SELECT  *
FROM    Sales.SalesOrderDetail
WHERE   SalesOrderDetailID = 121317
Copy after login



12、现在回到第一个窗体,并运行下面的脚本: 


UPDATE Sales.SalesOrderDetail
SET OrderQty=2
WHERE SalesOrderDetailID=121317
Copy after login



13、在第二个窗口输入下面语句: 


UPDATE Sales.SalesOrderDetail
SET OrderQty=2
WHERE SalesOrderDetailID=121316
Copy after login



14、 然后在第二个窗口就会看到下面的消息: 

第十六章处理锁、阻塞和死锁(3)使用SQLServer Profile

15、切换到SQLServer Profiler,可以看到下面的截图:

第十六章处理锁、阻塞和死锁(3)使用SQLServer Profile

16、 点击【Deadlock graph】时间,会显示死锁的图像:

第十六章处理锁、阻塞和死锁(3)使用SQLServer Profile

17、可以保存死锁图像,右键然后选择导出事件数据,并另存为xdl文件:

第十六章处理锁、阻塞和死锁(3)使用SQLServer Profile

下面是其XML格式:

 第十六章处理锁、阻塞和死锁(3)使用SQLServer Profile

分析:

在本文中,首先创建一个Profiler空白模版,然后选择下面的事件进行监控:


 

1、 Deadlock graph

2、 Lock:Deadlock

3、 Lock:Deadlock Chain

4、 SQL:StmtCompleted

5、 SQL:StmtStarting

然后通过限定数据库,来限制监控过得对象范围。

在配置好之后,运行跟踪,并在ssms中运行脚本。SQLServer会自动处理和侦测这种类型的死锁。然后会在第二个窗体中收到1205的错误。

在SQLServer Profiler中,演示了如何收集死锁事件,在跟踪结果中可以看到两个事务尝试在一个拥有共享锁的键上添加排它锁。通过死锁图像,可以看到死锁发生的细节。

为了避免或者最小化死锁的发生,有一些建议可以参考:

1、 确保你的事务尽可能地小,这里指范围。

2、 使用较低隔离级别的事务。

3、 对于可能的查询,使用NOLOCK查询提示。

4、 规范化数据库设计。

5、 在需要的列上创建索引,以便是表不需要经常扫描,减少锁问题的发生。

6、 控制数据库对象访问的顺序是相同的顺序。

 

 

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

How to import mdf file into sqlserver How to import mdf file into sqlserver Apr 08, 2024 am 11:41 AM

The import steps are as follows: Copy the MDF file to SQL Server's data directory (usually C:\Program Files\Microsoft SQL Server\MSSQL\DATA). In SQL Server Management Studio (SSMS), open the database and select Attach. Click the Add button and select the MDF file. Confirm the database name and click the OK button.

How to solve the problem that the object named already exists in the sqlserver database How to solve the problem that the object named already exists in the sqlserver database Apr 05, 2024 pm 09:42 PM

For objects with the same name that already exist in the SQL Server database, the following steps need to be taken: Confirm the object type (table, view, stored procedure). IF NOT EXISTS can be used to skip creation if the object is empty. If the object has data, use a different name or modify the structure. Use DROP to delete existing objects (use caution, backup recommended). Check for schema changes to make sure there are no references to deleted or renamed objects.

How to check sqlserver port number How to check sqlserver port number Apr 05, 2024 pm 09:57 PM

To view the SQL Server port number: Open SSMS and connect to the server. Find the server name in Object Explorer, right-click it and select Properties. In the Connection tab, view the TCP Port field.

How to recover accidentally deleted database in sqlserver How to recover accidentally deleted database in sqlserver Apr 05, 2024 pm 10:39 PM

If you accidentally delete a SQL Server database, you can take the following steps to recover: stop database activity; back up log files; check database logs; recovery options: restore from backup; restore from transaction log; use DBCC CHECKDB; use third-party tools. Please back up your database regularly and enable transaction logging to prevent data loss.

What to do if the sqlserver service cannot be started What to do if the sqlserver service cannot be started Apr 05, 2024 pm 10:00 PM

When the SQL Server service fails to start, here are some steps to resolve: Check the error log to determine the root cause. Make sure the service account has permission to start the service. Check whether dependency services are running. Disable antivirus software. Repair SQL Server installation. If the repair does not work, reinstall SQL Server.

Where is the sqlserver database? Where is the sqlserver database? Apr 05, 2024 pm 08:21 PM

SQL Server database files are usually stored in the following default location: Windows: C:\Program Files\Microsoft SQL Server\MSSQL\DATALinux: /var/opt/mssql/data The database file location can be customized by modifying the database file path setting.

BTCC tutorial: How to bind and use MetaMask wallet on BTCC exchange? BTCC tutorial: How to bind and use MetaMask wallet on BTCC exchange? Apr 26, 2024 am 09:40 AM

MetaMask (also called Little Fox Wallet in Chinese) is a free and well-received encryption wallet software. Currently, BTCC supports binding to the MetaMask wallet. After binding, you can use the MetaMask wallet to quickly log in, store value, buy coins, etc., and you can also get 20 USDT trial bonus for the first time binding. In the BTCCMetaMask wallet tutorial, we will introduce in detail how to register and use MetaMask, and how to bind and use the Little Fox wallet in BTCC. What is MetaMask wallet? With over 30 million users, MetaMask Little Fox Wallet is one of the most popular cryptocurrency wallets today. It is free to use and can be installed on the network as an extension

How to use Baidu Netdisk app How to use Baidu Netdisk app Mar 27, 2024 pm 06:46 PM

Cloud storage has become an indispensable part of our daily life and work nowadays. As one of the leading cloud storage services in China, Baidu Netdisk has won the favor of a large number of users with its powerful storage functions, efficient transmission speed and convenient operation experience. And whether you want to back up important files, share information, watch videos online, or listen to music, Baidu Cloud Disk can meet your needs. However, many users may not understand the specific use method of Baidu Netdisk app, so this tutorial will introduce in detail how to use Baidu Netdisk app. Users who are still confused can follow this article to learn more. ! How to use Baidu Cloud Network Disk: 1. Installation First, when downloading and installing Baidu Cloud software, please select the custom installation option.

See all articles