2009-05-27 16 views
8

私は2008年のSQL Serverで約10のテーブルを持っています。データベーステーブルのサイズとサイズを調べる方法はありますか?

現在、私のMDFは約3.5Gigです。 (私はまたいくつかのテーブルにいくつかのバイナリデータを持っています)。だから、私はどのテーブルがサイズが最大であるか見ることができる方法があるのだろうかと思っています。

これは可能ですか?

代わりにインデックスまたはFTSカタログですか?

答えて

11

実行この:解析するコードを編集Robert Caineブログ

から

/****************************************************************************** 
** File: “GetTableSpaceUseage.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 ; 

takenは、単一引用符にあったいくつかの文字は、同様に、特殊な単一引用符を使用 - サイン。

このコードは、Microsoft SQL 2005+

+2

聖なるロケット船バットマン! –

+0

このスクリプトはMicrosoft Sql Server用ですか?私はそれを解析するときにエラーのスコアを取得します。 –

+0

(ホーリーロケット船バットマン! - ジョシュアベルデン) - 地獄はい! – kevchadders

5
exec sp_spaceused <tablename> 
+1

をパラメータなしでこれを使用して、データベースのサイズ、インデックスのサイズを取得する、など すなわちで与えられている場合。 exec sp_spaceused 'sometable'です。テーブルの行数、データサイズ、インデックスサイズを指定します – pirho

+0

RESERVEDとDATAの違いは何ですか? –

+0

reservedは、テーブル用に予約されたディスク容量です。データは、表内のデータによって使用される合計スペース量です。 – pirho

6
exec sp_spaceused [tablename] 
+0

大括弧でうまくいっています。それは決して失敗しません、私は山括弧を使用し、インデントを忘れると、それらは消えます。 grrr。インターネットはいつ私が何をしようとしているのかを知るだろうか? –

+0

角括弧と角かっこの違いは何ですか(上記のJoshの回答を参照)。 –

+1

角カッコについてはわかりませんが、角かっこは "面白い"テーブル名(予約語、テーブル名のスペースなど)を有効にします – ChrisHDog

2

ためである時々私はそれは、一時テーブルにすべてのテーブルを取得し、それを通じループし、すべてのテーブルのサイズを取得します...これを実行します。結果データは@tablesizesになっていますので、好きなようにクエリできます。 SQLサーバー> 2005年

declare @tables TABLE 
(
    table_name nvarchar(200) 
) 

declare @tablesizes TABLE 
(
    [name] nvarchar(200), 
    [rows] int, 
    reserved nvarchar(200), 
    data nvarchar(200), 
    index_size nvarchar(200), 
    unused nvarchar(200), 
    reserved_int int, 
    data_int int, 
    index_size_int int, 
    unused_int int 
) 

declare @t nvarchar(200) 

insert into @tables 
select Table_Name from information_schema.tables 

while exists(select * from @tables) 
begin 
    set @t=(select top 1 table_name from @tables) 

    insert into @tablesizes([name],[rows],reserved,data,index_size,unused) 
    exec sp_spaceused @t 

    delete top (1) from @tables 
end 

update @tablesizes set 
reserved_int=convert(int, replace(reserved,' KB','')), 
data_int=convert(int, replace(data,' KB','')), 
index_size_int=convert(int, replace(index_size,' KB','')), 
unused_int=convert(int, replace(unused,' KB','')) 

select * from @tablesizes order by data_int desc 
6

sys.allocations_units

作品は、あなたが必要とする情報を持っています。 sys.partitionsに参加して、パーティションのすべてのアロケーションユニットをグループ化し、難解なallocation_unit_idではなく、より使用可能なobject_idを取得します。

select object_name(p.object_id), 
    sum(au.total_pages)*8 as [space_in_kb] 
    from sys.partitions p 
    join sys.allocation_units au on p.hobt_id = au.container_id 
    group by p.object_id 
    order by [space_in_kb] desc 

そして、はい、すべてのテーブル(ヒープまたはクラスタ化された)は、「partitions」であり、用語は、パーティション表を参照していません。 sys.partitionsには、あなたの興味をそそる可能性のある「行」列もあります。

+0

これは、単一の要約のための素晴らしいスクリプトです:) –

3

このクエリは、現在のデータベース内の各テーブルのサイズを示します。

2008+ SQLで
SELECT sysobjects.[name] AS [TableName], 
    SUM(sysindexes.reserved) * 8 AS [Size(KB)], 
    SUM(sysindexes.dpages) * 8 AS [Data(KB)], 
    (SUM(sysindexes.used) - SUM(sysindexes.dpages)) * 8 AS [Indexes(KB)], 
    (SUM(sysindexes.reserved) - SUM(sysindexes.dpages)) * 8 AS [Unused(KB)] 
FROM dbo.sysindexes AS sysindexes 
    JOIN dbo.sysobjects AS sysobjects ON sysobjects.id = sysindexes.id 
WHERE sysobjects.[type] = 'U' 
GROUP BY sysobjects.[name] 
ORDER BY [Size(KB)] DESC 
3

:SSMSでDB名を右クリックし、その後、ReportsStandard ReportsDisk Usage by Table選択します。

あなたが使用することができ
0

SELECT @@servername; 

IF EXISTS(SELECT name FROM tempdb.sys.tables WHERE name LIKE '#spaceUsed%') 
    BEGIN 
     DROP TABLE #spaceUsed; 
    END; 
CREATE TABLE #spaceUsed (
name VARCHAR(255) , 
rows INT , 
reserved VARCHAR(50) , 
data VARCHAR(50) , 
index_size VARCHAR(50) , 
unused VARCHAR(50)); 

EXEC sp_msforeachtable 
@command1 =' 
-- 
INSERT INTO #spaceUsed 
exec sp_spaceused N''?''; 
' 
,@whereand = ' And Object_id In (Select Object_id From sys.objects 
Where SCHEMA_NAME(Schema_ID) like ''%'')'; 

DECLARE 
    @spaceUsedData TABLE (
    name VARCHAR(255) , 
    rows INT , 
    reservedMB BIGINT NULL , 
    dataMB BIGINT NULL , 
    index_sizeMB BIGINT NULL , 
    unusedMB BIGINT NULL); 

INSERT INTO INTO @spaceUsedData (name , rows , reservedMB , dataMB ,index_sizeMB ,unusedMB) 
SELECT     name , rows , 
Convert (BIGINT ,Ltrim(Rtrim(Replace(reserved ,'KB' ,''))))/1024 , 
Convert (BIGINT ,Ltrim(Rtrim(Replace(data ,'KB' ,''))))/1024 , 
Convert (BIGINT ,Ltrim(Rtrim(Replace(index_size ,'KB' ,''))))/1024 , 
Convert (BIGINT ,Ltrim(Rtrim(Replace(unused ,'KB' ,''))))/1024 
FROM #spaceUsed; 

SELECT * , reservedMB+ dataMB+index_sizeMB+unusedMB AS TotalMB FROM @spaceUsedData 
ORDER BY rows DESC; 
関連する問題