有没有办法找出数据库表的大小/大小?

Pur*_*ome 8 sql-server size

我的sql 2008服务器中有大约10个表.

目前,我的mdf大约是3.5Gig.(我在一些表中也有一些二进制数据).所以,我想知道是否有一种方法可以看出哪些表的尺寸最大.

这可能吗?

也许它是索引或FTS目录而不是?

bal*_*dre 11

运行这个:

/******************************************************************************
**    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 ;
Run Code Online (Sandbox Code Playgroud)

采取罗伯特·凯恩博客

编辑了解析代码,单引号中的几个字符使用了特殊的单引号,以及 - 符号.

此代码适用于Microsoft SQL 2005+


Rem*_*anu 6

sys.allocations_units包含您需要的信息.您与sys.partitions一起将分区的所有分配单元组合在一起,并且还获得更可用的object_id而不是深奥的allocation_unit_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
Run Code Online (Sandbox Code Playgroud)

是的,所有表(堆或群集)都是' partitions',这些术语不是指分区表.sys.partitions还有你可能感兴趣的'rows'列.