Home Database Mysql Tutorial sqlserver阻塞定位(转载)

sqlserver阻塞定位(转载)

Jun 07, 2016 pm 03:40 PM
sqlserver position block

很多人都遇到过这样的情况,当网站达到一定的访问量,数据库就会成为瓶颈,进而引起阻塞。 有人认为这可能就是硬件的极限了,于是想办法增加硬件设备。而我本人认为问题的元凶可能是性能不高的sql脚本,引起了阻塞。 如果你和我有相同的看法,那我们就一起想

很多人都遇到过这样的情况,当网站达到一定的访问量,数据库就会成为瓶颈,进而引起阻塞。

有人认为这可能就是硬件的极限了,于是想办法增加硬件设备。而我本人认为问题的元凶可能是性能不高的sql脚本,引起了阻塞。

如果你和我有相同的看法,那我们就一起想办法找出问题的源头。

案例1.

某一天我被告知,我们的书城网站不能访问了,我马上查看,发现书城的有两台iis服务器均显示service unavailable,我初步断定是sqlserver数据库发生了阻塞,因为同一套程序使两台iis服务器同时当机的可能性不大。

要知道是否发生了阻塞,当然要看master库的sysprocess表,看看是否有什么进程堵住了别的进程,语句如下:

Select * from master..sysprocesses where blocked > 0

很快我发现,有一个blocked = 51 堵住了很多进程(查看blocked列可见),果然和我的判断吻合;为了进一步找出发生阻塞的语句,我用到的如下的语句

dbcc inputbuffer(51);

结果如下:

EventType      Parameters            EventInfo            

------------------------------------------------

RPC Event      0                          p_Book_content;1

从上面就可以看出是p_Book_content (是个存储过程)引起的阻塞,但是这个过程里面同时对多个表进行了操作,到底是那个语句出了问题呢?

下面我们再来进一步定位阻塞的位置:

Sp_lock

结果如下(大部分数据略)

Spid        dbid        objid              indid       type   resource   mode      status

-------------------------------------------------------------------------------------------------

51           14           206623779     0         TAB                    X            WAIT

52           14           0                   0            DB                       S            GRANT

53           14           0                   0            DB                       S            GRANT

。。。

。。。

。。。

现在我们来看看spid = 51 这行, mode = X 表示排它锁, status = WAIT表示正在等待(即被阻塞了),dbid = 14 是数据库的id,objid = 206623779 是被锁的对象id,我们可以通过下列函数得到数据库和表:

Select db_name(@dbid) -----》book_db

select object_name(@objid) -------》 t_book

即book_db库的t_book表被锁住了,这时候再回投仔细检查 p_Book_content 存储过程,发现只有一个语句对t_book进行了操作:

update t_book set hitcount = hitcount + 1 where bookid = @bookid

这个语句的作用是更新书本的点击次数,为什么上面这个语句会引起阻塞呢?我认为最可能的情况应该是同时访问的人过多,同时对表进行过多的update操作引起的,所以最终改用别的方式,不再实时对t_book表进行update操作,而是每次访问都先insert一条记录到一个中间表中,然后再用一个作业,每隔10分钟定时更新书本的点击次数,如此改进之后,此问题终于圆满解决了。

相关资料在book online可以找到, 关键字: sp_lock , sysprocesses , dbcc inputbuffer , db_name(), object_name()

 注:原来发在csdn的,现在荒芜了,挪到这边来栽培一下。

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

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
1 months ago By 尊渡假赌尊渡假赌尊渡假赌
Two Point Museum: All Exhibits And Where To Find Them
1 months ago By 尊渡假赌尊渡假赌尊渡假赌

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 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 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.

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.

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.

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.

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.

How to delete sqlserver if the installation fails? How to delete sqlserver if the installation fails? Apr 05, 2024 pm 11:27 PM

If the SQL Server installation fails, you can clean it up by following these steps: Uninstall SQL Server Delete registry keys Delete files and folders Restart the computer

How to change sqlserver English installation to Chinese How to change sqlserver English installation to Chinese Apr 05, 2024 pm 10:21 PM

SQL Server English installation can be changed to Chinese by following the following steps: download the corresponding language pack; stop the SQL Server service; install the language pack; change the instance language; change the user interface language; restart the application.

See all articles