SQL Serverデータベースのサイズを確認するにはどうすればよいですか?


29

基本:MS SQL Server DBのディスク上のサイズは?
詳細:データの場所をすばやく確認できますか?すなわち、どのテーブル、ログなど


SQLのバージョン
SQLChicken

これはプログラム上の質問です。StackOverflowに表示されます!読み取り:stackoverflow.com/questions/914182
balexandre 09年

1
同意しません。基本的にはシステム管理者の質問です。サーバーがディスク領域を使い果たした場合、プログラマーが気にするのはなぜですか?
ニックカバディアス09年

2
ニックに同意します。どちらかまたは両方の質問です。DBAの質問は間違いなくここに属します。
squillman

それは両方です。DBが使用可能なスペースを使い果たした場合、プログラマーは例外の処理を気にします。システム管理者は明らかな理由に気を配っています。:)
JYelton

回答:


35

おそらくsp_spaceusedコマンドから始めたいと思うでしょう。

例えば:

sp_spaceused データベースの合計サイズに関する情報を返します

sp_spaceused 'MyTable' MyTableの サイズに関する情報を返します

情報を取得できるすべてのものについては、ドキュメントを参照してください。sp_msforeachtableコマンドを使用して、すべてのテーブルに対して一度にsp_spaceusedを実行することもできます。

編集:コマンドが複数のデータセットを返すことがあり、各セットに異なる統計のチャンクが含まれることがあることに注意してください。


メモを追加するために、sp_spaceusedはデータベースファイルが占有する8KBページの数を返します。
ダリオソレラ

3
データベースが2000の場合、DBCC UPDATEUSAGEを実行して、ここで正しい数値を取得する必要がある場合があります。2005年は常に正しいはずです
ニックカバディアス09年

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 \ Enterprise Managerを実行します。データベースシートを開き、プロパティ%databasename%で、場所のデータファイルとトランザクションファイルを確認できます。


または、SQL Server 2005、2008などの場合は、SQL Management Studioを開き、データベースを右クリックしてプロパティを選択し、左側のタブの2番目の項目であるファイルをクリックします。ただし、これは全体のファイルサイズのみを返します。これは、データとログファイルが保存されているフォルダーを見るだけで確認できます。
デイビッド

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テーブルからファイル名を取得することもできます。

弊社のサイトを使用することにより、あなたは弊社のクッキーポリシーおよびプライバシーポリシーを読み、理解したものとみなされます。
Licensed under cc by-sa 3.0 with attribution required.