Table and Index size in SQL Server

前端 未结 9 1181
-上瘾入骨i
-上瘾入骨i 2020-12-04 05:41

Can we have a SQL query which will basically help in viewing table and index sizes in SQl Server.

How SQL server maintains memory usage for tables/indexes?

相关标签:
9条回答
  • 2020-12-04 05:53
    EXEC sp_MSforeachtable @command1="EXEC sp_spaceused '?'"
    
    0 讨论(0)
  • 2020-12-04 05:54

    Here is more compact version of the most successful answer:

    create table #tbl(
      name nvarchar(128),
      rows varchar(50),
      reserved varchar(50),
      data varchar(50),
      index_size varchar(50),
      unused varchar(50)
    )
    
    exec sp_msforeachtable 'insert into #tbl exec sp_spaceused [?]'
    
    select * from #tbl
        order by convert(int, substring(data, 1, len(data)-3)) desc
    
    drop table #tbl
    
    0 讨论(0)
  • 2020-12-04 05:56

    On SQL 2012 getting this information on a table level has become deliciously simple:

    SQL Management Studio -> Right click on Db -> Reports -> Standard Reports -> Disk usage by table !

    Enjoy

    0 讨论(0)
  • 2020-12-04 06:00
    --Gets the size of each index for the specified table
    DECLARE @TableName sysname = N'SomeTable';
    
    SELECT i.name AS IndexName
          ,8 * SUM(s.used_page_count) AS IndexSizeKB
    FROM sys.indexes AS i
        INNER JOIN sys.dm_db_partition_stats AS s 
            ON i.[object_id] = s.[object_id] AND i.index_id = s.index_id
    WHERE s.[object_id] = OBJECT_ID(@TableName, N'U')
    GROUP BY i.name
    ORDER BY i.name;
    
    SELECT i.name AS IndexName
          ,8 * SUM(a.used_pages) AS IndexSizeKB
    FROM sys.indexes AS i
        INNER JOIN sys.partitions AS p 
            ON i.[object_id]  = p.[object_id] AND i.index_id = p.index_id
        INNER JOIN sys.allocation_units AS a 
            ON p.partition_id = a.container_id
    WHERE i.[object_id] = OBJECT_ID(@TableName, N'U')
    GROUP BY i.name
    ORDER BY i.name;
    
    0 讨论(0)
  • 2020-12-04 06:05

    The exec sp_spaceused without parameter shows the summary for the whole database. The foreachtable solution generates one result set per table - which SSMS might not be able to handle if you have too many tables.

    I created a script which collects the table infos via sp_spaceused and displays a summary in a single record set, sorted by size.

    create table #t
    (
      name nvarchar(128),
      rows varchar(50),
      reserved varchar(50),
      data varchar(50),
      index_size varchar(50),
      unused varchar(50)
    )
    
    declare @id nvarchar(128)
    declare c cursor for
    select '[' + sc.name + '].[' + s.name + ']' FROM sysobjects s INNER JOIN sys.schemas sc ON s.uid = sc.schema_id where s.xtype='U'
    
    open c
    fetch c into @id
    
    while @@fetch_status = 0 begin
    
      insert into #t
      exec sp_spaceused @id
    
      fetch c into @id
    end
    
    close c
    deallocate c
    
    select * from #t
    order by convert(int, substring(data, 1, len(data)-3)) desc
    
    drop table #t
    
    0 讨论(0)
  • 2020-12-04 06:05

    sp_spaceused gives you the size of all the indexes combined.

    If you want the size of each index for a table, use one of these two queries:

    SELECT
        i.name                  AS IndexName,
        SUM(s.used_page_count) * 8   AS IndexSizeKB
    FROM sys.dm_db_partition_stats  AS s 
    JOIN sys.indexes                AS i
    ON s.[object_id] = i.[object_id] AND s.index_id = i.index_id
    WHERE s.[object_id] = object_id('dbo.TableName')
    GROUP BY i.name
    ORDER BY i.name
    
    SELECT
        i.name              AS IndexName,
        SUM(page_count * 8) AS IndexSizeKB
    FROM sys.dm_db_index_physical_stats(
        db_id(), object_id('dbo.TableName'), NULL, NULL, 'DETAILED') AS s
    JOIN sys.indexes AS i
    ON s.[object_id] = i.[object_id] AND s.index_id = i.index_id
    GROUP BY i.name
    ORDER BY i.name
    

    The results are usually slightly different but within 1%.

    0 讨论(0)
提交回复
热议问题