确定SQL Server数据库大小

时间:2008-10-06 21:59:57

标签: sql-server tsql

SQL Server 2005/2008 Express版每个数据库的限制为4 GB。据我所知,数据库引擎仅考虑数据,因此不包括日志文件,未使用的空间和索引大小。

获取MDF文件的长度不应在SQL Server限制方面提供正确的数据库大小。我的问题是如何获得数据库大小?

8 个答案:

答案 0 :(得分:100)

注释sp_spaceused

答案 1 :(得分:78)

sp_helpdb

不需要循环,与sp_spaceused不同。

答案 2 :(得分:16)

根据SQL2000的帮助,sp_spaceused包含数据和索引。

此脚本应该:

CREATE TABLE #t (name SYSNAME, rows CHAR(11), reserved VARCHAR(18), 
data VARCHAR(18), index_size VARCHAR(18), unused VARCHAR(18))

EXEC sp_msforeachtable 'INSERT INTO #t EXEC sp_spaceused ''?'''
-- SELECT * FROM #t ORDER BY name
-- SELECT name, CONVERT(INT, SUBSTRING(data, 1, LEN(data)-3)) FROM #t ORDER BY name
SELECT SUM(CONVERT(INT, SUBSTRING(data, 1, LEN(data)-3))) FROM #t
DROP TABLE #t

答案 3 :(得分:14)

在SQL Management Studio中,右键单击数据库,然后从上下文菜单中选择“属性”。看看“大小”数字。

答案 4 :(得分:8)

最佳解决方案是使用sys.sysfiles视图计算每个数据库文件的大小,考虑每页的大小为8 KB,如下所示:

USE [myDatabase]
GO

SELECT
    [size] * 8
    , [filename]
FROM sysfiles

[field]列表示文件的大小,以页(MSDN Reference to sysfiles)。

表示

您会看到至少会有两个文件(MDF和LDF):这些文件的总和将为您提供整个数据库的正确大小......

答案 5 :(得分:5)

我总是喜欢直接追求它:

SELECT 
    DB_NAME( dbid ) AS DatabaseName, 
    CAST( ( SUM( size ) * 8 ) / ( 1024.0 * 1024.0 ) AS decimal( 10, 2 ) ) AS DbSizeGb 
FROM 
    sys.sysaltfiles 
GROUP BY 
    DB_NAME( dbid )

答案 6 :(得分:2)

你也可以使用这个老式的......

set ANSI_NULLS ON
set QUOTED_IDENTIFIER ON
go

DECLARE @iCount int, @iMax int, @DatabaseName varchar(200), @SQL varchar (8000)

Select NAME, DBID, crdate, filename, version 
INTO #TEMP
from MAster..SYSDatabASES 

SELECT @iCount = Count(DBID) FROM #TEMP

Select @SQL='Create Table ##iFile1 ( DBName varchar( 200) NULL, Fileid INT, FileGroup int, TotalExtents INT , USedExtents INT , 
Name varchar(100), vFile varchar (300), AllocatedSpace int NUll, UsedSpace int Null, PercentageFree int Null ) '+ char(10)
exec (@SQL)


Create Table ##iTotals ( ServerName varchar(100), DBName varchar( 200) NULL, FileType varchar(10),Fileid INT, FileGroup int, TotalExtents INT , USedExtents INT , 
Name varchar(100), vFile varchar (300), AllocatedSpace int NUll, UsedSpace int Null, PercentageFree int Null ) 


WHILE @iCount>0
BEGIN    
    SELECT @iMax =Max(dbid) FROM #TEMP
    Select @DatabaseName = Name FROM #TEMP where dbid =@iMax

    SELECT @SQL = 'INSERT INTO ##iFile1(Fileid , FileGroup , TotalExtents  , USedExtents  , Name , vFile)
     EXEC (''USE [' + @DatabaseName +  '] DBCC showfilestats'')    ' + char(10)

    Print  (@SQL)
    EXEC (@SQL)


    SELECT @SQL = 'UPDATE ##iFile1 SET DBName ='''+ @DatabaseName +''' WHERE DBName IS NULL'
    EXEC  (@SQL)


    DELETE FROM #TEMP WHERE dbid =@iMax
    Select @iCount =@iCount -1
END
UPDATE ##iFile1
SET AllocatedSpace = (TotalExtents * 64.0 / 1024.0 ), UsedSpace =(USedExtents * 64.0 / 1024.0 )

UPDATE ##iFile1
SET PercentageFree = 100-Convert(float,UsedSpace)/Convert(float,AllocatedSpace   )* 100
WHERE USEDSPACE>0

CREATE TABLE #logspace (
   DBName varchar( 100),
   LogSize float,
   PrcntUsed float,
   status int
   )
INSERT INTO #logspace
EXEC ('DBCC sqlperf( logspace)')



INSERT INTO ##iTotals(ServerName, DBName, FileType,Name, vFile,PercentageFree,AllocatedSpace)
select @@ServerName ,DBNAME,  'Data' as FileType,Name, vFile, PercentageFree  , AllocatedSpace
from ##iFile1
UNION
select @@ServerName ,DBNAME, 'Log' as FileType ,DBName,'' as vFile ,PrcntUsed  , LogSize
from #logspace

Select * from ##iTotals

select ServerName ,DBNAME,  FileType, Sum( AllocatedSpace) as AllocatedSpaceMB
from ##iTotals
Group By  ServerName ,DBNAME, FileType
Order By  ServerName ,DBNAME, FileType


select ServerName ,DBNAME,  Sum( AllocatedSpace) as AllocatedSpaceMB
from ##iTotals
Group By  ServerName ,DBNAME
Order By  ServerName ,DBNAME



drop table ##iFile1
drop table #logspace
drop table #TEMP
drop table ##iTotals

答案 7 :(得分:0)

常见查询以检查同时支持Azure和本地的SQL Server中的数据库大小-

方法1 –使用“ sys.database_files”系统视图

SELECT
    DB_NAME() AS [database_name],
    CONCAT(CAST(SUM(
        CAST( (size * 8.0/1024) AS DECIMAL(15,2) )
    ) AS VARCHAR(20)),' MB') AS [database_size]
FROM sys.database_files;

方法2 –使用“ sp_spaceused”系统存储过程

EXEC sp_spaceused ;