SQL Server資料庫巡檢

马永猛發表於2024-04-25

資料庫檔案資訊

SELECT a.name [檔名稱] 
,a.type_desc AS [檔案型別] 
    ,cast(a.[size]*1.0/128 as decimal(12,1)) AS [檔案設定大小(MB)] 
    ,CAST( fileproperty(s.name,'SpaceUsed')/(8*16.0) AS DECIMAL(12,1)) AS [檔案所佔空間(MB)] 
    ,CAST( (fileproperty(s.name,'SpaceUsed')/(8*16.0))/(s.size/(8*16.0))*100.0  AS DECIMAL(12,1)) AS [所佔空間率%] 
    ,CASE WHEN A.growth =0 THEN '檔案大小固定,不會增長' ELSE '檔案將自動增長' end [增長模式] 
    ,CASE WHEN A.growth > 0 AND is_percent_growth = 0 THEN '增量為固定大小' 
        WHEN A.growth > 0 AND is_percent_growth = 1 THEN '增量將用整數百分比表示' 
        ELSE '檔案大小固定,不會增長' END AS [增量模式] 
    ,CASE WHEN A.growth > 0 AND is_percent_growth = 0 THEN cast(cast(a.growth*1.0/128as decimal(12,0)) AS VARCHAR)+'MB' 
        WHEN A.growth > 0 AND is_percent_growth = 1 THEN cast(cast(a.growth AS decimal(12,0)) AS VARCHAR)+'%' 
        ELSE '檔案大小固定,不會增長' end AS [增長值(%或MB)] 
       , CASE WHEN a.max_size=-1 then -1 ELSE cast(a.[max_size]*1.0/128 as decimal(12,1))  end as [檔案增長最大大小(MB)]
    ,a.physical_name AS [檔案所在目錄] 
    
FROM sys.database_files  a 
INNER JOIN sys.sysfiles AS s ON a.[file_id]=s.fileid 
LEFT JOIN sys.dm_db_file_space_usage b ON a.[file_id]=b.[file_id]

資料庫記憶體情況

SELECT type,
    sum(virtual_memory_reserved_kb)/ 1024  as '保留記憶體',
    sum(virtual_memory_committed_kb)/ 1024  as '提交的記憶體', 
    sum(awe_allocated_kb)/ 1024  as '開啟AWE後使用的記憶體', 
    sum(shared_memory_reserved_kb)/ 1024  as '共享的保留記憶體' ,
    sum(shared_memory_committed_kb)/ 1024  as '共享的提交記憶體' 
FROM sys.dm_os_memory_clerks 
GROUP BY type
ORDER BY type
 
SELECT  total_physical_memory_kb / 1024 AS [實體記憶體(MB)] ,  
        available_physical_memory_kb / 1024 AS [可用實體記憶體(MB)] ,  
        system_cache_kb / 1024 AS [系統快取記憶體總量(MB)] ,  
        ( kernel_paged_pool_kb + kernel_nonpaged_pool_kb ) / 1024 AS [核心池記憶體總量(MB)] ,  
        total_page_file_kb / 1024 AS [作業系統報告的提交限制的大小(MB)] ,  
        available_page_file_kb / 1024 AS [未使用的頁檔案的總量(MB)] ,  
        system_memory_state_desc AS [記憶體狀態說明]  
FROM    sys.dm_os_sys_memory  

資料庫使用者

select  sp.name,sp.type,sp.is_disabled,sp.create_date,sp.modify_date,sp.default_database_name,sp.is_fixed_role,sl.is_policy_checked,sl.is_expiration_checked from sys.server_principals sp left join sys.sql_logins sl
on sp.sid=sl.sid

資料庫異常檢查

WAITFOR TIME '00:10:00' --使SQL Server會話暫停,直到指定的時間。
DBCC CHECKDB() --檢查資料庫的一致性並修復錯誤(如果可能)。

相關文章