转自:https://blog.csdn.net/yenange/article/details/50493580

查询数据文件与日志文件占用情况,查看数据大小,查看库大小

1. 查看数据文件占用(权限要求较大)

DBCC showfilestats
2. 查看日志文件占用
dbcc sqlperf(logspace) 

USE master
go
--简易版
SELECT
Name,
physical_name,
Size/128.0 AS [Size(MB)],
FILEPROPERTY(Name,'SpaceUsed')/128.0 AS [SpaceUsed(MB)],
STR(FILEPROPERTY(Name,'SpaceUsed')*1.0/Size*100,6,3) AS [SpaceUsed(%)]
FROM master.sys.database_files
--详细版
SELECT a.name [文件名称] ,cast(a.[size]*1.0/128 as decimal(12,1)) AS [文件设置大小(MB)] ,
CAST( fileproperty(s.name,'SpaceUsed')/(8*16.0) AS DECIMAL(12,1)) AS [文件所占空间(MB)] ,
CAST( (fileproperty(s.name,'SpaceUsed')/(8*16.0))/(s.size/(8*16.0))*100.0 AS DECIMAL(12,1)) AS [所占空间率%] ,
CASE WHEN A.growth =0 THEN '文件大小固定,不会增长' ELSE '文件将自动增长' end [增长模式] ,CASE WHEN A.growth > 0 AND is_percent_growth = 0
THEN '增量为固定大小' WHEN A.growth > 0 AND is_percent_growth = 1 THEN '增量将用整数百分比表示' ELSE '文件大小固定,不会增长' END AS [增量模式] ,
CASE WHEN A.growth > 0 AND is_percent_growth = 0 THEN cast(cast(a.growth*1.0/128as decimal(12,0)) AS VARCHAR)+'MB'
WHEN A.growth > 0 AND is_percent_growth = 1 THEN cast(cast(a.growth AS decimal(12,0)) AS VARCHAR)+'%' ELSE '文件大小固定,不会增长' end AS [增长值(%或MB)] ,
a.physical_name AS [文件所在目录] ,a.type_desc AS [文件类型]
FROM sys.database_files a
INNER JOIN sys.sysfiles AS s ON a.[file_id]=s.fileid
LEFT JOIN sys.dm_db_file_space_usage b ON a.[file_id]=b.[file_id] ORDER BY a.[type]

转自:https://www.cnblogs.com/nikyxxx/archive/2012/10/08/2715423.html

SQL Server查看所有表大小,所占空间

基于T-SQL:
SELECT
t.NAME AS TableName,
s.Name AS SchemaName,
p.rows AS RowCounts,
SUM(a.total_pages) * 8 AS TotalSpaceKB,
CAST(ROUND(((SUM(a.total_pages) * 8) / 1024.00), 2) AS NUMERIC(36, 2)) AS 总共占用空间,
SUM(a.used_pages) * 8 AS UsedSpaceKB,
CAST(ROUND(((SUM(a.used_pages) * 8) / 1024.00), 2) AS NUMERIC(36, 2)) AS UsedSpaceMB,
(SUM(a.total_pages) - SUM(a.used_pages)) * 8 AS UnusedSpaceKB,
CAST(ROUND(((SUM(a.total_pages) - SUM(a.used_pages)) * 8) / 1024.00, 2) AS NUMERIC(36, 2)) AS UnusedSpaceMB
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
t.NAME NOT LIKE 'dt%'
AND t.is_ms_shipped = 0
AND i.OBJECT_ID > 0
GROUP BY
t.Name, s.Name, p.Rows
ORDER BY
总共占用空间 desc

基于存储过程--主要原理: 
exec sp_spaceused '表名' --取得表占用空間 
exec sp_spaceused ''--數據庫所有空間

create table #Data(name varchar(100),row varchar(100),reserved varchar(100),data varchar(100),index_size varchar(100),unused varchar(100)) 

declare @name varchar(100)
declare cur cursor for
select name from sysobjects where xtype='u' order by name
open cur
fetch next from cur into @name
while @@fetch_status=0
begin
insert into #data
exec sp_spaceused @name
print @name fetch next from cur into @name
end
close cur
deallocate cur create table #DataNew(name varchar(100),row int,reserved int,data int,index_size int,unused int) insert into #dataNew
select name,convert(int,row) as row,convert(int,replace(reserved,'KB','')) as reserved,convert(int,replace(data,'KB','')) as data,
convert(int,replace(index_size,'KB','')) as index_size,convert(int,replace(unused,'KB','')) as unused from #data select * from #dataNew order by data desc

查看索引大小

--查看索引大小
如果您想要表的每个索引的大小,请使用以下两个查询中的一个: SELECT
i.name AS IndexName,
SUM(s.used_page_count) * 8 AS IndexSizeKB
FROM sys.dm_db_partition_stats AS s
JOIN sys.indexes AS i
ON s.[object_id] = i.[object_id] AND s.index_id = i.index_id
WHERE s.[object_id] = object_id('dbo.TableName')
GROUP BY i.name
ORDER BY i.name ---第2种方法
SELECT
i.name AS IndexName,
SUM(page_count * 8) AS IndexSizeKB
FROM sys.dm_db_index_physical_stats(
db_id(), object_id('dbo.TableName'), NULL, NULL, 'DETAILED') AS s
JOIN sys.indexes AS i
ON s.[object_id] = i.[object_id] AND s.index_id = i.index_id
GROUP BY i.name
ORDER BY i.name
结果通常略有不同,但在1%之内。

查看堆表及数据量超过10W行的

select * from (
SELECT tables.NAME,
(SELECT rows
FROM sys.partitions
WHERE object_id = tables.object_id
AND index_id = 0 -- 0 is for heap
-- 1 is for clustered index
And rows >=100000
)AS numberofrows
FROM db_tank.sys.tables tables
WHERE Objectproperty(tables.object_id, N'TableHasClustIndex') = 0
)t where numberofrows is not null

查看表数据行数

但这种办法不是实时的,是sql server定时做的统计操作,执行下面代码可进一步精确

DBCC UpdateUSAGE(DatabaseName,[TABLENAME])WITH ROW_COUNTS

SELECT   a.name, b.rows
FROM sysobjects AS a INNER JOIN
sysindexes AS b ON a.id = b.id
WHERE (a.type = 'u') AND (b.indid IN (0, 1))
ORDER BY b.rows DESC

SQL Server查看库、表占用空间大小的更多相关文章

  1. SQL Server 查看数据表占用空间大小的SQL语句

    ) ) if object_id('tempdb..#space') is not null drop table #space ),rows ),data ),index_size ),unused ...

  2. 查看sql server数据库各表占用空间大小

    exec sp_MSForEachTable @precommand=N' create table ##(id int identity,表名 sysname,字段数 int,记录数 int,保留空 ...

  3. MySQL查看库表的大小

    MySQL数据库空间使用情况查询 如果想知道MySQL数据库中每个表占用的空间.表记录的行数的话,可以打开MySQL的 information_schema 数据库.在该库中有一个 TABLES 表, ...

  4. MySQL数据库查看数据表占用空间大小和记录数

    MySQL数据库中每个表占用的空间.表记录的行数的话,可以打开MySQL的 information_schema 数据库.在该库中有一个 TABLES 表,这个表主要字段分别是: TABLE_SCHE ...

  5. sql server 查看锁表SQL【转】

    1.select * from sys.dm_tran_locks或sp_LOCK 查看request_node 字段中为'X'(排他锁)或'IX'(意向排他锁)2.用sp_who2 + pid(进程 ...

  6. sql server查看创建表的代码,表定义

    1.查看建表语句在“对象资源管理器”中找到要导出的表,选中该表并单击右键,“编写表脚本为(S)”/“CREATE到(C)”/“新查询编辑器窗口”即可查看该表的建表语句.2.导出建表语句在“对象资源管理 ...

  7. SQL Server查看所有表大小、表行数和占用空间信息

    一.查看表名和对应的数据行数select  a.name as '表名',b.rows as '表数据行数'from sysobjects a inner join sysindexes bon a. ...

  8. SQL Server查看所有表大小,所占空间

    create table #Data(name varchar(100),row varchar(100),reserved varchar(100),data varchar(100),index_ ...

  9. SQL Server 查看指定表上的索引

    解决方案: sys.indexs; ---------------------------------------------------------------------------------- ...

随机推荐

  1. Oracle RAC:使用 NFS 共享存储时的 mount 选项 总结

    oracle rac 使用nfs作为共享存储时,mount的选项有 要求,不能随便设置 grid的要求:      rw,bg,hard,nointr,rsize=32768,wsize=32768, ...

  2. Windows路径操作API函数学习

    前言 在VC++开发过程中,经常需要用到一些路径操作,比如拼需要的文件路径,搜索路径中的内容等等.Windows提供了一套关于路径操作的API帮助我们更好的执行这些操作. 路径截断与合并API Pat ...

  3. OpenCV学习:体验ImageWatch

    Image Watch是在VS2012及以上版本上使用的一款OpenCV插件工具,能够实时显示图像和矩阵Mat的内容,跟Matlab很像,方便程序调试,相当好用. 1)安装Visual Studio ...

  4. 3th January 2014

    I owe my girl so much, i want to pay her, i know this is impossible,but I still try hard.

  5. tiny4412 linux+qtopia nfs网络文件系统的挂载

    1,首先确定uboot启动内核的bootargs参数 Linux-CommandLine = root=/dev/nfs nfsroot=192.168.1.131:/home/tiny4412/ro ...

  6. grep递归查找子目录

    想要在各种文件里面找一个指定的文本,本来的方法太土了,在网上搜了一下,发现个好的方法,不过也有些问题.原文如下: 第一个,这个是看别人脚本的,配合find实现,-maxdepth指定深度,如果查找到底 ...

  7. poj_3274 哈希

    哈希 Hash 哈希是一种将复杂数据转换为线性数据从而可以进行随机访问的查找算法. 哈希经常用于对复杂数据(如结构体.对象等)的查找,在使用的时候,需要定义一个Hash函数,将需要查找的复杂对象转化为 ...

  8. 【linux】Crontab 定时任务 使用实例

    1 使用putty 登录linux 服务器 2 输入以下命令.查看已有的定时任务 crontab -l 3 输入  以下命令,进入定时任务文件 crontab -e 4  键盘 选择 i  键 进行输 ...

  9. ajax jquery校验用户是否已经注册

    服务端代码这里就不贴了 html代码比较简单,需要自行引入jquery库 <body> 请输入用户名:<input type="text" id="us ...

  10. cursor:hand & cursor:pointer

    1.cursor:hand & cursor:pointer都是将鼠标设置为手形. 2.cursor:hand存在兼容性问题,firefox并不支持该属性值.但大部分主流浏览器支持cursor ...