SQL Server查看库、表占用空间大小
转自: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查看所有表大小,所占空间
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查看库、表占用空间大小的更多相关文章
- SQL Server 查看数据表占用空间大小的SQL语句
) ) if object_id('tempdb..#space') is not null drop table #space ),rows ),data ),index_size ),unused ...
- 查看sql server数据库各表占用空间大小
exec sp_MSForEachTable @precommand=N' create table ##(id int identity,表名 sysname,字段数 int,记录数 int,保留空 ...
- MySQL查看库表的大小
MySQL数据库空间使用情况查询 如果想知道MySQL数据库中每个表占用的空间.表记录的行数的话,可以打开MySQL的 information_schema 数据库.在该库中有一个 TABLES 表, ...
- MySQL数据库查看数据表占用空间大小和记录数
MySQL数据库中每个表占用的空间.表记录的行数的话,可以打开MySQL的 information_schema 数据库.在该库中有一个 TABLES 表,这个表主要字段分别是: TABLE_SCHE ...
- sql server 查看锁表SQL【转】
1.select * from sys.dm_tran_locks或sp_LOCK 查看request_node 字段中为'X'(排他锁)或'IX'(意向排他锁)2.用sp_who2 + pid(进程 ...
- sql server查看创建表的代码,表定义
1.查看建表语句在“对象资源管理器”中找到要导出的表,选中该表并单击右键,“编写表脚本为(S)”/“CREATE到(C)”/“新查询编辑器窗口”即可查看该表的建表语句.2.导出建表语句在“对象资源管理 ...
- SQL Server查看所有表大小、表行数和占用空间信息
一.查看表名和对应的数据行数select a.name as '表名',b.rows as '表数据行数'from sysobjects a inner join sysindexes bon a. ...
- SQL Server查看所有表大小,所占空间
create table #Data(name varchar(100),row varchar(100),reserved varchar(100),data varchar(100),index_ ...
- SQL Server 查看指定表上的索引
解决方案: sys.indexs; ---------------------------------------------------------------------------------- ...
随机推荐
- linux nfs
linux(十四)之linux NFS服务管理 学到这里差不多就结束了linux的基础学习了,其实linux的内容并不难,我们要经常的反复的去操作它,多多和它去联络感情才能很好的掌握这个linux. ...
- 在MathType如何让括号随内容自动调整大小的技巧
MathType软件是一款数学公式编辑器工具可以轻松输入各种复杂的公式和符号,与Office文档完美结合,显示效果超好,比Office自带的公式编辑器要强大很多.但是很多的新手朋友不知道在MathTy ...
- linux中,通过crontab -e编辑生成的定时任务,写在哪个文件中
环境描述: 操作系统:Red Hat Enterprise Linux Server release 6.6 (Santiago) 内核版本:2.6.32-504.el6.x86_64 需求描述: 一 ...
- Spring MVC异常统一处理
package com.shzq.common.exception; import java.io.PrintWriter;import java.io.StringWriter;import jav ...
- wm_concat函数 用法
首先让我们来看看这个神奇的函数wm_concat(列名),该函数可以把列值以","号分隔起来,并显示成一行,接下来上例子,看看这个神奇的函数如何应用 准备测试数据 SQL> ...
- ionic函数 官方使用帮助
项目里 lib/js/ionic.bundle.js 里很多ionic的函数,里面还带了很多使用示例,认真看一下肯定会对使用ionic有很多帮助啊!! 例如:$http
- mybatis 循环遍历
/****Service/ public ServiceMessage<MemberFreedomRepModel> getMFListByPay(Long memberId,Long f ...
- php遍历文件夹下的所有文件及文件夹
//第一种 遍历放入数据中 function my_scandir($dir) { $files = array(); if ( $handle = opendir($dir) ) { while ( ...
- 【java】将List中的实体按照某个字段进行分组的算法
如何将List中存放的实体按照某个字段进行分组呢?来看看下面的例子,假如实体中有个字段叫批次号,我们将具有相同批次号的实体放在一起,那么怎么实现呢?看下面的代码: 可以定义个Map,Map的key用于 ...
- Oracle12cWindows安装、介绍及简单使用(图文)
1.下载 地址为:http://www.oracle.com/technetwork/cn/database/enterprise-edition/downloads/index.html 含企业版和 ...