如何根据SQL Server中的行数来调整表的大小

时间:2016-01-16 08:08:32

标签: sql sql-server database

我有一个表格,我希望根据字节获取数字记录的大小 在SQL Server中:

enter image description here

1 个答案:

答案 0 :(得分:0)

我建议的唯一与this question相关的答案是:

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';

修改
您可以使用DATALENGTH这样在字段中获取数据的长度:

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);