Table and Index size in SQL Server

前端 未结 9 1206
-上瘾入骨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 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
    

提交回复
热议问题