这个例子是关于计算数据表中所有字段在一定的数据量下占用存储空间的情况,使用了sys.tables,SYSTYPES和SYSCOLUMNS几张系统表,意思就是遍历所有数据表,然后对表的所以字段的length进行sum,就可以了,方法很容易理解,没什么技术含量,但即起到了重要的效果。
DECLARE @tableName VARCHAR(50) DECLARE @totalRecord BIGINT SET @tableName = ‘user_info‘ SET @totalRecord = 1000000 SELECT @tableName + ‘表中有‘ + LTRIM(STR(@totalRecord)) + ‘条数据时,占用的空间为:‘ + LTRIM(STR(SUM(c.length) * @totalRecord / 1024.0 / 1024.0)) + ‘MB‘ FROM SYSTYPES T , SYSCOLUMNS C WHERE T.XTYPE = C.XTYPE AND C.ID = ( SELECT ID FROM SYSOBJECTS WHERE NAME = @tableName ) SELECT TT.NAME , DATA = @TOTALRECORD , SIZE = ( SELECT STR(SUM(C.LENGTH) * @TOTALRECORD / 1024.0 / 1024.0) + ‘MB‘ FROM SYSTYPES T , SYSCOLUMNS C WHERE T.XTYPE = C.XTYPE AND C.ID = TT.OBJECT_ID ) FROM SYS.TABLES AS TT ORDER BY TT.NAME
执行结果如下
怎么样,挺有意思吧!
时间: 2024-10-08 02:26:50