[SQL] 用SQL语句检查CPU和磁盘空间
在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和磁盘空间的更多相关文章
- 用SQL语句检查CPU和磁盘空间
--查看4小时内的CPU变化值,1分钟统计一次 DECLARE @ts_now BIGINT; SELECT @ts_now = ms_ticks FROM sys.dm_os_sys_info; - ...
- fatal error LNK1201:写入程序数据库“***.pdb”时出错;请检查是否是磁盘空间不足、路径无效或权限不够
问题很简单,是因为你的程序正在运行,或者windbg工具在执行dump文件,文件被占用,所以无法写入:
- 检查linux的磁盘空间占用
先初步看看哪个目录占用最大$ df -h 然后细看遍历某目录的占用情况:$ sudo du -a /data | sort -nr | less(单位是KB)
- 解决vs 编译的bug“请检查是否是磁盘空间不足、路径无效或权限不够”
昨晚用vs编译遇到一个问题,编译一半发现硬盘没空间,一直重启vs,重启电脑, 删除pdb文件都没用,之后尝试重新生成解决方案,就解决了.这个是vs的一个bug
- linux磁盘空间满的处理
Java中运行SQL插入数据时报错: linux磁盘空间满处理: 1.df -h 查看磁盘空间占用,实际上是查看磁盘块占用的文件(block) 2.分别查看输入以下命令 (面对磁盘满了,通过下列命令 ...
- MySQL中查询所有数据库占用磁盘空间大小和单个库中所有表的大小的sql语句
查询所有数据库占用磁盘空间大小的SQL语句: ,),' MB') as data_size, concat(,),'MB') as index_size from information_schema ...
- SQL Server阻塞的检查
1. 阻塞 除了内存.CPU.I/O这些系统资源以外,阻塞和死锁是影响数据库应用性能的另一大因素. 所谓的「阻塞」,是指当一个数据库会话中的事务,正在锁定其他会话事务想要读取或修改的资源,造成这些 ...
- SQL 2008执行语句遇到内存不足(1)——error 701
原文:SQL 2008执行语句遇到内存不足(1)--error 701 转自:http://blogs.msdn.com/b/apgcdsd/archive/2011/01/17/sql-2008-e ...
- 《Troubleshooting SQL Server》读书笔记-CPU使用率过高(下)
<Troubleshooting SQL Server>读书笔记-CPU使用率过高(下) 第三章 High CPU Utilization. CPU使用率过高的常见原因 查询优化器会尽量从 ...
随机推荐
- RMAN:简单的duplicate创建新数据库 for 12c+
构建参数文件 *.db_name='test2' ##### 需要注意的地方,和rman的duplicate目标库一致 *.compatible='18.0.0' ##### 关键的地方,每个版本的模 ...
- 解决网页响应慢,waiting(TTFB)时间过长
今天下午发现写的一个功能里,ajax等待时间过长 可以看到是waiting(TTFB)时间过长要1秒左右 用浏览器打开PHP页面测试,还是同样问题 将该接口的数据,都注释掉 发现是pdo连接数据库造成 ...
- android 不能在子线程中更新ui的讨论和分析
问题描写叙述 做过android开发基本都遇见过 ViewRootImpl$CalledFromWrongThreadException,上网一查,得到结果基本都是仅仅能在主线程中更改 ui.子线程要 ...
- SpringBoot------使用Fastjson解析Json数据
方法一: 1.在pom.xml文件下添加依赖包 <dependency> <groupId>com.alibaba</groupId> <artifactId ...
- centos下快速安装JDK
Linux系统自带了jdk(当然,如果没有,可以忽略这个步骤),但还是1.4的老版本,所以需要先卸载,然后在安装1.6,卸载步骤如下: [root@localhost ~]# rpm -qa | gr ...
- JSP面试知识
JSP方面 1. JSP四种范围是什么?区别是什么? Page:指单单一页jsp page的范围: Request:的范围只在一jsp页发出请求到另一页之间,随后这个属性失效: Session:范围是 ...
- Java使用dom4j读取xml时报错:org.dom4j.DocumentException: Error on line 2 of document : Invalid byte 2 of 2-byte UTF-8 sequence. Nested exception: Invalid byte 2 of 2-byte UTF-8 sequence
1.Java使用dom4j读取xml时报错: org.dom4j.DocumentException: Error on line 2 of document : Invalid byte 2 of ...
- SaltStack salt-ssh 用法
以下在 master 操作: (1) 我们在安装部署 SaltStack 的时候,需要安装 salt 客户端,还要与 salt 服务端进行认证才能互相通信(2) 如果我们使用 salt-ssh 的方式 ...
- Cesium添加水面
var viewer = new Cesium.Viewer('cesiumContainer');var waterPrimitive = new Cesium.Primitive({ //show ...
- 【LeetCode OJ】Remove Duplicates from Sorted Array
题目:Given a sorted array, remove the duplicates in place such that each element appear only once and ...