SQL Server 数据库巡检脚本
生活随笔
收集整理的這篇文章主要介紹了
SQL Server 数据库巡检脚本
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
--1.查看數據庫版本信息
select @@version
--2.查看所有數據庫名稱及大小
exec sp_helpdb
--3.查看數據庫所在機器的操作系統參數
exec master..xp_msver
--4.查看數據庫啟動的參數
exec sp_configure
--5.查看數據庫啟動時間
select convert(varchar(30),login_time,120)
from master..sysprocesses where spid=1
--6.查看數據庫服務器名
select 'Server Name:'+ltrim(@@servername)
--7.查看數據庫實例名
select 'Instance:'+ltrim(@@servicename)
--8.數據庫的磁盤空間呢使用信息
exec sp_spaceused
--9.日志文件大小及使用情況
dbcc sqlperf(logspace)
--10.表的磁盤空間使用信息
exec sp_spaceused 'tablename'
--11.獲取磁盤讀寫情況
select
@@total_read [讀取磁盤次數],
@@total_write [寫入磁盤次數],
@@total_errors [磁盤寫入錯誤數],
getdate() [當前時間]
--12.獲取I/O工作情況
select @@io_busy,
@@timeticks [每個時鐘周期對應的微秒數],
@@io_busy*@@timeticks [I/O操作毫秒數],
getdate() [當前時間]
--13.查看CPU活動及工作情況
select
@@cpu_busy,
@@timeticks [每個時鐘周期對應的微秒數],
@@cpu_busy*cast(@@timeticks as float)/1000 [CPU工作時間(秒)],
@@idle*cast(@@timeticks as float)/1000 [CPU空閑時間(秒)],
getdate() [當前時間]
--14.檢查鎖與等待
exec sp_lock
--15.檢查死鎖
exec sp_who_lock --自己寫個存儲過程即可
/*
create procedure sp_who_lock ?
as ?
begin ?declare @spid int,@bl int, ?@intTransactionCountOnEntry int, ?@intRowcount int, ?@intCountProperties int, ?@intCounter int ?create table #tmp_lock_who (id int identity(1,1),spid smallint,bl smallint) ?IF @@ERROR<>0 RETURN @@ERROR ?insert into #tmp_lock_who(spid,bl) select 0 ,blocked ?from (select * from sys.sysprocesses where blocked>0 ) a ??where not exists(select * from (select * from sys.sysprocesses where blocked>0 ) b ??where a.blocked=spid) ?union select spid,blocked from sys.sysprocesses where blocked>0 ?IF @@ERROR<>0 RETURN @@ERROR ?-- 找到臨時表的記錄數 ?select @intCountProperties = Count(*),@intCounter = 1 ?from #tmp_lock_who ?IF @@ERROR<>0 RETURN @@ERROR ?if @intCountProperties=0 ?select '現在沒有阻塞和死鎖信息' as message ?-- 循環開始 ?while @intCounter <= @intCountProperties ?begin ?-- 取第一條記錄 ?select @spid = spid,@bl = bl ?from #tmp_lock_who where id = @intCounter ??begin ?if @spid =0 ??select '引起數據庫死鎖的是: '+ CAST(@bl AS VARCHAR(10)) + '進程號,其執行的SQL語法如下' ?else ?select '進程號SPID:'+ CAST(@spid AS VARCHAR(10))+ '被' + '進程號SPID:'+ CAST(@bl AS VARCHAR(10)) +'阻塞,其當前進程執行的SQL語法如下' ?DBCC INPUTBUFFER (@bl ) ?end ?-- 循環指針下移 ?set @intCounter = @intCounter + 1 ?end ?drop table #tmp_lock_who ?return 0 ?
end ??
*/--16.用戶和進程信息
exec sp_who
exec sp_who2--17.活動用戶和進程的信息
exec sp_who 'active'--18.查看進程中正在執行的SQL
dbcc inputbuffer(進程號)
exec sp_who3
/*
CREATE PROCEDURE sp_who3 ( @SessionID INT = NULL )
AS?BEGINSELECT ?SPID = er.session_id ,Status = ses.status ,[Login] = ses.login_name ,Host = ses.host_name ,BlkBy = er.blocking_session_id ,DBName = DB_NAME(er.database_id) ,CommandType = er.command ,SQLStatement = st.text ,ObjectName = OBJECT_NAME(st.objectid) ,ElapsedMS = er.total_elapsed_time ,CPUTime = er.cpu_time ,IOReads = er.logical_reads + er.reads ,IOWrites = er.writes ,LastWaitType = er.last_wait_type ,StartTime = er.start_time ,Protocol = con.net_transport ,ConnectionWrites = con.num_writes ,ConnectionReads = con.num_reads ,ClientAddress = con.client_net_address ,Authentication = con.auth_schemeFROM ? ?sys.dm_exec_requests erOUTER APPLY sys.dm_exec_sql_text(er.sql_handle) stLEFT JOIN sys.dm_exec_sessions ses ON ses.session_id = er.session_idLEFT JOIN sys.dm_exec_connections con ON con.session_id = ses.session_idWHERE ? er.session_id > 50AND @SessionID IS NULLOR er.session_id = @SessionIDORDER BY er.blocking_session_id DESC ,er.session_id?END
*/--19.查看所有數據庫用戶登錄信息
exec sp_helplogins --20.查看所有數據庫用戶所屬的角色信息
exec sp_helpsrvrolemember--21.查看鏈接服務器
exec sp_helplinkedsrvlogin--22.查看遠端數據庫用戶登錄信息
exec sp_helpremotelogin--23.獲取網絡數據包統計信息
select
@@pack_received [輸入數據包數量],
@@pack_sent [輸出數據包數量],
@@packet_errors [錯誤包數量],
getdate() [當前時間]--24.檢查數據庫中的所有對象的分配和機構完整性是否存在錯誤
dbcc checkdb--25.查詢文件組和文件
select df.[name],df.physical_name,df.[size],df.growth, f.[name][filegroup],f.is_default
from sys.database_files df join sys.filegroups f
on df.data_space_id = f.data_space_id --26.查看數據庫中所有表的條數
select b.name as tablename , a.rowcnt as datacount
from sysindexes a , sysobjects b
where a.id = b.id and a.indid < 2 and objectproperty(b.id, 'IsMSShipped') = 0 --27.得到最耗時的前10條T-SQL語句
;with maco as
( select top 10 plan_handle, sum(total_worker_time) as total_worker_time , sum(execution_count) as execution_count , count(1) as sql_count from sys.dm_exec_query_stats group by plan_handle order by sum(total_worker_time) desc
)
select t.text , a.total_worker_time , a.execution_count , a.sql_count
from maco a cross apply sys.dm_exec_sql_text(plan_handle) t --28. 查看SQL Server的實際內存占用
select * from sysperfinfo where counter_name like '%Memory%'--29.顯示所有數據庫的日志空間信息
dbcc sqlperf(logspace)--30.收縮數據庫
dbcc shrinkdatabase(databaseName)
?
總結
以上是生活随笔為你收集整理的SQL Server 数据库巡检脚本的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: NeHe OpenGL教程 第三十六课:
- 下一篇: centos 安装部署 zabbix3