关于查看MSSQL 数据库 用户每个表 占用的空间大小
前端之家收集整理的这篇文章主要介绍了
关于查看MSSQL 数据库 用户每个表 占用的空间大小,
前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
最近做项目需要查看数据用户表的大小,包括记录条数和占用的磁盘空间数目。在网上找了很久其中查看MSsql数据库每个表占用的空间大小 相对还可以。
不过它的2、3中方法返回的数据比较多,有些是我们不关心的数据,我在AdventureWorks2012数据上做的测试。其中第二种方法代码如下:
<div class="codetitle"><a style="CURSOR: pointer" data="19224" class="copybut" id="copybut19224" onclick="doCopy('code19224')"> 代码如下:
<div class="codebody" id="code19224">
View Code
if not exists (select
from dbo.sysobjects where id = object_id(N'[dbo].[tablespaceinfo]') and OBJECTPROPERTY(id,N'IsUserTable') = 1)
create table tablespaceinfo --创建结果存储表
(nameinfo varchar(50),
rowsinfo int,reserved varchar(20),
datainfo varchar(20),
index_size varchar(20),
unused varchar(20) )
delete from tablespaceinfo --清空数据表
declare @tablename varchar(255) --表名称
declare @cmdsql varchar(500)
DECLARE Info_cursor CURSOR FOR
select o.name
from dbo.sysobjects o where OBJECTPROPERTY(o.id,N'IsTable') = 1
and o.name not like N'#%%' order by o.name
OPEN Info_cursor
FETCH NEXT FROM Info_cursor
INTO @tablename
WHILE @@FETCH_STATUS = 0
BEGIN
if exists (select from dbo.sysobjects where id = object_id(@tablename) and OBJECTPROPERTY(id,N'IsUserTable') = 1)
execute sp_execute
sql N'insert into tablespaceinfo exec sp_spaceused @tbname',
N'@tbname varchar(255)',
@tbname = @tablename
FETCH NEXT FROM Info_cursor
INTO @tablename
END
CLOSE Info_cursor
DEALLOCATE Info_cursor
GO
--itlearner注:
显示数据库信息
sp_spaceused @updateusage = 'TRUE'
--itlearner注:
显示表信息
select
from tablespaceinfo
order by cast(left(ltrim(rtrim(reserved)),len(ltrim(rtrim(reserved)))-2) as int) desc
运行效果如图:
<IMG alt="" src="https://files.jb51.cc/file_images/article/201306/2013062110341526.jpg">
很显然这个返回结果是
错误的。但是它提供了一种思路,<SPAN style="COLOR: #ff0000">
修改后的
sql语句如下:
<div class="codetitle">
<a style="CURSOR: pointer" data="10938" class="copybut" id="copybut10938" onclick="doCopy('code10938')"> 代码如下: <div class="codebody" id="code10938">
View Code
IF NOT EXISTS ( SELECT
FROM sys.tables
WHERE name = 'tablespaceinfo' )
BEGIN
CREATE TABLE tablespaceinfo --创建结果存储表
(
Table_Name VARCHAR(50),
Rows_Count INT,
reserved INT,
datainfo INT,
index_size INT,
unused INT
)
END
DELETE FROM tablespaceinfo
--清空数据表
CREATE TABLE #temp --创建结果存储表
(
nameinfo VARCHAR(50),
rowsinfo INT,
reserved VARCHAR(20),
datainfo VARCHAR(20),
index_size VARCHAR(20),
unused VARCHAR(20)
)
DECLARE @tablename VARCHAR(255)
--表
名称DECLARE @cmd
sql NVARCHAR(500)
DECLARE Info_cursor CURSOR
FOR
SELECT '[' + TABLE_SCHEMA + '].[' + TABLE_NAME + ']' AS Table_Name
FROM [INFORMATION_SCHEMA].[TABLES]
WHERE TABLE_TYPE = 'BASE TABLE'
AND TABLE_NAME <> 'tablespaceinfo'
OPEN Info_cursor
FETCH NEXT FROM Info_cursor
INTO @tablename
WHILE @@FETCH_STATUS = 0
BEGIN
SET @cmd
sql = 'insert into #temp exec sp_spaceused ''' + @tablename
+ ''''
EXECUTE sp_execute
sql @cmd
sql FETCH NEXT FROM Info_cursor
INTO @tablename
END
CLOSE Info_cursor
DEALLOCATE Info_cursor
GO
--itlearner注:
显示数据库信息
--sp_spaceused @updateusage = 'TRUE'
--itlearner注:
显示表信息
UPDATE #temp
SET reserved = REPLACE(reserved,'KB',''),
datainfo = REPLACE(datainfo,
index_size = REPLACE(index_size,
unused = REPLACE(unused,'')
INSERT INTO dbo.tablespaceinfo
SELECT nameinfo,
CAST(rowsinfo AS INT),
CAST(reserved AS INT),
CAST(datainfo AS INT),
CAST(index_size AS INT),
CAST(unused AS INT)
FROM #temp
DROP TABLE #temp
SELECT Table_Name,
Rows_Count,
CASE WHEN reserved > 1024
THEN CAST(reserved / 1024 AS VARCHAR(10)) + 'Mb'
ELSE CAST(reserved AS VARCHAR(10)) + 'KB'
END AS Data_And_Index_Reserved,
CASE WHEN datainfo > 1024
THEN CAST(datainfo / 1024 AS VARCHAR(10)) + 'Mb'
ELSE CAST(datainfo AS VARCHAR(10)) + 'KB'
END AS Used,
CASE WHEN Index_size > 1024
THEN CAST(index_size / 1024 AS VARCHAR(10)) + 'Mb'
ELSE CAST(index_size AS VARCHAR(10)) + 'KB'
END AS index_size,
CASE WHEN unused > 1024 THEN CAST(unused / 1024 AS VARCHAR(10)) + 'Mb'
ELSE CAST(unused AS VARCHAR(10)) + 'KB'
END AS unused
FROM dbo.tablespaceinfo
ORDER BY reserved DESC