SQL Server适用脚本收集一

1.IO分析

 1 with DB_IO as (

 2 select

 3     db_name(a.database_id) as [dbname],

 4     case when b.type_desc='LOG' then 'LogFile' else 'DataFile' end as FileType,

 5     num_of_reads,

 6     num_of_writes,

 7     sum(a.io_stall) as io_stall,

 8     sum(num_of_bytes_read+num_of_bytes_written) as TotalIOBytes

 9 from sys.dm_io_virtual_file_stats(null,null) a join sys.master_files b

10 on a.database_id=b.database_id and a.file_id=b.file_id

11 group by db_name(a.database_id),b.type_desc,num_of_reads,num_of_writes

12 )

13 select

14     dbname,

15     FileType,

16     num_of_reads,

17     num_of_writes,

18     cast(1. * TotalIOBytes/(1024*1024) as decimal(12,2)) as IO_MB,

19     cast(1. * io_stall/1000 as decimal(12,2)) as IO_stall_s,

20     cast(100. * io_stall/sum(io_stall) over() as decimal(10,2)) as [IO_stall_pct%],--占总IO的百分比

21     row_number() over(order by io_stall desc) as rn

22 from DB_IO
View Code

 

2.表结构

 1 DECLARE @TableName varchar(20)

 2 SET @TableName='HR_Duty'

 3 SELECT

 4         (CASE when a.colorder=1 then d.name else '' end) AS 表名,

 5         a.colorder 字段序号,

 6         a.name 字段名,

 7         (case when COLUMNPROPERTY( a.id,a.name,'IsIdentity')=1 then ''else '' end) 标识,

 8         (case when (SELECT count(*)

 9         FROM sysobjects

10         WHERE (name in

11                   (SELECT name

12                 FROM sysindexes

13                 WHERE (id = a.id) AND (indid in

14                           (SELECT indid

15                          FROM sysindexkeys

16                          WHERE (id = a.id) AND (colid in

17                                    (SELECT colid

18                                   FROM syscolumns

19                                   WHERE (id = a.id) AND (name = a.name))))))) AND

20               (xtype = 'PK'))>0 then '' else '' end) 主键,

21        b.name 类型,

22        a.length 占用字节数,

23        COLUMNPROPERTY(a.id,a.name,'PRECISION') as 长度,

24        isnull(COLUMNPROPERTY(a.id,a.name,'Scale'),0) as 小数位数,

25        (case when a.isnullable=1 then ''else '' end) 允许空,

26        isnull(e.text,'') 默认值,

27        isnull(g.[value],'') AS 字段说明

28 

29 FROM syscolumns  a

30     LEFT JOIN systypes b on  a.xtype=b.xusertype

31     inner join sysobjects d on a.id=d.id  and  d.xtype='U' and  d.name<>'dtproperties'

32     left join syscomments e on a.cdefault=e.id

33     left join sys.extended_properties g on a.id=g.major_id AND a.colid = g.minor_id

34 WHERE d.name=@TableName    --如果只查询指定表,加上此条件

35 order by a.id,a.colorder

36 

37 SELECT * FROM sys.extended_properties
View Code

 

3.查进程及CPU占用时间,text为运行的Sql

 1 select db_name(database_id),session_id,cpu_time,start_time,command,database_id,[user_id],connection_id,[text]

 2 from sys.dm_exec_requests t1

 3 cross apply sys.dm_exec_sql_text(t1.sql_handle)

 4 --WHERE session_id=@@spid

 5 ORDER BY cpu_time desc

 6 --杀死相关进程

 7 SELECT @@spid

 8 --KILL 2529

 9 

10 --sELECT text FROM sys.dm_exec_requests as t1

11 --cross apply sys.dm_exec_sql_text(t1.sql_handle)

12 --,sys.dm_os_tasks t2,sys.dm_os_threads t3

13 --where t1.session_id=t2.session_id and t2.worker_address=t3.worker_address

14 --and  t3.os_thread_id=24564.查找对象依赖项--------------------------------------------------------------------------------

15 --查找对象依赖项

16 --所有依赖此对象的 过程,函数,触发器,视图

17 ------------------------------------------------------------------------------

18 declare @obj_name nvarchar(128)

19 select @obj_name='C_Users'--'GetCSWC'

20 --select @obj_name='C_Depts'--'GetCSWC'

21 create table #obj_depends

22 (

23     [obj_name] nvarchar(128),

24     [obj_type] char(2)

25 )

26 

27 insert into #obj_depends

28 select o.[name],o.type

29 from sys.syscomments c

30     left join sys.objects o on c.id=o.[object_id]

31 where c.text like '%'+@obj_name+'%' and o.[name]<>@obj_name

32 

33 if exists(select 1 from sys.syscomments where object_name(id)<>@obj_name group by id having count(*)>1) begin

34     declare @objid int

35     declare cur_comments cursor for select id from sys.syscomments where object_name(id)<>@obj_name group by id having count(*)>1

36     open cur_comments

37     fetch cur_comments into @objid

38     while @@fetch_status=0 begin

39         declare @text nvarchar(4000)

40         select @text=''

41         select @text=@text+left(ltrim([text]),128)+'...'+right(rtrim([text]),128)

42         from sys.syscomments where id=@objid order by colid

43         if @text like '%'+@obj_name+'%' begin

44             if not exists(select 1 from #obj_depends where obj_name=object_name(@objid)) begin

45                 insert into #obj_depends

46                 select top 1 o.[name],o.type

47                 from sys.syscomments c

48                     left join sys.objects o on c.id=o.[object_id]

49                 where c.id=@objid

50             end

51         end

52         fetch next from cur_comments into @objid

53     end

54     close cur_comments

55     deallocate cur_comments

56 end

57 

58 select distinct * from #obj_depends order by [obj_type],[obj_name]

59 drop table #obj_depends
View Code


4.根据SQL存储过程名取得存储过程的内容

 1 --     GetContentByProcedureName '[dbo].[存储过程名]'

 2 --Create procedure GetContentByProcedureName

 3 --(@ProcedureName nvarchar(500))

 4 --as

 5 DECLARE @ProcedureName nvarchar(500)

 6 SELECT @ProcedureName='B_Add_Proc'

 7 if exists (select * from dbo.syscomments where id=object_id(N''+@ProcedureName+''))

 8     select c.text, c.encrypted, c.number,

 9     xtype=convert(nchar(2), o.xtype),    

10     datalength(c.text), convert(varbinary(8000),

11     c.text), 0 from dbo.syscomments c, dbo.sysobjects o   

12     where o.id = c.id and c.id = object_id(N''+@ProcedureName+'')

13     order by c.number, c.colid option(robust plan)
View Code

 

你可能感兴趣的:(SQL Server)