U can find many versions of this kind of SP in the Internet.
It will return a rowset with table name, table data sizes, sizes of indexes etc.
All (except row count)) in Kilobytes.
The reason why I wrote another version of this SP is my version do not use sp_spaceused system procedure. It also free of loops and cursors.
Its fast, it can filter table names, and by default it do not show empty tables.
I use it to check table size in KB versus row count. It really helps when U have limited DB size.
IF object_id(N'dbo.TECH_MassSpaceUsed', N'P') IS NOT NULL DROP PROCEDURE dbo.TECH_MassSpaceUsed
GO 
CREATE PROCEDURE dbo.TECH_MassSpaceUsed
  @filter varchar(100) = '',
  @showNoEmpty bit = 1
AS
BEGIN
  SET NOCOUNT ON 
  CREATE TABLE #tt_sizes (obId int, resPage int, usedPage int, page int, rCount int)
  INSERT INTO #tt_sizes
  select  p.object_id, sum(a.total_pages), sum(a.used_pages),
           sum(CASE
                      When a.type <> 1 Then a.used_pages
                      When p.index_id <>
                      Else 0
                  END),
           sum(CASE
                      When (p.index_id < type =" 1)">
                      Else 0
                  END)
  FROM sys.partitions p
  INNER JOIN sys.objects o ON o.object_id = p.object_id
  INNER JOIN sys.allocation_units a ON p.partition_id = a.container_id
  WHERE o.type = 'U ' AND (ISNULL(@filter, '') = '' OR o.name LIKE @filter)
  GROUP BY p.object_id
  UPDATE s
  SET resPage = it.resPage,
      usedPage = it.usedPage
  FROM #tt_sizes s
  INNER JOIN (
    SELECT it.parent_id obId, sum(a.total_pages) resPage, sum(a.used_pages) usedPage
    FROM sys.internal_tables it 
    INNER JOIN sys.partitions p ON p.object_id = it.object_id
    INNER JOIN  sys.allocation_units a ON p.partition_id = a.container_id
    WHERE it.internal_type IN (202,204) 
    GROUP BY it.parent_id 
  ) it ON s.obId = it.obId
  SELECT table_name = object_name(obId), 
    rows = convert(char(11), rCount),
        reserved_in_kb = ltrim(str(resPage * 8192 / 1024.,15,0)),
        data_in_kb = ltrim(str(page * 8192 / 1024.,15,0) ),
        index_size_in_kb = ltrim(str((usedPage - page) * 8192 / 1024.,15,0) ),
        unused_in_kb = ltrim(str((resPage - usedPage) * 8192 / 1024.,15,0))
  FROM #tt_sizes
  WHERE @showNoEmpty = 0 OR resPage > 0
  ORDER BY resPage DESC
END 
go
This SP is rewriten version of sp_spaceused sys SP. It uses same queries but i do not filter data for only one table. Thats all.
 
No comments:
Post a Comment