search
HomeDatabaseMysql Tutorial 5. SQL Server数据库性能监控

对于在线运行的系统,当前数据库性能监控,通常监视以下几点:(1)是否有阻塞(Blocking);(2)是否有等待(Waiting),阻塞就是锁(Lock)等待;(3)是否运行时间过长(Lon

对于在线运行的系统,当前数据库性能监控,通常监视以下几点:

(1) 是否有阻塞 (Blocking);

(2) 是否有等待 (Waiting),阻塞就是锁 (Lock) 等待;

(3) 是否运行时间过长(Long running);

(4) 是否有死锁 (Deadlock);

sys.dm_exec_query_stats之类,等一些统计性的信息,通常不作为实时告警内容,而是在性能优化时,作为参考。

 

一. 阻塞/等待/长时间运行

1. SQL Server 2005 及以后版本检查

SELECT r.session_id       ,r.blocking_session_id       ,DB_Name(r.database_id) as database_name       ,r.start_time       ,r.total_elapsed_time       ,r.[status]       ,CASE WHEN r.blocking_session_id 0 THEN'Blocking'            WHEN r.blocking_session_id= 0 AND r.wait_type is not null THEN 'Waiting'            ELSE 'Long-running'        END as slowness_type       ,r.percent_complete       ,r.command       ,r.wait_type       ,r.wait_time       ,r.wait_resource       ,r.last_wait_type       ,r.cpu_time       ,r.reads       ,r.writes       ,r.logical_reads       ,t.[text] as executing_batch       ,SUBSTRING(t.[text],                 r.statement_start_offset/2,                 (CASE WHENr.statement_end_offset =-1                        THENDATALENGTH(t.[text]) --LEN(CONVERT(NVARCHAR(MAX),t.text)) * 2                ELSE r.statement_end_offset                END - r.statement_start_offset )/2+ 1) as executing_sql       ,bt.[text] as blocking_batch       ,SUBSTRING(bt.[text],                 br.statement_start_offset/2,                 (CASE WHENbr.statement_end_offset = -1                        THENDATALENGTH(bt.[text]) --LEN(CONVERT(NVARCHAR(MAX),bt.text)) * 2                ELSE br.statement_end_offset                END - br.statement_start_offset )/2+ 1) as blocking_sql      --,p.query_plan   FROM sys.dm_exec_requests r  CROSS APPLY sys.dm_exec_sql_text(r.sql_handle) as t  CROSS APPLY sys.dm_exec_query_plan(r.plan_handle) as p   LEFT JOIN sys.dm_exec_requests br     ON r.blocking_session_id =br.session_id  OUTER APPLY sys.dm_exec_sql_text(br.session_id) as bt  WHERE r.session_id > 50 and r.session_id  @@SPID    AND r.total_elapsed_time >30 * 60 * 1000  ORDER BY r.total_elapsed_timeDESC;

以上脚本返回运行超过30分钟的语句,需要注意的是:

(1) 如果返回执行计划,会让以上脚本变慢很多,可以不返回,在收到告警后检查语句时,再去查看执行计划;

(2) 显示TEXT,比如: xp_cmdshell这样的语句,start_offset, end_offset都为0,截取的 text是空白,只有看TEXT才知道是什么语句;还有就是有时需要知道这个请求来自哪个batch或者存储过程;

(3) 有时显示TEXT还不够,还以xp_cmdshell为例,需要dbcc inputbuffer才能看到完整的sql语句;另外已运行结束但还没有commit/rollback的事务,在requests中已经没有了,也需要借用dbcc inputbuffer来查看sql 语句;

dbcc inputbuffer(@@SPID)

(4) SQL Agent作业,在这里会被一并检查,也可以通过msdb..sysjobactivity另行检查;

select b.name, *    from msdb..sysjobactivity a  inner join msdb.dbo.sysjobs b     on a.job_id = b.job_id  where b.name like '%backup%'

 

Statement
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
What Are the Limitations of Using Views in MySQL?What Are the Limitations of Using Views in MySQL?May 14, 2025 am 12:10 AM

MySQLviewshavelimitations:1)Theydon'tsupportallSQLoperations,restrictingdatamanipulationthroughviewswithjoinsorsubqueries.2)Theycanimpactperformance,especiallywithcomplexqueriesorlargedatasets.3)Viewsdon'tstoredata,potentiallyleadingtooutdatedinforma

Securing Your MySQL Database: Adding Users and Granting PrivilegesSecuring Your MySQL Database: Adding Users and Granting PrivilegesMay 14, 2025 am 12:09 AM

ProperusermanagementinMySQLiscrucialforenhancingsecurityandensuringefficientdatabaseoperation.1)UseCREATEUSERtoaddusers,specifyingconnectionsourcewith@'localhost'or@'%'.2)GrantspecificprivilegeswithGRANT,usingleastprivilegeprincipletominimizerisks.3)

What Factors Influence the Number of Triggers I Can Use in MySQL?What Factors Influence the Number of Triggers I Can Use in MySQL?May 14, 2025 am 12:08 AM

MySQLdoesn'timposeahardlimitontriggers,butpracticalfactorsdeterminetheireffectiveuse:1)Serverconfigurationimpactstriggermanagement;2)Complextriggersincreasesystemload;3)Largertablesslowtriggerperformance;4)Highconcurrencycancausetriggercontention;5)M

MySQL: Is it safe to store BLOB?MySQL: Is it safe to store BLOB?May 14, 2025 am 12:07 AM

Yes,it'ssafetostoreBLOBdatainMySQL,butconsiderthesefactors:1)StorageSpace:BLOBscanconsumesignificantspace,potentiallyincreasingcostsandslowingperformance.2)Performance:LargerrowsizesduetoBLOBsmayslowdownqueries.3)BackupandRecovery:Theseprocessescanbe

MySQL: Adding a user through a PHP web interfaceMySQL: Adding a user through a PHP web interfaceMay 14, 2025 am 12:04 AM

Adding MySQL users through the PHP web interface can use MySQLi extensions. The steps are as follows: 1. Connect to the MySQL database and use the MySQLi extension. 2. Create a user, use the CREATEUSER statement, and use the PASSWORD() function to encrypt the password. 3. Prevent SQL injection and use the mysqli_real_escape_string() function to process user input. 4. Assign permissions to new users and use the GRANT statement.

MySQL: BLOB and other no-sql storage, what are the differences?MySQL: BLOB and other no-sql storage, what are the differences?May 13, 2025 am 12:14 AM

MySQL'sBLOBissuitableforstoringbinarydatawithinarelationaldatabase,whileNoSQLoptionslikeMongoDB,Redis,andCassandraofferflexible,scalablesolutionsforunstructureddata.BLOBissimplerbutcanslowdownperformancewithlargedata;NoSQLprovidesbetterscalabilityand

MySQL Add User: Syntax, Options, and Security Best PracticesMySQL Add User: Syntax, Options, and Security Best PracticesMay 13, 2025 am 12:12 AM

ToaddauserinMySQL,use:CREATEUSER'username'@'host'IDENTIFIEDBY'password';Here'showtodoitsecurely:1)Choosethehostcarefullytocontrolaccess.2)SetresourcelimitswithoptionslikeMAX_QUERIES_PER_HOUR.3)Usestrong,uniquepasswords.4)EnforceSSL/TLSconnectionswith

MySQL: How to avoid String Data Types common mistakes?MySQL: How to avoid String Data Types common mistakes?May 13, 2025 am 12:09 AM

ToavoidcommonmistakeswithstringdatatypesinMySQL,understandstringtypenuances,choosetherighttype,andmanageencodingandcollationsettingseffectively.1)UseCHARforfixed-lengthstrings,VARCHARforvariable-length,andTEXT/BLOBforlargerdata.2)Setcorrectcharacters

See all articles

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 Article

Hot Tools

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Linux new version

SublimeText3 Linux new version

SublimeText3 Linux latest version

MantisBT

MantisBT

Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

Integrate Eclipse with SAP NetWeaver application server.