SQL Server 数据库巡检脚本

来源:互联网 发布:网络直播底线失守视频 编辑:程序博客网 时间:2024/05/21 09:42
  1. --1.查看数据库版本信息  
  2. select @@version  
  3. --2.查看所有数据库名称及大小  
  4. exec sp_helpdb  
  5. --3.查看数据库所在机器的操作系统参数  
  6. exec master..xp_msver  
  7. --4.查看数据库启动的参数  
  8. exec sp_configure  
  9. --5.查看数据库启动时间  
  10. select convert(varchar(30),login_time,120)  
  11. from master..sysprocesses where spid=1  
  12. --6.查看数据库服务器名  
  13. select 'Server Name:'+ltrim(@@servername)  
  14. --7.查看数据库实例名  
  15. select 'Instance:'+ltrim(@@servicename)   
  16. --8.数据库的磁盘空间呢使用信息  
  17. exec sp_spaceused  
  18. --9.日志文件大小及使用情况  
  19. dbcc sqlperf(logspace)  
  20. --10.表的磁盘空间使用信息  
  21. exec sp_spaceused 'tablename'  
  22. --11.获取磁盘读写情况  
  23. select   
  24. @@total_read [读取磁盘次数],  
  25. @@total_write [写入磁盘次数],  
  26. @@total_errors [磁盘写入错误数],  
  27. getdate() [当前时间]  
  28. --12.获取I/O工作情况  
  29. select @@io_busy,  
  30. @@timeticks [每个时钟周期对应的微秒数],  
  31. @@io_busy*@@timeticks [I/O操作毫秒数],  
  32. getdate() [当前时间]  
  33. --13.查看CPU活动及工作情况  
  34. select  
  35. @@cpu_busy,  
  36. @@timeticks [每个时钟周期对应的微秒数],  
  37. @@cpu_busy*cast(@@timeticks as float)/1000 [CPU工作时间(秒)],  
  38. @@idle*cast(@@timeticks as float)/1000 [CPU空闲时间(秒)],  
  39. getdate() [当前时间]  
  40. --14.检查锁与等待  
  41. exec sp_lock  
  42. --15.检查死锁  
  43. exec sp_who_lock --自己写个存储过程即可  
  44. /*  
  45. create procedure sp_who_lock    
  46. as    
  47. begin    
  48.     declare @spid int,@bl int,    
  49.     @intTransactionCountOnEntry int,    
  50.     @intRowcount int,    
  51.     @intCountProperties int,    
  52.     @intCounter int    
  53.     create table #tmp_lock_who (id int identity(1,1),spid smallint,bl smallint)    
  54.     IF @@ERROR<>0 RETURN @@ERROR    
  55.     insert into #tmp_lock_who(spid,bl) select 0 ,blocked    
  56.     from (select * from sys.sysprocesses where blocked>0 ) a     
  57.     where not exists(select * from (select * from sys.sysprocesses where blocked>0 ) b     
  58.     where a.blocked=spid)    
  59.     union select spid,blocked from sys.sysprocesses where blocked>0    
  60.     IF @@ERROR<>0 RETURN @@ERROR    
  61.         -- 找到临时表的记录数    
  62.         select @intCountProperties = Count(*),@intCounter = 1    
  63.         from #tmp_lock_who    
  64.     IF @@ERROR<>0 RETURN @@ERROR    
  65.     if @intCountProperties=0    
  66.     select '现在没有阻塞和死锁信息' as message    
  67.     -- 循环开始    
  68.     while @intCounter <= @intCountProperties    
  69.     begin    
  70.     -- 取第一条记录    
  71.     select @spid = spid,@bl = bl    
  72.     from #tmp_lock_who where id = @intCounter     
  73.     begin    
  74.     if @spid =0     
  75.         select '引起数据库死锁的是: 'CAST(@bl AS VARCHAR(10)) + '进程号,其执行的SQL语法如下'    
  76.     else    
  77.         select '进程号SPID:'CAST(@spid AS VARCHAR(10))+ '被' + '进程号SPID:'CAST(@bl AS VARCHAR(10)) +'阻塞,其当前进程执行的SQL语法如下'    
  78.     DBCC INPUTBUFFER (@bl )    
  79.     end    
  80.     -- 循环指针下移    
  81.     set @intCounter = @intCounter + 1    
  82.     end    
  83.     drop table #tmp_lock_who    
  84.     return 0    
  85. end     
  86. */  
  87.   
  88. --16.用户和进程信息  
  89. exec sp_who  
  90. exec sp_who2  
  91.   
  92. --17.活动用户和进程的信息  
  93. exec sp_who 'active'  
  94.   
  95. --18.查看进程中正在执行的SQL  
  96. dbcc inputbuffer(进程号)  
  97. exec sp_who3  
  98. /*  
  99. CREATE PROCEDURE sp_who3 ( @SessionID INT = NULL )  
  100. AS   
  101.     BEGIN  
  102.   
  103.   
  104.         SELECT  SPID = er.session_id ,  
  105.                 Status = ses.status ,  
  106.                 [Login] = ses.login_name ,  
  107.                 Host = ses.host_name ,  
  108.                 BlkBy = er.blocking_session_id ,  
  109.                 DBName = DB_NAME(er.database_id) ,  
  110.                 CommandType = er.command ,  
  111.                 SQLStatement = st.text ,  
  112.                 ObjectName = OBJECT_NAME(st.objectid) ,  
  113.                 ElapsedMS = er.total_elapsed_time ,  
  114.                 CPUTime = er.cpu_time ,  
  115.                 IOReads = er.logical_reads + er.reads ,  
  116.                 IOWrites = er.writes ,  
  117.                 LastWaitType = er.last_wait_type ,  
  118.                 StartTime = er.start_time ,  
  119.                 Protocol = con.net_transport ,  
  120.                 ConnectionWrites = con.num_writes ,  
  121.                 ConnectionReads = con.num_reads ,  
  122.                 ClientAddress = con.client_net_address ,  
  123.                 Authentication = con.auth_scheme  
  124.         FROM    sys.dm_exec_requests er  
  125.                 OUTER APPLY sys.dm_exec_sql_text(er.sql_handle) st  
  126.                 LEFT JOIN sys.dm_exec_sessions ses ON ses.session_id = er.session_id  
  127.                 LEFT JOIN sys.dm_exec_connections con ON con.session_id = ses.session_id  
  128.         WHERE   er.session_id > 50  
  129.                 AND @SessionID IS NULL  
  130.                 OR er.session_id = @SessionID  
  131.         ORDER BY er.blocking_session_id DESC ,  
  132.                 er.session_id   
  133.   
  134.   
  135.     END  
  136. */  
  137.   
  138. --19.查看所有数据库用户登录信息  
  139. exec sp_helplogins   
  140.   
  141. --20.查看所有数据库用户所属的角色信息  
  142. exec sp_helpsrvrolemember  
  143.   
  144. --21.查看链接服务器  
  145. exec sp_helplinkedsrvlogin  
  146.   
  147. --22.查看远端数据库用户登录信息  
  148. exec sp_helpremotelogin  
  149.    
  150. --23.获取网络数据包统计信息  
  151. select   
  152. @@pack_received [输入数据包数量],  
  153. @@pack_sent [输出数据包数量],  
  154. @@packet_errors [错误包数量],  
  155. getdate() [当前时间]  
  156.   
  157. --24.检查数据库中的所有对象的分配和机构完整性是否存在错误  
  158. dbcc checkdb  
  159.   
  160. --25.查询文件组和文件  
  161. select   
  162.     df.[name],df.physical_name,df.[size],df.growth,   
  163.     f.[name][filegroup],f.is_default   
  164. from sys.database_files df join sys.filegroups f   
  165. on df.data_space_id = f.data_space_id   
  166.   
  167. --26.查看数据库中所有表的条数  
  168. select  b.name as tablename ,    
  169.         a.rowcnt as datacount    
  170. from    sysindexes a ,    
  171.         sysobjects b    
  172. where   a.id = b.id    
  173.         and a.indid < 2    
  174.         and objectproperty(b.id, 'IsMSShipped') = 0   
  175.   
  176. --27.得到最耗时的前10条T-SQL语句  
  177. ;with maco as     
  178. (       
  179.     select top 10    
  180.         plan_handle,    
  181.         sum(total_worker_time) as total_worker_time ,    
  182.         sum(execution_count) as execution_count ,    
  183.         count(1) as sql_count    
  184.     from sys.dm_exec_query_stats group by plan_handle    
  185.     order by sum(total_worker_time) desc    
  186. )    
  187. select  t.text ,    
  188.         a.total_worker_time ,    
  189.         a.execution_count ,    
  190.         a.sql_count    
  191. from    maco a    
  192.         cross apply sys.dm_exec_sql_text(plan_handle) t   
  193.   
  194. --28. 查看SQL Server的实际内存占用  
  195. select * from sysperfinfo where counter_name like '%Memory%'  
  196.   
  197.   
  198. --29.显示所有数据库的日志空间信息  
  199. dbcc sqlperf(logspace)  
  200.   
  201. --30.收缩数据库  
  202. dbcc shrinkdatabase(databaseName) 
0 0