Guest User

Untitled

a guest
May 24th, 2018
99
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.06 KB | None | 0 0
  1. SET NOCOUNT ON
  2.  
  3. IF OBJECT_ID('tempdb..##INDEXTEMP') IS NOT NULL begin exec sp_executesql N'DROP TABLE ##INDEXTEMP' end
  4.  
  5. EXEC sp_MSforeachtable
  6. @command1 = N'INSERT INTO ##INDEXTEMP ([name], [rows], [reserved], [data], [index_size], [unused]) exec sp_spaceused ''?'', ''true'';'
  7. , @precommand = N'CREATE TABLE ##INDEXTEMP ([id] int identity(1,1) not null,[name] sysname,[rows] int,[reserved] varchar(20),[data] varchar(20),[index_size] varchar(20),[unused] varchar(20))'
  8. , @postcommand = N'
  9. SELECT
  10. [name], [rows], [reserved], [data], [index_size], [unused]
  11. , convert(varchar, coalesce(
  12. cast( replace([index_size], '' KB'', '''') as bigint ) * 100 /
  13. nullif(cast( replace([data], '' KB'', '''') as bigint ), 0)
  14. , 0)) + ''%'' index_data_ratio
  15. , convert(varchar, coalesce(
  16. cast( replace([unused], '' KB'', '''') as bigint ) * 100 /
  17. nullif(cast( replace([reserved], '' KB'', '''') as bigint ), 0)
  18. , 0)) + ''%'' unused_used_ratio
  19. FROM ##INDEXTEMP
  20. ORDER BY 1;
  21. SELECT sum([rows]) [Total Rows], ''KB'' [Factor]
  22. , sum(cast(replace([reserved], '' KB'', '''') as decimal(20, 3))) [Reserved]
  23. , sum(cast(replace([data], '' KB'', '''') as decimal(20, 3))) [Data]
  24. , sum(cast(replace([index_size], '' KB'', '''') as decimal(20, 3))) [Index]
  25. , sum(cast(replace([unused], '' KB'', '''') as decimal(20, 3))) [Unused]
  26. FROM ##INDEXTEMP
  27. UNION ALL SELECT null, ''MB''
  28. , sum(cast(replace([reserved], '' KB'', '''') as decimal(20, 3))) / 1024.0
  29. , sum(cast(replace([data], '' KB'', '''') as decimal(20, 3))) / 1024.0
  30. , sum(cast(replace([index_size], '' KB'', '''') as decimal(20, 3))) / 1024.0
  31. , sum(cast(replace([unused], '' KB'', '''') as decimal(20, 3))) / 1024.0
  32. FROM ##INDEXTEMP
  33. UNION ALL SELECT null, ''GB''
  34. , sum(cast(replace([reserved], '' KB'', '''') as decimal(20, 3))) / 1024.0 / 1024.0
  35. , sum(cast(replace([data], '' KB'', '''') as decimal(20, 3))) / 1024.0 / 1024.0
  36. , sum(cast(replace([index_size], '' KB'', '''') as decimal(20, 3))) / 1024.0 / 1024.0
  37. , sum(cast(replace([unused], '' KB'', '''') as decimal(20, 3))) / 1024.0 / 1024.0
  38. FROM ##INDEXTEMP;
  39. DROP TABLE ##INDEXTEMP'
  40.  
  41. SET NOCOUNT OFF
Add Comment
Please, Sign In to add comment