当前位置:
凯发ag旗舰厅登录网址下载 >
运维知识
> 数据库
>内容正文
数据库
sql server 数据库巡检脚本 -凯发ag旗舰厅登录网址下载
凯发ag旗舰厅登录网址下载
收集整理的这篇文章主要介绍了
sql server 数据库巡检脚本
小编觉得挺不错的,现在分享给大家,帮大家做个参考.
--1.查看数据库版本信息
select @@version
--2.查看所有数据库名称及大小
exec sp_helpdb
--3.查看数据库所在机器的操作系统参数
exec master..xp_msver
--4.查看数据库启动的参数
exec sp_configure
--5.查看数据库启动时间
select convert(varchar(30),login_time,120)
from master..sysprocesses where spid=1
--6.查看数据库服务器名
select 'server name:'ltrim(@@servername)
--7.查看数据库实例名
select 'instance:'ltrim(@@servicename)
--8.数据库的磁盘空间呢使用信息
exec sp_spaceused
--9.日志文件大小及使用情况
dbcc sqlperf(logspace)
--10.表的磁盘空间使用信息
exec sp_spaceused 'tablename'
--11.获取磁盘读写情况
select
@@total_read [读取磁盘次数],
@@total_write [写入磁盘次数],
@@total_errors [磁盘写入错误数],
getdate() [当前时间]
--12.获取i/o工作情况
select @@io_busy,
@@timeticks [每个时钟周期对应的微秒数],
@@io_busy*@@timeticks [i/o操作毫秒数],
getdate() [当前时间]
--13.查看cpu活动及工作情况
select
@@cpu_busy,
@@timeticks [每个时钟周期对应的微秒数],
@@cpu_busy*cast(@@timeticks as float)/1000 [cpu工作时间(秒)],
@@idle*cast(@@timeticks as float)/1000 [cpu空闲时间(秒)],
getdate() [当前时间]
--14.检查锁与等待
exec sp_lock
--15.检查死锁
exec sp_who_lock --自己写个存储过程即可
/*
create procedure sp_who_lock
as
begin declare @spid int,@bl int, @inttransactioncountonentry int, @introwcount int, @intcountproperties int, @intcounter int create table #tmp_lock_who (id int identity(1,1),spid smallint,bl smallint) if @@error<>0 return @@error insert into #tmp_lock_who(spid,bl) select 0 ,blocked from (select * from sys.sysprocesses where blocked>0 ) a where not exists(select * from (select * from sys.sysprocesses where blocked>0 ) b where a.blocked=spid) union select spid,blocked from sys.sysprocesses where blocked>0 if @@error<>0 return @@error -- 找到临时表的记录数 select @intcountproperties = count(*),@intcounter = 1 from #tmp_lock_who if @@error<>0 return @@error if @intcountproperties=0 select '现在没有阻塞和死锁信息' as message -- 循环开始 while @intcounter <= @intcountproperties begin -- 取第一条记录 select @spid = spid,@bl = bl from #tmp_lock_who where id = @intcounter begin if @spid =0 select '引起数据库死锁的是: ' cast(@bl as varchar(10)) '进程号,其执行的sql语法如下' else select '进程号spid:' cast(@spid as varchar(10)) '被' '进程号spid:' cast(@bl as varchar(10)) '阻塞,其当前进程执行的sql语法如下' dbcc inputbuffer (@bl ) end -- 循环指针下移 set @intcounter = @intcounter 1 end drop table #tmp_lock_who return 0
end
*/--16.用户和进程信息
exec sp_who
exec sp_who2--17.活动用户和进程的信息
exec sp_who 'active'--18.查看进程中正在执行的sql
dbcc inputbuffer(进程号)
exec sp_who3
/*
create procedure sp_who3 ( @sessionid int = null )
as beginselect spid = er.session_id ,status = ses.status ,[login] = ses.login_name ,host = ses.host_name ,blkby = er.blocking_session_id ,dbname = db_name(er.database_id) ,commandtype = er.command ,sqlstatement = st.text ,objectname = object_name(st.objectid) ,elapsedms = er.total_elapsed_time ,cputime = er.cpu_time ,ioreads = er.logical_reads er.reads ,iowrites = er.writes ,lastwaittype = er.last_wait_type ,starttime = er.start_time ,protocol = con.net_transport ,connectionwrites = con.num_writes ,connectionreads = con.num_reads ,clientaddress = con.client_net_address ,authentication = con.auth_schemefrom sys.dm_exec_requests erouter apply sys.dm_exec_sql_text(er.sql_handle) stleft join sys.dm_exec_sessions ses on ses.session_id = er.session_idleft join sys.dm_exec_connections con on con.session_id = ses.session_idwhere er.session_id > 50and @sessionid is nullor er.session_id = @sessionidorder by er.blocking_session_id desc ,er.session_id end
*/--19.查看所有数据库用户登录信息
exec sp_helplogins --20.查看所有数据库用户所属的角色信息
exec sp_helpsrvrolemember--21.查看链接服务器
exec sp_helplinkedsrvlogin--22.查看远端数据库用户登录信息
exec sp_helpremotelogin--23.获取网络数据包统计信息
select
@@pack_received [输入数据包数量],
@@pack_sent [输出数据包数量],
@@packet_errors [错误包数量],
getdate() [当前时间]--24.检查数据库中的所有对象的分配和机构完整性是否存在错误
dbcc checkdb--25.查询文件组和文件
select df.[name],df.physical_name,df.[size],df.growth, f.[name][filegroup],f.is_default
from sys.database_files df join sys.filegroups f
on df.data_space_id = f.data_space_id --26.查看数据库中所有表的条数
select b.name as tablename , a.rowcnt as datacount
from sysindexes a , sysobjects b
where a.id = b.id and a.indid < 2 and objectproperty(b.id, 'ismsshipped') = 0 --27.得到最耗时的前10条t-sql语句
;with maco as
( select top 10 plan_handle, sum(total_worker_time) as total_worker_time , sum(execution_count) as execution_count , count(1) as sql_count from sys.dm_exec_query_stats group by plan_handle order by sum(total_worker_time) desc
)
select t.text , a.total_worker_time , a.execution_count , a.sql_count
from maco a cross apply sys.dm_exec_sql_text(plan_handle) t --28. 查看sql server的实际内存占用
select * from sysperfinfo where counter_name like '%memory%'--29.显示所有数据库的日志空间信息
dbcc sqlperf(logspace)--30.收缩数据库
dbcc shrinkdatabase(databasename)
总结
以上是凯发ag旗舰厅登录网址下载为你收集整理的sql server 数据库巡检脚本的全部内容,希望文章能够帮你解决所遇到的问题。
如果觉得凯发ag旗舰厅登录网址下载网站内容还不错,欢迎将凯发ag旗舰厅登录网址下载推荐给好友。
- 上一篇:
- 下一篇: