19. 查看数据库各种设置
select name,State,user_access,is_read_only,recovery_model from sys.databases
20. 查看某个数据库中是否存在会话
select DB_NAME(dbid),* from sys.sysprocesses where dbid=db_id('dbname')
21. 查询当前阻塞的所有请求
select * from sys.sysprocesses where blocked>0或SELECT t1.resource_type,db_name(t1.resource_database_id),t1.resource_associated_entity_id,t1.request_mode,t1.request_session_id,t2.blocking_session_id,t2.wait_duration_msFROM sys.dm_tran_locks as t1INNER JOIN sys.dm_os_waiting_tasks as t2ON t1.lock_owner_address = t2.resource_address;或select A.SPID as 被阻塞进程,a.CMD AS 正在执行的操作,b.spid AS 阻塞进程号,b.cmd AS 阻塞进程正在执行的操作from master..sysprocesses a,master..sysprocesses bwhere a.blocked<>0 and a.blocked= b.spid或SELECT session_Id,spid,ecid,DB_NAME (sp.dbid),nt_username,er.status,wait_type,[Individual Query] =SUBSTRING (qt.text,er.statement_start_offset / 2,( CASEWHEN er.statement_end_offset = -1THENLEN (CONVERT (NVARCHAR (MAX), qt.text)) * 2ELSEer.statement_end_offsetEND- er.statement_start_offset)/ 2),qt.text,program_name,Hostname,nt_domain,start_timeFROM sys.dm_exec_requests erINNER JOIN sys.sysprocesses sp ON er.session_id = sp.spidCROSS AppLY sys.dm_exec_sql_text (er.sql_handle) AS qtWHERE session_Id > 50 /* Ignore system spids.*/AND sp.blocked>0 AND session_Id NOT IN (@@SPID)或SELECT session_id ,status ,blocking_session_id,wait_type ,wait_time ,wait_resource,transaction_idFROM sys.dm_exec_requestsWHERE status = N'suspended';--sys.dm_exec_requests返回SQL Server 中正在执行的每个请求的信息
22. 查看哪些表被锁了,以及这些表被哪个进程锁了
select request_session_id spid,OBJECT_NAME(resource_associated_entity_id) tableNamefrom sys.dm_tran_locks where resource_type='OBJECT' ORDER BY request_session_id ASC
23. 查询某个job是否被堵塞
select * from msdb.dbo.sysjobs where name='jobname'select a.program_name,a.* from master..sysprocesses a where a.program_name like '%0D1CE57E8AC5%'--把第一个语句查询到的job_id代入第二个语句的program_name
24. 检查SQL Agent是否开启
IF EXISTS (SELECT TOP 1 1FROM sys.sysprocessesWHERE program_name = 'SQLAgent - Generic Refresher')SELECT 'Running'ELSESELECT 'Not Running'
25. 查看活动线程执行的sql语句,并生成批量杀掉的语句
select 'KILL '+CAST(a.spid AS NVARCHAR(100)) AS KillCmd,REPLACE(hostname,' ','') as hostname ,replace(program_name,' ','') as program_name,REPLACE(loginame, ' ', '') AS loginame, db_name(a.dbid) AS DBname,spid,blocked,waittime/1000 as waittime,a.status,Replace(b.text,'''','''') as sqlmessage,cpufrom sys.sysprocesses as a with(nolock)cross apply sys.dm_exec_sql_text(sql_handle) as bwhere a.status<>'sleeping' AND a.spid<>@@SPID
26. 查看备份进度
SELECT DB_NAME(database_id) AS Exec_DB,percent_complete,CASE WHEN estimated_completion_time < 36000000THEN '0' ELSE '' END + RTRIM(estimated_completion_time/1000/3600)+ ':' + RIGHT('0' + RTRIM((estimated_completion_time/1000)%3600/60), 2)+ ':' + RIGHT('0' + RTRIM((estimated_completion_time/1000)%60), 2) AS [Time Remaining],b.text as tsql,*FROM SYS.DM_EXEC_REQUESTScross apply sys.dm_exec_sql_text(sql_handle) as bWHERE command LIKE 'Backup%' --and database_id=db_id('cardorder')--OR command LIKE 'RESTORE%'ORDER BY 2 DESC
27. 查看恢复进度
SELECT DB_NAME(database_id) AS Exec_DB,percent_complete,CASE WHEN estimated_completion_time < 36000000THEN '0' ELSE '' END + RTRIM(estimated_completion_time/1000/3600)+ ':' + RIGHT('0' + RTRIM((estimated_completion_time/1000)%3600/60), 2)+ ':' + RIGHT('0' + RTRIM((estimated_completion_time/1000)%60), 2) AS [Time Remaining],b.text as tsql,*FROM SYS.DM_EXEC_REQUESTScross apply sys.dm_exec_sql_text(sql_handle) as bWHERE command LIKE 'RESTORE%' --and database_id=db_id('cardorder')--OR command LIKE 'RESTORE%'ORDER BY 2 DESC
28. 查看数据库的最近备份信息
SELECT database_name,type,MAX(backup_finish_date) AS backup_finish_date FROM msdb.dbo.backupset GROUP BY database_name,type ORDER BY database_name,type备注:D 表示全备份,i 表示差异备份,L 表示日志备份
29. 查看数据库的历史备份记录,并生成restore语句
SELECTCONVERT(CHAR(100),SERVERPROPERTY('Servername'))AS Server,bs.database_name,bs.backup_start_date,bs.backup_finish_date,bs.expiration_date,CASE bs.typeWHEN 'D' THEN 'Database'WHEN 'L' THEN 'Log'END AS backup_type,bs.backup_size,bmf.logical_device_name,bmf.physical_device_name,bs.name AS backupset_name,bs.description,'RESTORE DATABASE ['+bs.database_name+'] FROM DISK=N'''+bmf.physical_device_name+ '''WITH NORECOVERY;'FROM msdb.dbo.backupmediafamily bmfINNER JOIN msdb.dbo.backupset bsON bmf.media_set_id=bs.media_set_idWHERE bs.backup_start_date>DATEADD(DAY,-1,GETDATE())ORDER BY bs.backup_finish_date
推荐阅读
- 关于交换机的一些常用术语你是否了解?
- Win7常用运行命令都有哪些你知道吗?
- MySQL运行机制
- MySQL5.7数据库主从架构部署,你再也不用去问度娘了
- MySQL高可用架构的演进
- CENTOS Mysql5.7数据库自动安装脚本
- 黑客的辛酸历程:使用sqlmap曲折渗透某服务器
- 我司服务器上几个常用的监控小工具,俺全瞟来了
- 神奇的 SQL 之性能优化 → 让 SQL 飞起来
- 做好mysql运维,必须熟练掌握备份和恢复,实战一次不行多来几次