監控數據庫運行情況(三)

[sql] view plaincopyprint?
--1.查看數據庫版本信息   
select @@version  
--2.查看所有數據庫名稱及大小   
select 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 sysprocesses where blocked>0 ) a   
    where not exists(select * from (select * from sysprocesses where blocked>0 ) b   
    where a.blocked=spid)  
    union select spid,blocked from 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  
  
  
--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)  
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章