對(duì)于DBA來(lái)說(shuō),監(jiān)控磁盤使用情況是必要的工作,然后沒(méi)有比較簡(jiǎn)單的方法能獲取到磁盤空間使用率信息,下面總結(jié)下這些年攢下的腳本:
最常用的查看磁盤剩余空間,這個(gè)屬于DBA入門必記的東西:
-- 查看磁盤可用空間EXEC master.dbo.xp_fixeddrives
xp_fixeddrives方式有點(diǎn)是系統(tǒng)自帶,可直接使用,缺點(diǎn)是不能查看磁盤總大小和不能查看SQL Server未使用到的磁盤信息
==============================================================
使用sys.dm_os_volume_stats函數(shù)
--======================================================================--查看數(shù)據(jù)庫(kù)文件使用的磁盤空間使用情況WITH T1 AS (SELECT DISTINCTREPLACE(vs.volume_mount_point,':\','') AS Drive_Name ,CAST(vs.total_bytes / 1024.0 / 1024 / 1024 AS NUMERIC(18,2)) AS Total_Space_GB ,CAST(vs.available_bytes / 1024.0 / 1024 / 1024 AS NUMERIC(18,2)) AS Free_Space_GBFROM sys.master_files AS fCROSS APPLY sys.dm_os_volume_stats(f.database_id, f.file_id) AS vs )SELECTDrive_Name, Total_Space_GB, Total_Space_GB-Free_Space_GB AS Used_Space_GB, Free_Space_GB,CAST(Free_Space_GB*100/Total_Space_GB AS NUMERIC(18,2)) AS Free_Space_PercentFROM T1