简体   繁体   中英

How to size table by number of rows in SQL Server

I have a table where I want to get size of the number records based on byte In SQL Server:

在此处输入图片说明

The only answer related to this question that I can suggest is this:

SELECT
    s.name AS SchemaName,
    t.name AS TableName,
    p.rows AS RowCounts,
    a.total_pages * 8 AS TotalSpaceKB,
    a.used_pages * 8 AS UsedSpaceKB,
    a.total_pages * 8 / p.rows AS eachRowTotalSpaceKB,
    a.used_pages * 8 / p.rows AS eachRowUsedSpaceKB,
    a.total_pages * 8 / p.rows * (select * from dbo.PictureTable where SchoolCode=1001) AS queryTotalSpaceKB,
    a.used_pages * 8 / p.rows * (select * from dbo.PictureTable where SchoolCode=1001) AS queryTotalSpaceKB
FROM 
    sys.tables t
INNER JOIN      
    sys.indexes i ON t.OBJECT_ID = i.object_id
INNER JOIN 
    sys.partitions p ON i.object_id = p.OBJECT_ID AND i.index_id = p.index_id
INNER JOIN 
    sys.allocation_units a ON p.partition_id = a.container_id
LEFT OUTER JOIN 
    sys.schemas s ON t.schema_id = s.schema_id
WHERE 
    s.name = 'dbo' AND
    t.name = 'PictureTable';

Edit
You can get length of your data in a field by using DATALENGTH like this:

SELECT SUM(DATALEGTH(PictureTable_ID)) + SUM(DATALEGTH(Groupcourses_ID)) +SUM(DATALEGTH(per_ID)) + SUM(DATALEGTH(School_ID)) + SUM(DATALEGTH(AcademicYear_ID)) + SUM(DATALEGTH(PictureTable_Description)) + SUM(DATALEGTH(SchoolCode)) As rowSizes
FROM PictureTable
WHERE (SchoolCode = 1001);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM