SQL Server 2014 Express上的可用空间

时间:2013-11-02 14:48:33

标签: sql sql-server

由于Express版本限制为10 GB(无法弄清楚它是'每个数据库'还是作为一个整体,因为文档令人困惑,至少对我而言),我想知道有没有办法检查这个空间可用性,到目前为止我用了多少?我使用了互联网上的一些建议,但不知道我的所有数据库都显示出来了。

2 个答案:

答案 0 :(得分:4)

10 GB用于数据文件大小。您可以像这样获得每个文件的使用百分比:

;WITH x AS 
(
  SELECT name, [file] = physical_name, size = size * 8, 
    su_bytes = FILEPROPERTY(name, 'SpaceUsed') * 8
  FROM sys.database_files
)
SELECT name, [file], size, SpaceUsed = su_bytes,
  [SpaceUsed%] = CONVERT(DECIMAL(5,2), su_bytes*100.0/size)
FROM x;

manual on sys.database_files所示,size列保持以8K页表示的大小,因此将该值乘以8会得到以千字节为单位的大小。同样适用于FILEPROPERTY(..., 'SpaceUsed')的结果。要获得其他单位的金额,请相应地更改计算。

答案 1 :(得分:0)

你是对的......做了一些谷歌搜索并找到了更好的解决方案:

SELECT name AS 'File Name' , physical_name AS 'Physical Name', size/128 AS 'Total Size in MB',

size/128.0 - CAST(FILEPROPERTY(name, 'SpaceUsed') AS int)/128.0 AS 'Available Space In MB', *

FROM sys.database_files;

更好:

SELECT DB_NAME(A.DATABASE_ID) AS DBNAME,A.NAME AS FILENAME ,

SIZE/128.0 AS CURRENTSIZE_MB,

B.RECOVERY_MODEL_DESC,A.TYPE_DESC ,

CASE WHEN IS_PERCENT_GROWTH = 0

THEN LTRIM(STR(A.GROWTH * 8.0 / 1024,10,1)) + ' MB, '

ELSE 'BY ' + CAST(A.GROWTH AS VARCHAR) + ' PERCENT, 'END +

CASE WHEN MAX_SIZE = -1 THEN 'UNRESTRICTED GROWTH'

ELSE 'RESTRICTED GROWTH TO ' +LTRIM(STR(MAX_SIZE * 8.0 / 1024,10,1)) + ' MB'

END AS AUTOGROW,A.PHYSICAL_NAME

FROM SYS.MASTER_FILES A JOIN SYS.DATABASES B

ON A.DATABASE_ID =B.DATABASE_ID

--WHERE A.PHYSICAL_NAME LIKE 'E%'

-- AND A.FILE_ID =2

ORDER BY A.SIZE/128.0 DESC