在MS Sql Server中可以能过以下的方法查询出磁盘空间的使用情况及各数据库数据文件及日志文件的大小及使用利用率:

1、查询各个磁盘分区的剩余空间:
Exec master.dbo.xp_fixeddrives

2、查询数据库的数据文件及日志文件的相关信息(包括文件组、当前文件大小、文件最大值、文件增长设置、文件逻辑名、文件路径等)
select * from [数据库名].[dbo].[sysfiles]
转换文件大小单位为MB:
select name, convert(float,size) * (8192.0/1024.0)/1024. from [数据库名].dbo.sysfiles

3、查询当前数据库的磁盘使用情况:
Exec sp_spaceused

4、查询数据库服务器各数据库日志文件的大小及利用率
DBCC SQLPERF(LOGSPACE)


--查看4小时内的CPU变化值,1分钟统计一次

declare @ts_now bigint

select @ts_now = ms_ticks from sys.dm_os_sys_info

--select * from sys.dm_os_sys_info

select record_id,

      dateadd(ms, convert(bigint,-1) * (@ts_now - [timestamp]), GetDate()) as EventTime,

      SQLProcessUtilization,

      SystemIdle,

      100 - SystemIdle - SQLProcessUtilization as OtherProcessUtilization

from (

      select

            record.value('(./Record/@id)[1]', 'int') as record_id,

            record.value('(./Record/SchedulerMonitorEvent/SystemHealth/SystemIdle)[1]', 'int') as SystemIdle,

            record.value('(./Record/SchedulerMonitorEvent/SystemHealth/ProcessUtilization)[1]', 'int') as SQLProcessUtilization,

            timestamp

      from (

            select timestamp, convert(xml, record) as record

            from sys.dm_os_ring_buffers

            where ring_buffer_type = N'RING_BUFFER_SCHEDULER_MONITOR'

            and record like '%<SystemHealth>%') as x

      ) as y

order by record_id desc

--查看磁盘空间大小

create table #a (id int IDENTITY(1,1),DiskName varchar(50))
go
sp_configure 'show advanced options',1
reconfigure
go
sp_configure 'xp_cmdshell',1
reconfigure
GO insert into #a(DiskName) exec xp_cmdshell 'wmic LOGICALDISK get name' create table #b (id int IDENTITY(1,1),freespace varchar(50)) insert into #b(freespace) exec xp_cmdshell 'wmic LOGICALDISK get freespace' create table #c (id int IDENTITY(1,1),size varchar(50)) insert into #c(size) exec xp_cmdshell 'wmic LOGICALDISK get size' select server_name=@@servername,DiskName ,convert(bigint,replace(size,char(13),''))/1024/1024/1024 as total_disk_size_gb ,convert(bigint,replace(#b.freespace,char(13),''))/1024/1024/1024 as free_disk_size_gb ,convert(varchar,convert(decimal(4, 2),(convert(decimal(15, 2),convert(decimal(15, 2),replace(#b.freespace,char(13),''))/1024/1024/1024*100)/ convert(decimal(15, 2),convert(decimal(15, 2),replace(size,char(13),''))/1024/1024/1024))))+'%' as free_space_percent from #a join #b on #a.id=#b.id join #c on #a.id=#c.id where #a.id >1 and #b.freespace is not null and charindex(char(13),replace(#b.freespace,' ','')) <>1 drop table #a,#b,#c

[SQL] 用SQL语句检查CPU和磁盘空间的更多相关文章

  1. 用SQL语句检查CPU和磁盘空间

    --查看4小时内的CPU变化值,1分钟统计一次 DECLARE @ts_now BIGINT; SELECT @ts_now = ms_ticks FROM sys.dm_os_sys_info; - ...

  2. fatal error LNK1201:写入程序数据库“***.pdb”时出错;请检查是否是磁盘空间不足、路径无效或权限不够

    问题很简单,是因为你的程序正在运行,或者windbg工具在执行dump文件,文件被占用,所以无法写入:

  3. 检查linux的磁盘空间占用

    先初步看看哪个目录占用最大$ df -h 然后细看遍历某目录的占用情况:$ sudo du -a /data  | sort -nr | less(单位是KB)

  4. 解决vs 编译的bug“请检查是否是磁盘空间不足、路径无效或权限不够”

    昨晚用vs编译遇到一个问题,编译一半发现硬盘没空间,一直重启vs,重启电脑, 删除pdb文件都没用,之后尝试重新生成解决方案,就解决了.这个是vs的一个bug

  5. linux磁盘空间满的处理

    Java中运行SQL插入数据时报错: linux磁盘空间满处理: 1.df -h  查看磁盘空间占用,实际上是查看磁盘块占用的文件(block) 2.分别查看输入以下命令 (面对磁盘满了,通过下列命令 ...

  6. MySQL中查询所有数据库占用磁盘空间大小和单个库中所有表的大小的sql语句

    查询所有数据库占用磁盘空间大小的SQL语句: ,),' MB') as data_size, concat(,),'MB') as index_size from information_schema ...

  7. SQL Server阻塞的检查

    1. 阻塞   除了内存.CPU.I/O这些系统资源以外,阻塞和死锁是影响数据库应用性能的另一大因素. 所谓的「阻塞」,是指当一个数据库会话中的事务,正在锁定其他会话事务想要读取或修改的资源,造成这些 ...

  8. SQL 2008执行语句遇到内存不足(1)——error 701

    原文:SQL 2008执行语句遇到内存不足(1)--error 701 转自:http://blogs.msdn.com/b/apgcdsd/archive/2011/01/17/sql-2008-e ...

  9. 《Troubleshooting SQL Server》读书笔记-CPU使用率过高(下)

    <Troubleshooting SQL Server>读书笔记-CPU使用率过高(下) 第三章 High CPU Utilization. CPU使用率过高的常见原因 查询优化器会尽量从 ...

随机推荐

  1. json字符串使用注意问题

    json本身是字符串,即 json字符串 js使用 要把 json字符串 转为  javascript对象 json字符串转为js对象的方法:jquery的parseJSON var str='[{& ...

  2. Ubuntu下安装配置redis

    安装redis apt-get install redis-server 查看是否启动 ps -aux|grep redis 客户端连接 注: 安装Redis服务器,会自动地一起安装Redis命令行客 ...

  3. “NHibernate.Cfg.Configuration 的类型初始值设定项引发异常。”的解决方法【备忘】

    今天搞到NHibernate时,突然报了一个“NHibernate.Cfg.Configuration 的类型初始值设定项引发异常.”的异常. 详细异常信息“System.IO.FileLoadExc ...

  4. iOS app开发入门

    https://github.com/qinjx/30min_guides/blob/master/ios.md

  5. Track and Follow an Object----4

    原创博文:转载请标明出处(周学伟):http://www.cnblogs.com/zxouxuewei/tag/ ntroduction: 在本示例中,我们将探索包含Kinect摄像头的自主行为. 这 ...

  6. EditDistance,求两个字符串最小编辑距离,动态规划

    问题描述: 题目描述Edit DistanceGiven two words word1 and word2, find the minimum number of steps required to ...

  7. 【Oracle】两个表Join关联更新

    两个表关联,用B表的字段更新A表的字段. UPDATE ( SELECT A.COL1 A_COL, B.COL2 B_COL FROM table1 A INNER JOIN table2 B ON ...

  8. Maven -- 发布jar包至远程仓库

    啦啦啦

  9. 5 -- Hibernate的基本用法 --4 深入Hibernate配置文件

    Hibernate的持久化操作离不开SessionFactory对象,这个对象是整个数据库映射关系经过编译后的内存镜像,该对象的openSession()方法可打开Session对象.该对象通常由Co ...

  10. 【Ubuntu】Windows 远程桌面连接ubuntu及xrdp的一些小问题(远程桌面闪退、连接失败、tab补全功能,无菜单栏,error - problem connecting )【转】

    转:https://blog.csdn.net/u014447845/article/details/80291678 1.远程桌面闪退,shell可以用的问题:(1)需要在该用户目录创建一个.xse ...