【问题标题】:Is there any way to find out the size / sizes of your database tables?有没有办法找出你的数据库表的大小?
【发布时间】:2009-05-27 06:02:21
【问题描述】:

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

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

这可能吗?

也许它是一个索引或 FTS 目录?

【问题讨论】:

    标签: sql-server size


    【解决方案1】:

    运行这个:

    /******************************************************************************
    **    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 来自 Robert Caine 博客

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

    此代码适用于 Microsoft SQL 2005+

    【讨论】:

    • 此脚本是否适用于 Microsoft Sql Server?当我解析检查它时,我得到了一些错误。
    • 发现单引号和双破折号是特殊字体,现在全部完成,您需要的只是复制/粘贴并执行...查看代码中的 cmets 以更改架构/表跨度>
    • 我会奖励这个,因为它是冗长而不是太多信息的完美结合。干杯:)
    【解决方案2】:
    exec sp_spaceused [tablename]
    

    【讨论】:

    • 方括号很好。它永远不会失败,我使用尖括号,忘记缩进,它们就消失了。呸。互联网何时才能最终知道我想要做什么?
    • 方括号和尖括号之间的区别是什么(上面有 Josh 的回答)?
    • 我不确定尖括号,但方括号可以启用“有趣”的表名(保留字、表名中的空格等...)
    【解决方案3】:

    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
    

    是的,所有表(堆或集群)都是“partitions”,这些术语不指分区表。 sys.partitions 也有您可能感兴趣的“行”列。

    【讨论】:

    • 这是一个很棒的单一摘要脚本:)
    【解决方案4】:
     exec sp_spaceused <tablename>
    

    【讨论】:

    • 使用这个不带参数将得到你的数据库大小,索引大小等,如果用 ie 给出。 exec sp_spaceused 'sometable'。它将为您提供表的行数、数据大小和索引大小
    • 请问RESERVED和DATA有什么区别?
    • reserved 是为表保留的磁盘空间。 data 是表中数据使用的空间总量。
    【解决方案5】:

    此查询显示当前数据库中每个表的大小。

    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
    

    【讨论】:

      【解决方案6】:

      在 SQL 2008+ 中:右键单击 SSMS 中的 DB 名称,选择 Reports,然后选择 Standard Reports,然后选择 Disk Usage by Table

      【讨论】:

        【解决方案7】:

        有时我会运行这个...它将所有表获取到临时表,循环遍历并获取所有表的大小。结果数据在@tablesizes中,你可以随意查询。

        适用于 Sql Server >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
        

        【讨论】:

          【解决方案8】:

          你可以使用:

          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;
          

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 1970-01-01
            • 2020-12-26
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2016-09-06
            • 2021-07-29
            • 2022-06-28
            相关资源
            最近更新 更多