如何确定SQL Server数据库的大小?


29

基本:我的MS SQL Server DB的磁盘大小是多少?
更多:我可以快速查看数据在哪里吗?即哪些表,日志等


什么版本的SQL?
SQLChicken

这是一个程序性问题,显示在StackOverflow中!阅读:stackoverflow.com/questions/914182
balexandre,

1
我不同意。从根本上说,这是一个sysadmin问题。程序员为什么要关心服务器是否磁盘空间不足?
尼克·卡瓦迪亚斯

2
我同意尼克。这是一个非此即彼的问题。DBA问题肯定属于这里。
squillman

两者都是:程序员关心如果数据库用完了可用空间,则处理异常。系统管理员关心的原因很明显。:)
JYelton 2011年

Answers:


35

您可能需要从sp_spaceused命令开始。

例如:

sp_spaceused 返回有关数据库总大小的信息

sp_spaceused'MyTable' 返回有关MyTable大小的信息

阅读文档以获取您可以获取的所有信息。您还可以使用sp_msforeachtable命令对所有表一次运行sp_spaceused。

编辑:请注意,该命令有时会返回多个数据集,每个数据集包含不同的统计信息块。


仅添加一条注释,sp_spaceused返回数据库文件占用的8KB页面数。
达里奥·索雷拉

3
如果您的数据库是2000,则可能需要运行DBCC UPDATEUSAGE在此处获取正确的数字。在2005
尼克·卡瓦迪亚斯

16

最简单的方法(无需输入!):在Management Studio的SQL 2005/8中,右键单击数据库,选择“报告”,“标准报告”,“磁盘使用情况”(也按“顶层表”,“表”和“分区”)。



1

您可以在中查看物理文件sys.database_files。它具有文件的路径和大小(以IIRC块为单位)。

sp_spaceused 将显示单个对象占用了多少空间。


1

运行此命令以获取每个表的大小:

/******************************************************************************
**    File: “GetTableSpaceUsage.sql”
**    Name: Get Table Space Useage for a specific schema
**    Auth: Robert C. Cain
**    Date: 01/27/2008
**
**    Desc: Calls the sp_spaceused proc for each table in a schema and returns
**        the Table Name, Number of Rows, and space used for each table.
**
**    Called by:
**     n/a – As needed
**
**    Input Parameters:
**     In the code check the value of @schemaname, if you need it for a
**     schema other than dbo be sure to change it.
**
**    Output Parameters:
**     NA
*******************************************************************************/

/*—————————————————————————*/
/* Drop the temp table if it's there from a previous run                     */
/*—————————————————————————*/
if object_id(N'tempdb..[#TableSizes]') is not null
  drop table #TableSizes ;
go

/*—————————————————————————*/
/* Create the temp table                                                     */
/*—————————————————————————*/
create table #TableSizes
  (
    [Table Name] nvarchar(128)   /* Name of the table */
  , [Number of Rows] char(11)    /* Number of rows existing in the table. */
  , [Reserved Space] varchar(18) /* Reserved space for table. */
  , [Data Space] varchar(18)    /* Amount of space used by data in table. */
  , [Index Size] varchar(18)    /* Amount of space used by indexes in table. */
  , [Unused Space] varchar(18)   /* Amount of space reserved but not used. */
  ) ;
go

/*—————————————————————————*/
/* Load the temp table                                                        */
/*—————————————————————————*/
declare @schemaname varchar(256) ;
-- Make sure to set next line to the Schema name you want!
set @schemaname = 'dbo' ;

-- Create a cursor to cycle through the names of each table in the schema
declare curSchemaTable cursor
  for select sys.schemas.name + '.' + sys.objects.name
      from    sys.objects
            , sys.schemas
      where   object_id > 100
              and sys.schemas.name = @schemaname
              /* For a specific table uncomment next line and supply name */
              --and sys.objects.name = 'specific-table-name-here'    
              and type_desc = 'USER_TABLE'
              and sys.objects.schema_id = sys.schemas.schema_id ;

open curSchemaTable ;
declare @name varchar(256) ;  /* This holds the name of the current table*/

-- Now loop thru the cursor, calling the sp_spaceused for each table
fetch curSchemaTable into @name ;
while ( @@FETCH_STATUS = 0 )
  begin    
    insert into #TableSizes
            exec sp_spaceused @objname = @name ;       
    fetch curSchemaTable into @name ;   
  end

/* Important to both close and deallocate! */
close curSchemaTable ;     
deallocate curSchemaTable ;


/*—————————————————————————*/
/* Feed the results back                                                     */
/*—————————————————————————*/
select [Table Name]
      , [Number of Rows]
      , [Reserved Space]
      , [Data Space]
      , [Index Size]
      , [Unused Space]
from    [#TableSizes]
order by [Table Name] ;

/*—————————————————————————*/
/* Remove the temp table                                                     */
/*—————————————————————————*/
drop table #TableSizes ;

采取罗伯特·凯恩博客

此代码适用于Microsoft SQL 2005+


0

运行开始\程序\ Microsoft SQL Server \企业管理器。打开数据库表,在属性%databasename%中,您可以看到位置数据文件和事务文件。


或者,如果是SQL Server 2005、2008等,请打开SQL Management Studio,右键单击数据库,选择属性,然后单击左侧选项卡上的第二项,即文件。但是,这只会返回整个文件的大小-您只需查看存储数据和日志文件的文件夹即可看到。
大卫,

0

这是一个查询/视图,可获取所有这些信息以及更多信息,而没有任何“邪恶的”游标或循环。;-)

    /*
    vwTableInfo - Table Information View

 This view display space and storage information for every table in a
SQL Server 2005 database.
Columns are:
    Schema
    Name
    Owner       may be different from Schema)
    Columns     count of the max number of columns ever used)
    HasClusIdx  1 if table has a clustered index, 0 otherwise
    RowCount
    IndexKB     space used by the table's indexes
    DataKB      space used by the table's data

 16-March-2008, RBarryYoung@gmail.com
 31-January-2009, Edited for better formatting
*/
--CREATE VIEW vwTableInfo
-- AS

    SELECT SCHEMA_NAME(tbl.schema_id) as [Schema]
    , tbl.Name
    , Coalesce((Select pr.name 
            From sys.database_principals pr 
            Where pr.principal_id = tbl.principal_id)
        , SCHEMA_NAME(tbl.schema_id)) as [Owner]
    , tbl.max_column_id_used as [Columns]
    , CAST(CASE idx.index_id WHEN 1 THEN 1 ELSE 0 END AS bit) AS [HasClusIdx]
    , Coalesce( (Select sum (spart.rows) from sys.partitions spart 
        Where spart.object_id = tbl.object_id and spart.index_id < 2), 0) AS [RowCount]

    , Coalesce( (Select Cast(v.low/1024.0 as float) 
        * SUM(a.used_pages - CASE WHEN a.type <> 1 THEN a.used_pages WHEN p.index_id < 2 THEN a.data_pages ELSE 0 END) 
            FROM sys.indexes as i
             JOIN sys.partitions as p ON p.object_id = i.object_id and p.index_id = i.index_id
             JOIN sys.allocation_units as a ON a.container_id = p.partition_id
            Where i.object_id = tbl.object_id  )
        , 0.0) AS [IndexKB]

    , Coalesce( (Select Cast(v.low/1024.0 as float)
        * SUM(CASE WHEN a.type <> 1 THEN a.used_pages WHEN p.index_id < 2 THEN a.data_pages ELSE 0 END) 
            FROM sys.indexes as i
             JOIN sys.partitions as p ON p.object_id = i.object_id and p.index_id = i.index_id
             JOIN sys.allocation_units as a ON a.container_id = p.partition_id
            Where i.object_id = tbl.object_id)
        , 0.0) AS [DataKB]
    , tbl.create_date, tbl.modify_date

     FROM sys.tables AS tbl
      INNER JOIN sys.indexes AS idx ON (idx.object_id = tbl.object_id and idx.index_id < 2)
      INNER JOIN master.dbo.spt_values v ON (v.number=1 and v.type='E')

请享用。


-1

关于如何从GUI进行的操作,有一些描述。

真正的DBA知道:GUI适用于块。

sp_helpdb

返回所有文件名,位置,磁盘空间和类型的记录集。

您还可以从每个数据库的sysfiles表中检索文件名。

By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.