Vous êtes sur la page 1sur 10

========================================================================= -- A temp table is created to capture the results from the sp_spaceused -- system stored procedure.

sp_spaceused shows the table name, row count, -- and DB space values in 4 categories: ----* * * * reserved data index unused

-- The 4 DB space values from sp_spaceused are in the form "99999 KB" -- and represents KB of disk space for each of the 4 DB space categories. ---------The table name and row count values are loaded directly into the temp table in their desired final form (varchar and bigint), requiring no further transformation. The 4 DB space values must be loaded into the temp table in varchar format, due to the "KB" suffix on each value, and does not represent the desired final format or unit measure for these DB space values. A later step will transform these 4 character-based DB space values (in KB unit measures) into a preferred final form (numeric and in MB unit measures).

Drop Table #restorehistory; Select Top 1 restore_date As [DB Restore Date], user_name As [Restored By User] -* Into #restorehistory From msdb.dbo.restorehistory With (NoLock) Where (destination_database_name = DB_Name()) Order By restore_date Desc Select ServerProperty('ServerName') As 'DB Instance Name', DB_Name() As [DB Name], ( Select create_date From sys.databases Where (name = DB_Name()) ) As [Create Date], ( Select [DB Restore Date] From #restorehistory ) As [DB Restore Date], ( Select

[Restored By User] From #restorehistory ) As [Restored By User], GetDate() As [Report Date / Time] /* Select ServerProperty('ServerName') As 'DB Instance Name', DB_Name() As [DB Name], ( Select create_date From sys.databases Where (name = DB_Name()) ) As [Create Date], r.[DB Restore Date], r.[Restored By User], GetDate() As [Report Date / Time] From ( Select ' ' As abc ) a Cross Join ( Select Top 1 restore_date As [DB Restore Date], user_name As [Restored By User] -* From msdb.dbo.restorehistory With (NoLock) Where (destination_database_name = DB_Name()) Order By restore_date Desc ) r */ Drop Table #DBTableSpace Go Drop Table #DBTableSpaceRank Go Drop Table #DBTableSpaceRank2 Go Create Table #DBTableSpace ( name varchar(128), rows bigint, reserved varchar(20), data varchar(20), index_size varchar(20), unused varchar(20) ); ---A SQL statement sequence of Insert ... / Exec sp_spaceused is used to capture the DB space values for a given table in the DB into the temp table.

-----

The system stored procedure sp_MSforeachtable is used (instead of a SQL cursor and loop) to perform the Insert ... / Exec sp_spaceused statements over all tables in the DB, capturing those results into the temp table.

Exec sp_MSforeachtable 'Insert Into #DBTableSpace exec sp_spaceused [?]' ------After the sp_spaceused results are loaded into the temp table, 4 new columns are added to the temp table that will represent the DB space for the 4 categories in MB numeric values (instead of KB character values). The temp table definition had to initially exclude these new MB DB space columns to properly import the sp_spaceused data as provided (in KB and character format).

Alter Table #DBTableSpace Add ReservedMB Decimal(11, 2); Go Alter Table #DBTableSpace Add DataMB Decimal(11, 2); Go Alter Table #DBTableSpace Add IndexMB Decimal(11, 2); Go Alter Table #DBTableSpace Add UnusedMB Decimal(11, 2); Go Alter Table #DBTableSpace Add TotalMB Decimal(11, 2); Go Alter Table #DBTableSpace Add TableSpaceRank int; Go ----Once the new columns are added, an update query parses each DB space type column in KB / character format, strips off the "KB" suffix, converts the result to numeric form, and then converts the KB unit measure DB space value to a MB unit measure DB space value.

Update #DBTableSpace Set ReservedMB = Convert(float, Replace(reserved, ' KB', '')) / 1024, DataMB = Convert(float, Replace(data, ' KB', '')) / 1024, IndexMB = Convert(float, Replace(index_size, ' KB', '')) / 1024, UnusedMB = Convert(float, Replace(unused, ' KB', '')) / 1024 Update #DBTableSpace Set TotalMB = DataMB + IndexMB

/* -Add new columns for the tab datetime; datetime;

Alter Table #DBTableSpace Add TableCreationDate Alter Table #DBTableSpace Add TableUpdatedDate Update #DBTableSpace */

-- Display the collected table level DB space results for the desired DB, -- using the MB unit measure for DB space values, and sorted in descending -- order by table DB space used (largest tables shown first). -- Select * From #DBTableSpace Declare @GrandTotalMB Declare @GrandTotalRows decimal(18, 2) decimal(18, 0)

-Select @GrandTotalMB = Sum(DataMB + IndexMB) Select @GrandTotalMB = Sum(TotalMB) From #DBTableSpace Select @GrandTotalRows = Sum(rows) From #DBTableSpace Select name, TotalMB, Cast(Row_Number() Over (Order By TotalMB Desc, name) As Decimal(5, 0)) As TRank Into #DBTableSpaceRank From #DBTableSpace /* Select * From #DBTableSpaceRank Select tsr.TRank, tsr.name, Cast(tsr.TotalMB As decimal(7, 2)) As TotalMB, Cast(0 As decimal(7, 2)) As TotalMB2 Into #DBTableSpaceRank2 From #DBTableSpaceRank tsr

*/ Select tsr.TRank, tsr.name, Cast(tsr.TotalMB As decimal(18, 2)) As TotalMB, Cast(0 As decimal(18, 2)) As TotalMB2 Into #DBTableSpaceRank2 From #DBTableSpaceRank tsr -Where (1 = 0) Declare @RunningTotalMB Set @RunningTotalMB = 0 Update #DBTableSpaceRank2 Set @RunningTotalMB = TotalMB2 = @RunningTotalMB + TotalMB From #DBTableSpaceRank2 /* Declare Declare Declare Declare @TRank @TableName @TotalMB @RunningTotalMB int varchar(128) Decimal(18, 2) Decimal(18, 2) Decimal(18, 2)

Declare RunningTotalCursor Cursor For Select TRank, name, TotalMB From #DBTableSpaceRank Order By TRank Open RunningTotalCursor Fetch Next From RunningTotalCursor Into @TRank, @TableName, @TotalMB Set @RunningTotalMB = 0 While (@@FETCH_STATUS = 0) Begin Set @RunningTotalMB = @RunningTotalMB + @TotalMB Insert #DBTableSpaceRank2 Values (@TRank, @TableName, @TotalMB, @RunningTotalMB) Fetch Next From RunningTotalCursor Into @TRank, @TableName, @TotalMB End Close RunningTotalCursor

Deallocate RunningTotalCursor */ /* Select tsr.TRank, tsr.name, Cast((tsr.TotalMB) As decimal(7, 2)) As TotalMB, Sum(tsr2.TotalMB) As TotalMB2 Into #DBTableSpaceRank2 From #DBTableSpaceRank tsr Inner Join #DBTableSpaceRank tsr2 On (tsr2.TRank <= tsr.TRank) Group By tsr.TRank, tsr.name, tsr.TotalMB Order By tsr.TRank, tsr.name, tsr.TotalMB */ /* Select * From #DBTableSpaceRank2 */

Select '' As TRank, '' As TableType, Count(*) As '# Tables', Sum(Case When ts.rows = 0 Then 1 Else 0 End) As '# Empty Tables', Cast((((Sum(Case When ts.rows = 0 Then 1 Else 0 End) * 100.0)) / Count(*)) As decimal(5, 2)) As '% Empty', Cast(Sum(ts.rows) As decimal(11, 0)) As '# Rows', Cast(100.00 As decimal(5, 2)) As '% Rows', Cast(100.00 As decimal(5, 2)) As '% MB', Cast(Sum(ts.TotalMB) As decimal(9, 2)) As 'TotalMB', Cast(Sum(ts.DataMB) As decimal(9, 2)) As 'DataMB', Cast(Sum(ts.IndexMB) As decimal(9, 2)) As 'IndexMB', Cast(Sum(ts.UnusedMB) As decimal(9, 2)) As 'UnusedMB', Cast(Sum(ts.ReservedMB) As decimal(9, 2)) As 'ReservedMB', Case When Sum(ts.ReservedMB) = 0 Then Cast(0 As decimal(5, 2)) Else Cast((((Sum(ts.DataMB)) * 100) / Sum(ts.ReservedMB)) As decimal(5, 2)) End As '% Data',

Case When Sum(ts.ReservedMB) = 0 Then Cast(0 As decimal(5, 2)) Else Cast((((Sum(ts.IndexMB)) * 100) / Sum(ts.ReservedMB)) As decimal(5, 2)) End As '% Index', Case When Sum(ts.ReservedMB) = 0 Then Cast(0 As decimal(5, 2)) Else Cast((((Sum(ts.UnusedMB)) * 100) / Sum(ts.ReservedMB)) As decimal(5, 2)) End As '% Unused', Case Sum(ts.rows) When 0 Then Null Else Cast(((Sum(ts.TotalMB) * 1048576) / Sum(ts.rows)) As int) End As 'Average Bytes/Row' From #DBTableSpace ts Select '' As TRank, -ObjectPropertyEx(Object_ID(ts.name), 'TableHasClustIndex') As TableType1, TableType, -Case IsNull(ObjectPropertyEx(Object_ID(ts.name), 'TableHasClustIndex'), 0) -When 1 Then 'Clustered' -Else 'Heap' End As TableType, Count(*) As '# Tables', Sum(Case When ts.rows = 0 Then 1 Else 0 End) As '# Empty Tables', Cast((((Sum(Case When ts.rows = 0 Then 1 Else 0 End) * 100.0)) / Count(*)) As decimal(5, 2)) As '% Empty', Cast(Sum(ts.rows) As decimal(11, 0)) As '# Rows', Case When (@GrandTotalRows = 0) Then Cast(0.00 As decimal(5, 2)) Else Cast(((Sum(ts.rows) * 100) / @GrandTotalRows) As decimal(5, 2)) End As '% Rows', Case When (@GrandTotalMB = 0.00) Then Cast(0.00 As decimal(5, 2)) Else Cast(((Sum(ts.TotalMB) * 100) / @GrandTotalMB) As decimal(5, 2)) End As '% MB', Cast(Sum(ts.TotalMB) As decimal(9, 2)) As 'TotalMB', Cast(Sum(ts.DataMB) As decimal(9, 2)) As 'DataMB', Cast(Sum(ts.IndexMB) As decimal(9, 2)) As 'IndexMB', Cast(Sum(ts.UnusedMB) As decimal(9, 2)) As 'UnusedMB', Cast(Sum(ts.ReservedMB) As decimal(9, 2)) As 'ReservedMB', Case When Sum(ts.ReservedMB) = 0 Then Cast(0 As decimal(5, 2)) Else Cast((((Sum(ts.DataMB)) * 100) / Sum(ts.ReservedMB)) As decimal(5, 2)) End As '% Data', Case When Sum(ts.ReservedMB) = 0 Then Cast(0 As decimal(5, 2)) Else Cast((((Sum(ts.IndexMB)) * 100) / Sum(ts.ReservedMB)) As decimal(5, 2)) End As '% Index', Case When Sum(ts.ReservedMB) = 0 Then Cast(0 As decimal(5, 2)) Else Cast((((Sum(ts.UnusedMB)) * 100) / Sum(ts.ReservedMB)) As decimal(5, 2)) End As '% Unused', Case Sum(ts.rows)

When 0 Then Null Else Cast(((Sum(ts.TotalMB) * 1048576) / Sum(ts.rows)) As int) End As 'Average Bytes/Row' From ( Select Case IsNull(ObjectPropertyEx(Object_ID(name), 'TableHasClustIndex'), 0) When 1 Then 'Clustered' Else 'Heap' End As TableType, * From #DBTableSpace ) ts Group By TableType Order By TableType -Group By IsNull(ObjectPropertyEx(Object_ID(ts.name), 'TableHasClustIndex'), 0) -Order By IsNull(ObjectPropertyEx(Object_ID(ts.name), 'TableHasClustIndex'), 0) Desc

Select -Cast(Row_Number() Over (Order By TotalMB Desc) As Decimal(5, 0)) As TRank, tsr2.TRank, -ObjectPropertyEx(Object_ID(ts.name), 'TableHasClustIndex') As TableType1, Case ObjectPropertyEx(Object_ID(ts.name), 'TableHasClustIndex') When 1 Then 'Clustered' Else 'Heap' End As TableType, Cast(ts.name As varchar(80)) As 'Table Name', -Cast(ts.name As varchar(40)) As 'Table Name', Cast(ts.rows As decimal(9, 0)) As '# Rows', Case When (@GrandTotalRows = 0) Then Cast(0.00 As decimal(5, 2)) Else Cast(((ts.rows * 100) / @GrandTotalRows) As decimal(5, 2)) End As '% Rows', Case When (@GrandTotalMB = 0.00) Then Cast(0.00 As decimal(5, 2)) Else Cast(((ts.TotalMB * 100) / @GrandTotalMB) As decimal(5, 2)) End As '% MB', Case When (@GrandTotalMB = 0.00) Then Cast(0.00 As decimal(5, 2)) Else Cast(((tsr2.TotalMB2 * 100) / @GrandTotalMB) As decimal(5, 2)) End As '% Cum', Cast((ts.TotalMB) As decimal(7, 2)) As 'TotalMB', Cast(ts.DataMB As decimal(7, 2)) As 'DataMB',

Cast(ts.IndexMB As decimal(7, 2)) As 'IndexMB', Cast(ts.UnusedMB As decimal(7, 2)) As 'UnusedMB', Cast(ts.ReservedMB As decimal(7, 2)) As 'ReservedMB', Case When ts.ReservedMB = 0 Then Cast(0 As decimal(5, 2)) Else Cast((((ts.DataMB) * 100) / ts.ReservedMB) As decimal(5, 2)) End As '% Data', Case When ts.ReservedMB = 0 Then Cast(0 As decimal(5, 2)) Else Cast((((ts.IndexMB) * 100) / ts.ReservedMB) As decimal(5, 2)) End As '% Index', Case When ts.ReservedMB = 0 Then Cast(0 As decimal(5, 2)) Else Cast((((ts.UnusedMB) * 100) / ts.ReservedMB) As decimal(5, 2)) End As '% Unused', Case ts.rows When 0 Then Null Else Cast(((ts.TotalMB * 1048576) / ts.rows) As int) End As 'Average Bytes/Row', scic.CIKeyColumns, scic.CIKeySize, Convert(varchar(16), so.crdate, 120) As 'Table Created' From #DBTableSpace ts Join sysobjects so On (so.name Collate Database_Default = ts.name) Inner Join #DBTableSpaceRank2 tsr2 On (tsr2.name = ts.name) Left Outer Join ( Select Object_Name(sic.object_id) As TableName, Count(*) As CIKeyColumns, Sum(sc.length) As CIKeySize From sys.index_columns sic Join syscolumns sc On (sc.colid = sic.index_column_id) And (sc.id = sic.object_id) Where (sic.index_id = 1) And (sic.object_id > 200) Group By Object_Name(sic.object_id) ) scic On (scic.TableName Collate Database_Default = ts.name) Order By tsr2.TRank -Order By (ts.TotalMB) Desc /* Select Cast(Row_Number() Over (Order By (DataMB + IndexMB) Desc) As Decimal(5, 0)) As 'Rank#', Case ObjectPropertyEx(Object_ID(ts.name), 'TableHasClustIndex') When 1 Then 'Clustered' Else 'Heap' End As TableType,

Cast(ts.name As varchar(40)) As TableName, Convert(varchar(16), so.crdate, 120) As 'Table Created', Cast(ts.rows As decimal(9, 0)) As '# Rows', Cast((((ts.DataMB + ts.IndexMB) * 100) / @GrandTotalMB) As decimal(5, 2)) As 'MB %', Cast((ts.DataMB + ts.IndexMB) As decimal(7, 2)) As 'TotalMB', Cast(ts.DataMB As decimal(7, 2)) As 'DataMB', Cast(ts.IndexMB As decimal(7, 2)) As 'IndexMB', Cast(ts.UnusedMB As decimal(7, 2)) As 'UnusedMB', Cast(ts.ReservedMB As decimal(7, 2)) As 'ReservedMB', Case ts.rows When 0 Then Null Else Cast((((ts.DataMB + ts.IndexMB) * 1048576) / ts.rows) As int) End As 'Average Bytes/Row' From #DBTableSpace ts Join sysobjects so On (so.name = ts.name) Order By (ts.DataMB + ts.IndexMB) Desc */ /* Select * From sysobjects Where xtype = 'U' */

Vous aimerez peut-être aussi