如何通过SQL命令查看数据库的文件大小[转]

时间:2022-03-19 22:14:02

1. 查看数据文件占用(权限要求较大)

DBCC showfilestats  

2. 查看日志文件占用

dbcc sqlperf(logspace)   

会列出所有能够查看的数据库的日志情况.. 需要挑选出当前数据库..

 

3.全面查看

SELECT a.name [文件名称]  
    ,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)]  
    ,a.physical_name AS [文件所在目录]  
    ,a.type_desc 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]  

 

亲测可用..