The size of the ms SQL Server case that makes it easy to obtain the data table

Source: Internet
Author: User
-- The size of the ms SQL Server case that makes it easy to obtain the data table
-- The following method can only be used for estimation, but it can roughly solve some ways for the resource manager to check the growth of the data.

Declare   @ Tblname   Varchar ( 50 ) -- Data Table name changes (user data table name changes)
If   Exists ( Select   *   From DBO. sysobjects Where ID =   Object_id (N ' [DBO]. [# tablestatics_temp] ' ) And   Objectproperty (ID, n ' Isusertable ' ) =   1 )
Drop   Table   [ DBO ] . [ # Tablestatics_temp ] -- Inventory data table
Create   Table # Tablestatics_temp ( -- Create a metadata table
[ Name ]   [ Varchar ] ( 50 ) Collate chinese_taiwan_stroke_ci_as Null , -- Data Table Name ‑
[ Rows ]   [ Varchar ] ( 50 ) Collate chinese_taiwan_stroke_ci_as Null , -- Current data columns in the data table
[ Reserved ]   [ Varchar ] ( 50 ) Collate chinese_taiwan_stroke_ci_as Null , -- The size of the Space configured by the object in the Resource
[ Data ]   [ Varchar ] ( 50 ) Collate chinese_taiwan_stroke_ci_as Null , -- Data Space size
[ Index_size ]   [ Varchar ] ( 50 ) Collate chinese_taiwan_stroke_ci_as Null , -- Size of the blank space used by the index
[ Unused ]   [ Varchar ] ( 50 ) Collate chinese_taiwan_stroke_ci_as Null ) -- The size of the space reserved for objects in the resource but not used
Declare Cur_tablestatics Cursor Forward_only For  
Select Name From Sysobjects Where Xtype = ' U '   Order   By Name -- Retrieve the user data table name
Open Cur_tablestatics
Fetch   Next   From Cur_tablestatics
Into   @ Tblname
While   @ Fetch_status   =   0
Begin
Insert # Tablestatics_temp
Exec Sp_spaceused @ Tblname , @ Updateusage   = N ' True ' -- Displays the related disk space information of the data table.
Fetch   Next   From Cur_tablestatics
Into   @ Tblname
End
Close Cur_tablestatics
Deallocate Cur_tablestatics
Select Name, rows,
Convert ( Varchar ( 18 ), Convert (Numeric ( 18 , 2 ), Substring (Reserved, 1 , Len (Reserved) - 2 )) / 1024 ) +   ' MB '   As Reserved,
Convert ( Varchar ( 18 ), Convert (Numeric ( 18 , 2 ), Substring (Data, 1 , Len (Data) - 2 )) / 1024 ) +   ' MB '   As Data,
Convert ( Varchar ( 18 ), Convert (Numeric ( 18 , 2 ), Substring (Index_size, 1 , Len (Index_size) - 2 )) / 1024 ) +   ' MB '   As Index_siz,
Convert ( Varchar ( 18 ), Convert (Numeric ( 18 , 2 ), Substring (Unused, 1 , Len (Unused) - 2 )) / 1024 ) +   ' MB '   As Unused
From # Tablestatics_temp Where Rows <>   ' 0 ' -- Query the data table with the ratio of data saved in the metadata table being different.
Drop   Table # Tablestatics_temp -- Remove from the inventory table

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.