Know the side can not be enough ~ wrote a calculation of the data table to occupy the storage space method

Source: Internet
Author: User

Back to Catalog

This example is about calculating the amount of storage space used by all the fields in a data table under a certain volume of data. Using the Sys.tables,systypes and syscolumns a few system tables, meaning is to traverse all the data table, and then the table so that the length of the field to sum, it is easy to understand the method, there is no technical content, but it played an important effect.

DECLARE @tableName VARCHAR( -)DECLARE @totalRecord BIGINTSET @tableName = 'User_info'SET @totalRecord = 1000000SELECT  @tableName + 'the table has' + LTRIM(STR(@totalRecord))+ 'data, the space occupied is:'        + LTRIM(STR(SUM(c.length)* @totalRecord / 1024.0 / 1024.0))+ 'MB' fromsystypes T, syscolumns CWHERET.xtype=C.xtype andC.id=(SELECTID fromSYSOBJECTSWHERENAME= @tableName                   ) SELECTTt.name, DATA= @TOTALRECORD, SIZE=(SELECT STR(SUM(c.length)* @TOTALRECORD / 1024.0 / 1024.0)                        + 'MB'                  fromsystypes T, syscolumns CWHERET.xtype=C.xtype andC.id=Tt.object_id               ) fromSYS. TABLES asTTORDER  byTt.name

Execution results are as follows

What's the fun?

Back to Catalog

Know the side can not be enough ~ wrote a calculation of the data table to occupy the storage space method

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.