实战:sqlserver 日常检查脚本

实战:sqlserver 日常检查脚本,第1张

概述--sqlserver  日常检查脚本 print '----------------------------' print ' 0.sqlserver all information  ' print '----------------------------' print '                             ' print '**********************

--sqlserver  日常检查脚本

print '----------------------------'

print ' 0.sqlserver all information  ' print '----------------------------' print '                             ' print '*********************************' --Step 1: Setting NulLs and quoted IDentifIErs to ON and checking the version of sql Server  GO SET ANSI_NulLS ON GO SET QUOTED_IDENTIFIER ON GO IF EXISTS (SELECT * FROM dbo.sysobjects WHERE ID = OBJECT_ID(N'prodver') AND OBJECTPROPERTY(ID,N'IsUsertable') = 1)                         drop table prodver create table prodver ([index] int,name nvarchar(50),Internal_value int,Charcater_Value nvarchar(50)) insert into prodver exec xp_msver 'ProductVersion' if (select substring(Charcater_Value,1,1)from prodver)!=8 begin                     -- Step 2: This code will be used if the instance is Not sql Server 2000  Declare @image_path nvarchar(100)                         Declare @startup_type int                         Declare @startuptype nvarchar(100)                         Declare @start_username nvarchar(100)                         Declare @instance_name nvarchar(100)                         Declare @system_instance_name nvarchar(100)                         Declare @log_directory nvarchar(100)                         Declare @key nvarchar(1000)                         Declare @registry_key nvarchar(100)                         Declare @registry_key1 nvarchar(300)                         Declare @registry_key2 nvarchar(300)                         Declare @IpAddress nvarchar(20)                         Declare @domain nvarchar(50)                         Declare @cluster int                         Declare @instance_name1 nvarchar(100)                         -- Step 3: Reading registry keys for IP,BinarIEs,Startup type,startup username,errorlogs location and domain. SET @instance_name = coalesce(convert(nvarchar(100),serverproperty('Instancename')),'MSsqlSERVER');                         If @instance_name!='MSsqlSERVER'                         Set @instance_name=@instance_name                              Set @instance_name1= coalesce(convert(nvarchar(100),'MSsqlSERVER');                         If @instance_name1!='MSsqlSERVER'                         Set @instance_name1='MSsql$'+@instance_name1                         EXEC master.dbo.xp_regread N'HKEY_LOCAL_MACHINE',N'Software\Microsoft\Microsoft sql Server\Instance names\sql',@instance_name,@system_instance_name output;                                                  Set @key=N'SYstem\CurrentControlSet\Services\' +@instance_name1;                         SET @registry_key = N'Software\Microsoft\Microsoft sql Server\' + @system_instance_name + '\MSsqlServer\Parameters';                         If @registry_key is NulL                         set @instance_name=coalesce(convert(nvarchar(100),'MSsqlSERVER');                         EXEC master.dbo.xp_regread N'HKEY_LOCAL_MACHINE',@system_instance_name output;                         SET @registry_key = N'Software\Microsoft\Microsoft sql Server\' + @system_instance_name + '\MSsqlServer\Parameters';                         SET @registry_key1 = N'Software\Microsoft\Microsoft sql Server\' + @system_instance_name + '\MSsqlServer\supersocketnetlib\TCP\IP1';                         SET @registry_key2 = N'SYstem\ControlSet001\Services\Tcpip\Parameters\';                                                  EXEC master.dbo.xp_regread 'HKEY_LOCAL_MACHINE',@key,@value_name='ImagePath',@value=@image_path OUTPUT                         EXEC master.dbo.xp_regread 'HKEY_LOCAL_MACHINE',@value_name='Start',@value=@startup_type OUTPUT                         EXEC master.dbo.xp_regread 'HKEY_LOCAL_MACHINE',@value_name='Objectname',@value=@start_username OUTPUT                         EXEC master.dbo.xp_regread 'HKEY_LOCAL_MACHINE',@registry_key,@value_name='sqlArg1',@value=@log_directory OUTPUT                         EXEC master.dbo.xp_regread 'HKEY_LOCAL_MACHINE',@registry_key1,@value_name='IpAddress',@value=@IpAddress OUTPUT                         EXEC master.dbo.xp_regread 'HKEY_LOCAL_MACHINE',@registry_key2,@value_name='Domain',@value=@domain OUTPUT                                                  Set @startuptype= (select 'Start Up Mode' =                         CASE                         WHEN @startup_type=2 then 'autoMATIC'                         WHEN @startup_type=3 then 'MANUAL'                         WHEN @startup_type=4 then 'Disabled'                         END)                                                  --Step 4: Getting the cluster node names if the server is on cluster .else this value will be NulL. declare @Out nvarchar(400)                         SELECT @Out = COALESCE(@Out+'','') + Nodename                         from sys.dm_os_cluster_nodes                                                  -- Step 5: printing Server details                           SELECT                        @domain as 'Domain',                      serverproperty('ComputernamePhysicalNetBIOS') as 'Machinename',                      cpu_COUNT as 'cpuCount',(physical_memory_in_bytes/1048576) as 'PhysicalMemoryMB',                      @Ipaddress as 'IP_Address',                      @instance_name1 as 'Instancename',@image_path as 'BinarIEsPath',                      @log_directory as 'ErrorLogsLocation',                      @start_username as 'StartupUser',                      @Startuptype as 'StartupType',                      serverproperty('Productlevel') as 'ServicePack',                      serverproperty('edition') as 'Edition',                      serverproperty('productversion') as 'Version',                      serverproperty('collation') as 'Collation',                      serverproperty('Isclustered') as 'ISClustered',                      @out as 'ClusterNodes',                      serverproperty('IsFullTextInstalled') as 'ISFullText'                        From sys.dm_os_sys_info                                                 -- Step 6: Printing database details  SELECT                        serverproperty ('ComputernamePhysicalNetBIOS') as 'Machine'                      ,@instance_name1 as Instancename,                      (SELECT 'file_type' =                       CASE                       WHEN s.groupID <> 0 THEN 'data'                       WHEN s.groupID = 0 THEN 'log'                       END) AS 'fileType'                      ,d.dbID as 'DBID'                      ,d.name AS 'DBname'                      ,s.name AS 'Logicalfilename'                      ,s.filename AS 'Physicalfilename'                        ,(s.size * 8 / 1024) AS 'fileSizeMB' -- file size in MB                        ,d.cmptlevel as 'CompatibilityLevel'                        ,DATABASEPROPERTYEX (d.name,'Recovery') as 'RecoveryModel'                        ,'Status') as 'DatabaseStatus',                       --,d.is_published as 'Publisher'                         --,d.is_subscribed as 'Subscriber'                         --,d.is_distributor as 'distributor'    (SELECT 'is_replication' =                       CASE                       WHEN d.category = 1 THEN 'Published'                       WHEN d.category = 2 THEN 'subscribed'                       WHEN d.category = 4 THEN 'Merge published' WHEN d.category = 8 THEN 'merge subscribed' Else 'NO replication' END) AS 'Is_replication'                        ,m.mirroring_state as 'MirroringState'                       --INTO master.[dbo].[databasedetails]                       FROM                       sys.sysdatabases d INNER JOIN sys.sysaltfiles s                       ON                       d.dbID=s.dbID                       INNER JOIN sys.database_mirroring m                       ON                       d.dbID=m.database_ID                       ORDER BY                       d.name                                             --Step 7 :printing Backup details                        Select distinct                              b.machine_name as 'Servername',                        b.server_name as 'Instancename',                        b.database_name as 'Databasename',                            d.database_ID 'DBID',                            CASE b.[type]                                   WHEN 'D' THEN 'Full'                                   WHEN 'I' THEN 'Differential'                                   WHEN 'L' THEN 'Transaction Log'                                   END as 'BackupType'                                  --INTO [dbo].[backupdetails]                         from sys.databases d inner join msdb.dbo.backupset b                             On b.database_name =d.name                         End else begin --Step 8: If the instance is 2000 this code will be used. declare @registry_key4 nvarchar(100)                         declare @Host_name varchar(100) declare @cpu varchar(3) declare @nodes nvarchar(400) set @nodes =null /* We are not able to trap the node names for sql Server 2000 so far*/ declare @mirroring varchar(15) set @mirroring ='NOT APPliCABLE' /*Mirroring does not exist in sql Server 2000*/ Declare @reg_node1 varchar(100) Declare @reg_node2 varchar(100) Declare @reg_node3 varchar(100) Declare @reg_node4 varchar(100)   SET @reg_node1 = N'Cluster\Nodes\1' SET @reg_node2 = N'Cluster\Nodes\2' SET @reg_node3 = N'Cluster\Nodes\3' SET @reg_node4 = N'Cluster\Nodes\4'   Declare @image_path1 varchar(100) Declare @image_path2 varchar(100) Declare @image_path3 varchar(100) Declare @image_path4 varchar(100) set @image_path1=null set @image_path2=null set @image_path3=null set @image_path4=null Exec master.dbo.xp_regread 'HKEY_LOCAL_MACHINE',@reg_node1,@value_name='Nodename',@value=@image_path1 OUTPUT Exec master.dbo.xp_regread 'HKEY_LOCAL_MACHINE',@reg_node2,@value=@image_path2 OUTPUT Exec master.dbo.xp_regread 'HKEY_LOCAL_MACHINE',@reg_node3,@value=@image_path3 OUTPUT Exec master.dbo.xp_regread 'HKEY_LOCAL_MACHINE',@reg_node4,@value=@image_path4 OUTPUT     IF EXISTS (SELECT * FROM dbo.sysobjects WHERE ID = OBJECT_ID(N'nodes') AND OBJECTPROPERTY(ID,N'IsUsertable') = 1)                         drop table nodes Create table nodes (name varchar (20))  insert into nodes values (@image_path1)  insert into nodes values (@image_path2)  insert into nodes values (@image_path3)  insert into nodes values (@image_path4)  --declare @Out nvarchar(400)                          --declare @value nvarchar (20)  SELECT @Out = COALESCE(@Out+'/','') + name from nodes where name is not null     -- Step 9: Reading registry keys for Number of cpus,errorlogs location and domain. SET @instance_name = coalesce(convert(nvarchar(100),'MSsqlSERVER'); IF @instance_name!='MSsqlSERVER' BEGIN set @system_instance_name=@instance_name set @instance_name='MSsql$'+@instance_name SET @key=N'SYstem\CurrentControlSet\Services\' +@instance_name; SET @registry_key = N'Software\Microsoft\Microsoft sql Server\' + @system_instance_name + '\MSsqlServer\Parameters'; SET @registry_key1 = N'Software\Microsoft\Microsoft sql Server\' + @system_instance_name + '\Setup'; SET @registry_key2 = N'SYstem\CurrentControlSet\Services\Tcpip\Parameters\'; SET @registry_key4 = N'SYstem\CurrentControlSet\Control\Session Manager\Environment' EXEC master.dbo.xp_regread 'HKEY_LOCAL_MACHINE',@value_name='sqlPath',@value=@image_path OUTPUT EXEC master.dbo.xp_regread 'HKEY_LOCAL_MACHINE',@value=@startup_type OUTPUT EXEC master.dbo.xp_regread 'HKEY_LOCAL_MACHINE',@value=@start_username OUTPUT EXEC master.dbo.xp_regread 'HKEY_LOCAL_MACHINE',@value=@log_directory OUTPUT EXEC master.dbo.xp_regread 'HKEY_LOCAL_MACHINE',@value=@domain OUTPUT EXEC master.dbo.xp_regread 'HKEY_LOCAL_MACHINE',@registry_key4,@value_name='NUMBER_OF_PROCESSORS',@value=@cpu OUTPUT                         END IF @instance_name='MSsqlSERVER' BEGIN SET @key=N'SYstem\CurrentControlSet\Services\' +@instance_name; SET @registry_key = N'Software\Microsoft\MSsqlSERVER\MSsqlServer\Parameters'; SET @registry_key1 = N'Software\Microsoft\MSsqlSERVER\Setup'; SET @registry_key2 = N'SYstem\CurrentControlSet\Services\Tcpip\Parameters\'; SET @registry_key4 = N'SYstem\CurrentControlSet\Control\Session Manager\Environment'                                                EXEC master.dbo.xp_regread 'HKEY_LOCAL_MACHINE',@value=@log_directory OUTPUT --EXEC master.dbo.xp_regread 'HKEY_LOCAL_MACHINE',@value=@IpAddress OUTPUT EXEC master.dbo.xp_regread 'HKEY_LOCAL_MACHINE',@value=@cpu OUTPUT                         END set @startuptype= (select 'Start Up Mode' = CASE WHEN @startup_type=2 then 'autoMATIC' WHEN @startup_type=3 then 'MANUAL' WHEN @startup_type=4 then 'Disabled' END) --Step 10 : Using ipconfig and xp_msver to get physical memory and IP IF EXISTS (SELECT * FROM dbo.sysobjects WHERE ID = OBJECT_ID(N'tmp') AND OBJECTPROPERTY(ID,N'IsUsertable') = 1)                       DROP table tmp create table tmp (server varchar(100)default cast( serverproperty ('Machinename') as varchar),[index] int,name sysname,internal_value int,character_value varchar(30)) insert into tmp([index],name,internal_value,character_value) exec xp_msver PhysicalMemory IF EXISTS (SELECT * FROM dbo.sysobjects WHERE ID = OBJECT_ID(N'ipadd') AND OBJECTPROPERTY(ID,N'IsUsertable') = 1)                       drop table ipadd create table ipadd (server varchar(100)default cast( serverproperty ('Machinename') as varchar),IP varchar (100)) insert into ipadd (IP)exec xp_cmdshell 'ipconfig' delete from ipadd where ip not like '%IP Address.%' or IP is null -- Step 11 : Getting the Server details  SELECT  top 1               @domain as 'Domain',                      serverproperty('Machinename') as 'Machinename',                      @cpu as 'cpuCount',cast (t.internal_value as bigint) as PhysicalMemoryMB,cast(substring ( I.IP,44,41) as nvarchar(20))as IP_Address,serverproperty('Instancename') as 'Instancename',                      @image_path as 'BinarIEsPath',                      @Out as 'ClustreNodes',serverproperty('IsFullTextInstalled') as 'ISFullText'                        From tmp t inner join IPAdd I on t.server = I.server --Step 12 : Getting the instance details  SELECT                        serverproperty ('Machinename') as 'Machine',                      serverproperty ('Instancename') as 'Instancename',                      (SELECT 'file_type' =                       CASE                       WHEN s.groupID <> 0 THEN 'data'                       WHEN s.groupID = 0 THEN 'log'                       END) AS 'fileType'                      ,s.filename AS 'Physicalfilename'                      ,(s.size * 8 / 1024) AS 'fileSizeMB' -- file size in MB                      ,d.cmptlevel as 'CompatibilityLevel'                      ,'Recovery') as 'RecoveryModel'                      ,                     (SELECT 'is_replication' =                       CASE                       WHEN d.category = 1 THEN 'Published'                       WHEN d.category = 2 THEN 'subscribed'                       WHEN d.category = 4 THEN 'Merge published' WHEN d.category = 8 THEN 'merge subscribed' Else 'NO replication'  END) AS 'Is_replication', @Mirroring as 'MirroringState' FROM                       sysdatabases d INNER JOIN sysaltfiles s                       ON                       d.dbID=s.dbID                       ORDER BY                       d.name                       --Step 13 : Getting backup details  Select distinct                              b.machine_name as 'Servername',                            d.dbID 'DBID',                            CASE b.[type]                                   WHEN 'D' THEN 'Full'                                   WHEN 'I' THEN 'Differential'                                   WHEN 'L' THEN 'Transaction Log'                                   END as 'BackupType'                                  from sysdatabases d inner join msdb.dbo.backupset b                             On b.database_name =d.name    --Step 14: DropPing the table we created for IP and Physical memory Drop table TMP Drop table IPADD drop table Nodes end go --Step 15 : Setting Nulls and Quoted IDentifIEr back to Off  SET ANSI_NulLS OFF GO SET QUOTED_IDENTIFIER OFF GO print '*********************************' print '     ' print '     ' print '     ' print '     ' print '----------------------------' print ' 1.sqlserver all information  ' print '----------------------------' print '                             ' print '*********************************' use master go print 'ths information about sqlserver ' print '                                ' print @@version go print '                                                   ' print '                                                   ' select cast(serverproperty('productversion') as varchar(30)) as 产品版本号,cast(serverproperty('productlevel') as varchar(30)) as sp_level,cast(serverproperty('edition') as varchar(30)) as 版本 go print '                                                   ' print '                                                   ' print 'sqlSERVER max user connect         ' print '                                   ' print @@max_connections go print '*********************************' print '                             ' print '                             ' print '                             ' print '----------------------------' print '2.查看服务器有哪些数据库' print '----------------------------' print '*********************************' print '                           ' SELECT Databasename,DataSize,LogSize,DataSize+LogSize AS TotalSize,Collation,RecoveryType,autoClose,autoShrink FROM  (SELECT DBID,CASE Sum(size*8/1024)  WHEN 0 THEN 1  ELSE Sum(size*8/1024)  END AS DataSize FROM master..sysaltfiles WHERE GroupID <> 0 GROUP BY DBID) q1 INNER JOIN (SELECT DBID,CASE Sum(size*8/1024)  WHEN 0 THEN 1  ELSE Sum(size*8/1024) END AS LogSize FROM master..sysaltfiles WHERE GroupID = 0 GROUP BY DBID) q2  ON q1.DBID = q2.DBID INNER JOIN (SELECT DBID,[name] AS Databasename,CONVERT(varchar(100),DATABASEPROPERTYEX([name],'Collation')) AS Collation,'Recovery')) AS RecoveryType,CASE CONVERT(varchar(10),'IsautoClose')) WHEN 0 THEN '-' WHEN 1 THEN 'Yes' END  AS autoClose,  CASE CONVERT(varchar(10),'IsautoShrink')) WHEN 0 THEN '-' WHEN 1 THEN 'Yes' END AS autoShrink FROM master.dbo.sysdatabases) q3 ON q1.DBID = q3.dbID ORDER BY Databasename  print '*********************************' print '                             ' print '                             ' print '                             ' print '----------------------------' print '3.查看每个数据库具体结构信息' print'----------------------------' print '*********************************' print '                         ' use master go declare @databasename varchar(50) declare cur01 cursor for select name from sys.databases open cur01 fetch next from cur01 into @databasename while @@fetch_status=0 begin begin select 'databasename:  '+ @databasename print '                     ' exec sp_helpdb @databasename end fetch next from cur01 into @databasename; end; close cur01 deallocate cur01 go print '*********************************' print '                             ' print '                             ' print '                             ' print '----------------------------' print '4.1所有数据库的index          ' print '----------------------------' print '*********************************' use master go BEGIN CREATE table #INDEXFRAGINFO ( Databasename nvarchar(128),DatabaseID smallint,full_obj_name nvarchar(384),index_ID INT,  [name] nvarchar(128),  index_type_desc nvarchar(60),  index_depth tinyint,index_level tinyint,[AVG Fragmentation] float,  fragment_count bigint,[Rank] bigint  ) DECLARE @command VARCHAR(1000)  SELECT @command = 'Use [' + '?' + '] select ' + '''' + '?' + '''' + ' AS Databasename,DB_ID() AS DatabaseID,QUOTEname(DB_name(i.database_ID),'+ '''' + '"' + '''' +')+ N'+ '''' + '.' + '''' +'+ QUOTEname(OBJECT_SCHEMA_name(i.object_ID,i.database_ID),'+ '''' + '"' + '''' +')+ N'+ '''' + '.' + '''' +'+ QUOTEname(OBJECT_name(i.object_ID,'+ '''' + '"' + '''' +') as full_obj_name,  i.index_ID,o.name,  i.index_type_desc,  i.index_depth,i.index_level,i.avg_fragmentation_in_percent as [AVG Fragmentation],  i.fragment_count,  i.rnk as Rank from ( select *,DENSE_RANK() OVER(PARTITION by database_ID ORDER BY avg_fragmentation_in_percent DESC) as rnk from sys.dm_db_index_physical_stats(DB_ID(),default,'+ '''' + 'limited' + '''' +') where avg_fragmentation_in_percent >0 AND  INDEX_ID > 0 AND  Page_Count > 500  ) as i join sys.indexes o on o.object_ID = i.object_ID and o.index_ID = i.index_ID where i.rnk <= 25 order by i.database_ID,i.rnk;' INSERT #INDEXFRAGINFO EXEC sp_MSForEachDB @command  SELECT substring(databasename,30) as databasename,ltrim(databaseID) as databaseID,substring(full_obj_name,50) as full_obj_name,ltrim(index_ID) as index_ID,[name],  index_type_desc,ltrim(index_depth) as index_depth,ltrim(index_level) as index_level,ltrim([AVG Fragmentation]) as [AVG Fragmentation], ltrim(fragment_count) as fragment_count,ltrim([Rank]) as [Rank] FROM #INDEXFRAGINFO Where DatabaseID > 4 order by [RANK]; DROP table #INDEXFRAGINFO END GO print '*********************************' print '                             ' print '                             ' print '                             ' print '----------------------------' print '4.2找出很少使用的index         ' print '----------------------------' print '*********************************' declare @dbID int select @dbID = db_ID() select objectname=object_name(s.object_ID),s.object_ID,indexname=i.name,i.index_ID,user_seeks AS 搜索次数,user_scans AS 扫描次数,  user_lookups AS 查找次数,user_updates 更新次数 from sys.dm_db_index_usage_stats s,sys.indexes i where database_ID = @dbID  and objectproperty(s.object_ID,'IsUsertable') = 1 and i.object_ID = s.object_ID and i.index_ID = s.index_ID order by (user_seeks + user_scans + user_lookups + user_updates) asc go print '                             ' print '                             ' print '                             ' print '----------------------------' print '4.3所有数据库未使用的索引  ' print '----------------------------' print '*********************************' SELECT top 1         Databasename = DB_name()        ,tablename = OBJECT_name(s.[object_ID])        ,Indexname = i.name        ,user_updates            ,system_updates             -- Useful fIElds below:         --,* INTO #Tempunusedindexes FROM   sys.dm_db_index_usage_stats s  INNER JOIN sys.indexes i ON  s.[object_ID] = i.[object_ID]      AND s.index_ID = i.index_ID  WHERE  s.database_ID = DB_ID()     AND OBJECTPROPERTY(s.[object_ID],'IsMsShipped') = 0     AND    user_seeks = 0     AND user_scans = 0      AND user_lookups = 0     AND s.[object_ID] = -999  -- Dummy value to get table structure. ; -- Loop around all the databases on the server. EXEC sp_MSForEachDB    'USE [?];  -- table already exists. INSERT INTO #Tempunusedindexes  SELECT top 10             Databasename = DB_name()        ,system_updates     FROM   sys.dm_db_index_usage_stats s  INNER JOIN sys.indexes i ON  s.[object_ID] = i.[object_ID]      AND s.index_ID = i.index_ID  WHERE  s.database_ID = DB_ID()     AND OBJECTPROPERTY(s.[object_ID],''IsMsShipped'') = 0     AND    user_seeks = 0     AND user_scans = 0      AND user_lookups = 0     AND i.name IS NOT NulL    -- Ignore HEAP indexes. ORDER BY user_updates DESC ; ' -- Select records. SELECT top 10 * FROM #Tempunusedindexes ORDER BY [user_updates] DESC -- TIDy up. DROP table #Tempunusedindexes print '                             ' print '                             ' print '                             ' print '----------------------------' print '4.4所有数据库高开销的缺失索引  ' print '----------------------------' print '*********************************' SELECT  top 10          [Total Cost]  = ROUND(avg_total_user_cost * avg_user_impact * (user_seeks + user_scans),0)         ,avg_user_impact        ,tablename = statement        ,[EqualityUsage] = equality_columns         ,[InequalityUsage] = inequality_columns        ,[Include Cloumns] = included_columns FROM        sys.dm_db_missing_index_groups g  INNER JOIN    sys.dm_db_missing_index_group_stats s         ON s.group_handle = g.index_group_handle  INNER JOIN    sys.dm_db_missing_index_details d         ON d.index_handle = g.index_handle ORDER BY [Total Cost] DESC; print '                             ' print '                             ' print '                             ' print '----------------------------' print '5.查询数据库IO                 ' print '----------------------------' print '*********************************' go WITH IOFORDATABASE AS ( SELECT  DB_name(VFS.database_ID) AS Databasename,CASE WHEN smf.type = 1 THEN 'LOG_file' ELSE 'DATA_file' END AS Databasefile_Type,SUM(VFS.num_of_bytes_written) AS IO_Write,SUM(VFS.num_of_bytes_read) AS IO_Read,SUM(VFS.num_of_bytes_read + VFS.num_of_bytes_written) AS Total_IO,SUM(VFS.io_stall) AS IO_STALL FROM sys.dm_io_virtual_file_stats(NulL,NulL) AS VFS JOIN sys.master_files AS smf   ON VFS.database_ID = smf.database_ID  AND VFS.file_ID = smf.file_ID GROUP BY   DB_name(VFS.database_ID),smf.type ) SELECT   ltrim(ROW_NUMBER() OVER(ORDER BY io_stall DESC)) AS RowNumber,substring(Databasename,30) as Databasename,Databasefile_Type,ltrim(CAST(1.0 * IO_Read/ (1024 * 1024) AS DECIMAL(12,2))) AS IO_Read_MB,ltrim(CAST(1.0 * IO_Write/ (1024 * 1024) AS DECIMAL(12,2))) AS IO_Write_MB,ltrim(CAST(1. * Total_IO / (1024 * 1024) AS DECIMAL(12,2))) AS IO_TOTAL_MB,ltrim(CAST(IO_STALL / 1000. AS DECIMAL(12,2))) AS IO_STALL_Seconds,ltrim(CAST(100. * IO_STALL / SUM(IO_STALL) OVER() AS DECIMAL(10,2))) AS IO_STALL_Pct FROM IOFORDATABASE ORDER BY IO_STALL_Seconds DESC; go print '*********************************' print '                             ' print '                             ' print '                             ' print '----------------------------' print '6.查看数据库是否有死锁         ' print '----------------------------' print '*********************************' use master go select ltrim(request_session_ID) "会话ID",substring(resource_type,30) "被锁定的资源",resource_database_ID "数据库",object_name(resource_associated_entity_ID) "对象",request_mode "资源模式",request_status  "锁状态" from sys.dm_tran_locks go print '*********************************' print '                             ' print '                             ' print '                             ' print '----------------------------' print '7.查看性能统计信息             ' print'----------------------------' print '*********************************' use master go dbcc freeproccache go select t.text as "执行的文本",st.total_logical_reads  as "逻辑读取总次数",st.total_physical_reads  as "物理读取总次数",st.total_elapsed_time/1000000 as "占用的总时间",st.total_logical_writes  as "逻辑写入总次数" from sys.dm_exec_query_stats st cross apply sys.dm_exec_sql_text(st.sql_handle) t go print '*********************************' print '                             ' print '                             ' print '                             ' print '----------------------------' print '8.临时数据库使用情况              ' print '----------------------------' print '*********************************' use master go select ltrim(sum(user_object_reserved_page_count)*8) as user_objects_kb,   ltrim(sum(internal_object_reserved_page_count)*8) as internal_objects_kb,   ltrim(sum(version_store_reserved_page_count)*8) as version_store_kb,   ltrim(sum(unallocated_extent_page_count)*8) as freespace_kb from sys.dm_db_file_space_usage where database_ID = 2 go print '*********************************' print '                             ' print '                             ' print '                             ' print '----------------------------' print '9.查cpu瓶颈                    ' print '----------------------------' print '*********************************' use master go Select ltrim(scheduler_ID) as scheduler_ID,ltrim(current_tasks_count) as current_tasks_count,ltrim(runnable_tasks_count) as current_tasks_count from sys.dm_os_schedulers where scheduler_ID<255  go print '*********************************' print '                             ' print '                             ' print '                             ' print '------------------------------------------' print '10.当前被缓存的消耗cpu资源最多的批处理或者过程' print '------------------------------------------' print '*********************************' use master go Select top 50 ltrim(sum(total_worker_time)) as total_cpu_time,ltrim(sum(execution_count)) as total_execution_count,  ltrim(count(*)) as number_of_statements,plan_handle  from sys.dm_exec_query_stats qs group by  plan_handle order by sum(total_worker_time) desc  go print '*********************************' print '                             ' print '                             ' print '                             ' print '--------------------------------------------' print '11.查询前100个缓存使用率高、最消耗缓存的SQL语句' print '--------------------------------------------' print '*********************************' use master go select top 100 ltrim(usecounts) as usecounts,objtype,ltrim(p.size_in_bytes) as  size_in_bytes,sql.text from sys.dm_exec_cached_plans  p outer apply sys.dm_exec_sql_text(p.plan_handle) sql order by usecounts desc go print '*********************************' print '                             ' print '                             ' print '                             ' print '---------------------------------------------------------------' print '12.解数据库中的缓存情况,包括被使用的次数、缓存类型、占用的内存大小' print '---------------------------------------------------------------' print '*********************************' use master go SELECT usecounts=ltrim(usecounts),substring(cacheobjtype,30) as cacheobjtype,ltrim(size_in_bytes) as size_in_bytes,plan_handle FROM sys.dm_exec_cached_plans go print '*********************************' print '                             ' print '                             ' print '                             ' print '----------------------------' print '13.计划缓存总数' print '----------------------------' print '*********************************' use master go Select ltrim(Count(*)) CNT,cast(sum(size_in_bytes)/1024/1024 as varchar(100))+' MB' TotalSize From sys.dm_exec_cached_plans go print '*********************************' print '                             ' print '                             ' print '                             ' print '--------------------------------------' print '14.检查sql Server的执行缓存和数据缓存占用' print '--------------------------------------' print '*********************************' use master go dbcc memorystatus  go print '*********************************' print '                             ' print '                             ' print '                             ' print '-------------------------------------' print '15.所有数据库备份情况' print '-------------------------------------' print '*********************************' -- sql server 2000/2005 version use master go set nocount on go declare @counter smallint declare @dbname varchar(100) declare @db_bkpdate varchar(100) declare @status varchar(20) declare @svr_name varchar(100) declare @media_set_ID varchar(20) declare @filepath varchar(1000) declare @filestatus int declare @fileavailable varchar(20) declare @backupsize float select @counter=max(dbID) from master..sysdatabases create table #backup_details  ( servername varchar(100),databasename varchar(100),bkpdate varchar(20) null,backupsize_in_mb varchar(20),status varchar(20),filepath varchar(1000),fileavailable varchar(200) ) select @svr_name = cast(serverproperty('servername')as sysname) while @counter > 0 begin /* need to re-initialize all variables*/ select @dbname = null,@db_bkpdate = null,@media_set_ID = null,@backupsize = null,@filepath = null,@filestatus = null,  @fileavailable = null,@status = null,@backupsize = null select @dbname = name from master..sysdatabases where dbID = @counter select @db_bkpdate = max(backup_start_date) from msdb..backupset where database_name = @dbname and type='d' select @media_set_ID = media_set_ID from msdb..backupset where backup_start_date = ( select max(backup_start_date) from msdb..backupset where database_name = @dbname and type='d') select @backupsize = backup_size from msdb..backupset where backup_start_date = ( select max(backup_start_date) from msdb..backupset where database_name = @dbname and type='d') select @filepath = physical_device_name from msdb..backupmediafamily where media_set_ID = @media_set_ID exec master..xp_fileexist @filepath,@filestatus out if @filestatus = 1 set @fileavailable = 'available' else set @fileavailable = 'not available' if (datediff(day,@db_bkpdate,getdate()) > 7) set @status = 'warning' else set @status = 'healthy' set @backupsize = (@backupsize/1024)/1024 insert into #backup_details select @svr_name,@dbname,@backupsize,@status,@filepath,@fileavailable update #backup_details set status = 'warning' where bkpdate is null set @counter = @counter - 1 end select substring(servername,20) AS [服务器名],substring(databasename,20) AS [数据库名],  rtrim(ltrim(bkpdate)) AS  [备份日期],rtrim(ltrim(backupsize_in_mb)) AS [备份大小],rtrim(ltrim([status])) AS [备份状态],substring(rtrim(ltrim(filepath)),40) AS  [备份文件路径],rtrim(fileavailable) AS  [备份文件是否可用]  from #backup_details where databasename not in ('tempdb','northwind','pubs') drop table #backup_details set nocount off go print '*********************************' print '                             ' print '                             ' print '                             ' print '-------------------------------------' print '16.监控cpu瓶颈' print '-------------------------------------' print '*********************************' use master go print '-------------------------------------' print '16.1当前缓存的哪些批处理或过程占用了大部分 cpu 资源' print '-------------------------------------' SELECT top 50        ltrim(SUM(qs.total_worker_time)) AS total_cpu_time,        ltrim(SUM(qs.execution_count)) AS total_execution_count,      ltrim(COUNT(*)) AS  number_of_statements,        qs.sql_handle  FROM sys.dm_exec_query_stats AS qs GROUP BY qs.sql_handle ORDER BY SUM(qs.total_worker_time) DESC go print '                             ' print '                             ' print '-------------------------------------------------------' print '16.2查询显示缓存计划所占用的 cpu 总使用率(带 sql 文本)' print '-------------------------------------------------------' SELECT        total_cpu_time,        total_execution_count,      number_of_statements,      s2.text       --(SELECT SUBSTRING(s2.text,statement_start_offset / 2,((CASE WHEN statement_end_offset = -1 THEN (LEN(CONVERT(NVARCHAR(MAX),s2.text)) * 2) ELSE statement_end_offset END) - statement_start_offset) / 2) ) AS query_text FROM        (SELECT top 50              SUM(qs.total_worker_time) AS total_cpu_time,              SUM(qs.execution_count) AS total_execution_count,            COUNT(*) AS  number_of_statements,              qs.sql_handle --,            --MIN(statement_start_offset) AS statement_start_offset,              --MAX(statement_end_offset) AS statement_end_offset       FROM              sys.dm_exec_query_stats AS qs       GROUP BY qs.sql_handle       ORDER BY SUM(qs.total_worker_time) DESC) AS stats       CROSS APPLY sys.dm_exec_sql_text(stats.sql_handle) AS s2 go print '                             ' print '                             ' print '-------------------------------------------------------' print '16.3显示 cpu 平均占用率最高的前 50 个 sql 语句' print '-------------------------------------------------------' SELECT top 50 total_worker_time/execution_count AS [Avg cpu Time],(SELECT SUBSTRING(text,statement_start_offset/2,(CASE WHEN statement_end_offset = -1 then LEN(CONVERT(nvarchar(max),text)) * 2 ELSE statement_end_offset end -statement_start_offset)/2) FROM sys.dm_exec_sql_text(sql_handle)) AS query_text,* FROM sys.dm_exec_query_stats  ORDER BY [Avg cpu Time] DESC go print '                             ' print '                             ' print '-------------------------------------------------------' print '16.4找出过多编译/重新编译的 DMV 查询' print '-------------------------------------------------------' select * from sys.dm_exec_query_optimizer_info where        counter = 'optimizations'       or counter = 'elapsed time' go print '                             ' print '                             ' print '-------------------------------------------------------' print '16.5显示已重新编译的前 25 个存储过程' print '-------------------------------------------------------' select top 25       sql_text.text,      sql_handle,      plan_generation_num,      execution_count,      dbID,      objectID  from sys.dm_exec_query_stats a       cross apply sys.dm_exec_sql_text(sql_handle) as sql_text where plan_generation_num > 1 order by plan_generation_num desc go print '                             ' print '                             ' print '-------------------------------------------------------' print '16.6哪个查询占用了最多的 cpu 累计使用率' print '-------------------------------------------------------' SELECT      ltrim(highest_cpu_querIEs.plan_handle) as plan_handle,      ltrim(highest_cpu_querIEs.total_worker_time) as total_worker_time,    q.dbID,    ltrim(q.objectID),    q.number,    q.encrypted,    q.[text] from      (select top 50          qs.plan_handle,          qs.total_worker_time     from          sys.dm_exec_query_stats qs     order by qs.total_worker_time desc) as highest_cpu_querIEs     cross apply sys.dm_exec_sql_text(plan_handle) as q order by highest_cpu_querIEs.total_worker_time desc go print '                             ' print '                             ' print '-------------------------------------------------------' print '16.7可能占用大量 cpu 使用率的运算符 print '-------------------------------------------------------' select * from        sys.dm_exec_cached_plans       cross apply sys.dm_exec_query_plan(plan_handle) where        cast(query_plan as nvarchar(max)) like '%sort%'       or cast(query_plan as nvarchar(max)) like '%Hash Match%' go print '                             ' print '                             ' print '-------------------------------------------------------' print '17.内存瓶颈' print '-------------------------------------------------------' print '                             ' print '                             ' print '-------------------------------------------------------' print '17.1确保已启用 sql Server 中的高级选项' print '-------------------------------------------------------' use master go sp_configure 'show advanced options' go sp_configure 'show advanced options',1 go reconfigure go print '                             ' print '                             ' print '-------------------------------------------------------' print '17.2运行以下查询以检查内存相关配置选项' print '-------------------------------------------------------' sp_configure 'awe_enabled' go sp_configure 'min server memory' go sp_configure 'max server memory' go sp_configure 'min memory per query' go sp_configure 'query wait' go print '                             ' print '                             ' print '-------------------------------------------------------' print '17.3查看 cpu、计划程序内存和缓冲池信息' print '-------------------------------------------------------' select  ltrim(cpu_count) as cpu_count,ltrim(hyperthread_ratio) as hyperthread_ratio,ltrim(scheduler_count) as scheduler_count,ltrim(physical_memory_in_bytes / 1024 / 1024) as physical_memory_mb,ltrim(virtual_memory_in_bytes / 1024 / 1024) as virtual_memory_mb,ltrim(bpool_committed * 8 / 1024) as bpool_committed_mb,ltrim(bpool_commit_target * 8 / 1024) as bpool_target_mb,ltrim(bpool_visible * 8 / 1024) as bpool_visible_mb from sys.dm_os_sys_info go print '                             ' print '                             ' print '-------------------------------------------------------' print '17.4I/O 瓶颈' print '-------------------------------------------------------' select wait_type,ltrim(waiting_tasks_count) as waiting_tasks_count,ltrim(wait_time_ms) as wait_time_ms,  ltrim(signal_wait_time_ms) as signal_wait_time_ms,ltrim(wait_time_ms / waiting_tasks_count) as avgtime from sys.dm_os_wait_stats   where wait_type like 'PAGEIolATCH%'  and waiting_tasks_count > 0 order by wait_type go print '                             ' print '                             ' print '-------------------------------------------------------' print '17.5查找当前挂起的 I/O 请求' print '-------------------------------------------------------' print '正常情况下不返回任何值' select      database_ID,      file_ID,      io_stall,    io_pending_ms_ticks,    scheduler_address  from  sys.dm_io_virtual_file_stats(NulL,NulL)t1,        sys.dm_io_pending_io_requests as t2 where t1.file_handle = t2.io_handle go print '                             ' print '                             ' print '-------------------------------------------------------' print '17.6查看IO相关查询读取次数' print '-------------------------------------------------------' select top 5 (total_logical_reads/execution_count) as avg_logical_reads,                   (total_logical_writes/execution_count) as avg_logical_writes,           (total_physical_reads/execution_count) as avg_physical_reads,           Execution_count,statement_start_offset,p.query_plan,q.text from sys.dm_exec_query_stats       cross apply sys.dm_exec_query_plan(plan_handle) p       cross apply sys.dm_exec_sql_text(plan_handle) as q order by (total_logical_reads + total_logical_writes)/execution_count Desc go print '                             ' print '                             ' print '-------------------------------------------------------' print '17.7查找哪些批处理/请求生成的 I/O 最多' print '-------------------------------------------------------' select top 5      ltrim(total_logical_reads/execution_count) as avg_logical_reads,    ltrim(total_logical_writes/execution_count) as avg_logical_writes,    ltrim(total_physical_reads/execution_count) as avg_phys_reads,     ltrim(Execution_count) as Execution_count,      ltrim(statement_start_offset) as stmt_start_offset,      sql_handle,      plan_handle from sys.dm_exec_query_stats   order by  (total_logical_reads + total_logical_writes) Desc go print '                             ' print '                             ' print '-------------------------------------------------------' print '18.阻塞' print '-------------------------------------------------------' print '                             ' print '                             ' print '-------------------------------------------------------' print '18.1 确定阻塞的会话' print '-------------------------------------------------------' use master go select blocking_session_ID,wait_duration_ms,session_ID from  sys.dm_os_waiting_tasks where blocking_session_ID is not null go print '                             ' print '                             ' print '-------------------------------------------------------' print '18.2 sql 等待分析和前 10 个等待的资源' print '-------------------------------------------------------' select top 10 ltrim(wait_type) as wait_type, ltrim(wait_time_ms) as  wait_time_ms,    ltrim(max_wait_time_ms) as  max_wait_time_ms, ltrim(signal_wait_time_ms) as signal_wait_time_ms from sys.dm_os_wait_stats --where wait_type not in ('CLR_SEMAPHORE','LAZYWRITER_SLEEP','RESOURCE_QUEUE','SLEEP_TASK','SLEEP_SYstemTASK','WAITFOR') order by wait_time_ms desc go print '                             ' print '                             ' print '-------------------------------------------------------' print '19. 查看各个数据库性能负载' print '-------------------------------------------------------' SELECT substring (a.name,12) as [数据库名],[连接数] = (SELECT COUNT(*) FROM master..sysprocesses b WHERE a.dbID = b.dbID),[阻塞进程] = (SELECT COUNT(*) FROM master..sysprocesses b WHERE a.dbID = b.dbID AND blocked <> 0),[总内存] = ISNulL((SELECT SUM(memusage) FROM master..sysprocesses b WHERE a.dbID = b.dbID),0),[总IO] = ISNulL((SELECT SUM(physical_io) FROM master..sysprocesses b WHERE a.dbID = b.dbID),[总cpu] = ISNulL((SELECT SUM(cpu) FROM master..sysprocesses b WHERE a.dbID = b.dbID),[总等待时间] = ISNulL((SELECT SUM(waittime) FROM master..sysprocesses b WHERE a.dbID = b.dbID),[SELECTs] = (SELECT COUNT(*)  FROM master..sysprocesses b WHERE  a.dbID = b.dbID AND b.cmd liKE '%sELECT%'),[DELETEs] = (SELECT COUNT(*)  FROM master..sysprocesses b WHERE  a.dbID = b.dbID AND b.cmd liKE '%DELETE%'),[DBCC Commands] = ISNulL((SELECT COUNT(*) FROM master..sysprocesses b WHERE a.dbID = b.dbID and b.cmd like '%DBCC%'),[BCP Running] = ISNulL((SELECT COUNT(*) FROM master..sysprocesses b WHERE a.dbID = b.dbID and b.cmd like '%BCP%'),[Backups Running] = ISNulL((SELECT COUNT(*) FROM master..sysprocesses b WHERE a.dbID = b.dbID and b.cmd liKE '%BACKUP%'),0) FROM master.dbo.sysdatabases a WITH (nolock) WHERE  DatabasePropertyEx(a.name,'Status') = 'ONliNE' ORDER BY [数据库名] go print '                             ' print '                             ' print '-------------------------------------------------------' print '20. 查看所有数据库大小、恢复模式等信息' print '-------------------------------------------------------' SELECT substring (Databasename,12) as Databasename,'IsautoShrink')) WHEN 0 THEN '-' WHEN 1 THEN 'Yes' END AS autoShrink FROM master.dbo.sysdatabases) q3 ON q1.DBID = q3.dbID ORDER BY Databasename  print '                             ' print '                             ' print '-------------------------------------------------------' print '21. 查看数据库群集信息' print '-------------------------------------------------------' PRINT ' **** Cluster information ****' PRINT ' ' PRINT ' The following is information on the cluster you are connected' PRINT ' ' PRINT '... name of all nodes used and are part of this failover cluster' SELECT * FROM sys.dm_os_cluster_nodes  PRINT ' ' PRINT '... Node which is the active ' SELECT SERVERPROPERTY('ComputernamePhysicalNetBIOS')  PRINT ' ' PRINT '... Drive letters that are part of the resourse group which contain the data and log files' SELECT * FROM sys.dm_io_cluster_shared_drives go print '                             ' print '                             ' print '-------------------------------------------------------' print '22. 当前数据库服务器登录用户、会话连接数、认证类型' print '-------------------------------------------------------' SELECT '认证方式'=( CASE  WHEN nt_user_name IS not null THEN 'windows认证'  ELSE 'sql认证'  END),login_name AS '登录名',ISNulL(nt_user_name,'-') AS 'windows登录名',COUNT(session_ID) AS '会话数' FROM sys.dm_exec_sessions GROUP BY login_name,nt_user_name go print '                             ' print '                             ' print '-------------------------------------------------------' print '23. 查看执行效率低的语句' print '-------------------------------------------------------' SELECT creation_time  N'语句编译时间'        ,last_execution_time  N'上次执行时间'        ,total_physical_reads N'物理读取总次数'        ,total_logical_reads/execution_count N'每次逻辑读次数'        ,total_logical_reads  N'逻辑读取总次数'        ,total_logical_writes N'逻辑写入总次数'        ,execution_count  N'执行次数'        ,total_worker_time/1000 N'所用的cpu总时间ms'        ,total_elapsed_time/1000  N'总花费时间ms'        ,(total_elapsed_time / execution_count)/1000  N'平均时间ms'        ,SUBSTRING(st.text,(qs.statement_start_offset/2) + 1,         ((CASE statement_end_offset           WHEN -1 THEN DATALENGTH(st.text)           ELSE qs.statement_end_offset END             - qs.statement_start_offset)/2) + 1) N'执行语句' FROM sys.dm_exec_query_stats AS qs CROSS APPLY sys.dm_exec_sql_text(qs.sql_handle) st where SUBSTRING(st.text,         ((CASE statement_end_offset           WHEN -1 THEN DATALENGTH(st.text)           ELSE qs.statement_end_offset END             - qs.statement_start_offset)/2) + 1) not like '%fetch%' ORDER BY  total_elapsed_time / execution_count DESC; print '                             ' print '                             ' print '-------------------------------------------------------' print '24. 所有数据库高开销的缺失索引' print '-------------------------------------------------------' SELECT  top 100          [Total Cost]  = ROUND(avg_total_user_cost * avg_user_impact * (user_seeks + user_scans),[Include Cloumns] = included_columns FROM        sys.dm_db_missing_index_groups g  INNER JOIN    sys.dm_db_missing_index_group_stats s         ON s.group_handle = g.index_group_handle  INNER JOIN    sys.dm_db_missing_index_details d         ON d.index_handle = g.index_handle ORDER BY [Total Cost] DESC; print '                             ' print '                             ' print '-------------------------------------------------------' print '25. 查看buffer cache命中率' print '-------------------------------------------------------'  SELECT   (CAST(SUM(CASE LTRIM(RTRIM(counter_name))     WHEN 'Buffer cache hit ratio'     THEN CAST(cntr_value AS INTEGER) ELSE NulL END) AS float) /    CAST(SUM(CASE LTRIM(RTRIM(counter_name))     WHEN 'Buffer cache hit ratio base' THEN CAST(cntr_value AS INTEGER)ELSE NulL END) AS float)) * 100    AS BufferCacheHitRatio    FROM sys.dm_os_performance_counters     WHERE LTRIM(RTRIM([object_name])) liKE '%:Buffer Manager' AND      [counter_name] liKE 'Buffer Cache Hit Ratio%'   go print '                             ' print '                             ' print '-------------------------------------------------------' print '26. 查看job执行情况' print '-------------------------------------------------------' SELECT 作业名     = sj.name       ,开始时间 = sja.start_execution_date       ,结束时间   = sja.stop_execution_date       ,状态    = CASE                      WHEN ISNulL(sjh.run_status,-1) = -1 AND sja.start_execution_date IS NulL AND sja.stop_execution_date IS NulL THEN 'IDle'                     WHEN ISNulL(sjh.run_status,-1) = -1 AND sja.start_execution_date IS NOT NulL AND sja.stop_execution_date IS NulL THEN 'Running'                     WHEN ISNulL(sjh.run_status,-1) =0  THEN 'Failed'                     WHEN ISNulL(sjh.run_status,-1) =1  THEN 'Succeeded'                     WHEN ISNulL(sjh.run_status,-1) =2  THEN 'Retry'                     WHEN ISNulL(sjh.run_status,-1) =3  THEN 'Canceled'                     END    FROM MSDB.DBO.sysjobs sj    JOIN MSDB.DBO.sysjobactivity sja      ON sj.job_ID = sja.job_ID     JOIN (SELECT MaxSessionID = MAX(Session_ID) FROM MSDB.DBO.syssessions) ss      ON ss.MaxSessionID = sja.session_ID  left JOIN MSDB.DBO.sysjobhistory sjh      ON sjh.instance_ID = sja.job_history_ID print '                             ' print '                             ' print '-------------------------------------------------------' print '27. 获得每个数据库空间使用情况' print '-------------------------------------------------------' CREATE table #output(  server_name varchar(128),  dbname varchar(128),  physical_name varchar(260),  dt datetime,  file_group_name varchar(128),  size_mb int,  free_mb int)     exec sp_MSforeachdb @command1=  'USE [?]; INSERT #output  SELECT CAST(SERVERPROPERTY(''Servername'') AS varchar(128)) AS server_name,  ''?'' AS dbname,  f.filename AS physical_name,  CAST(FLOOR(CAST(getdate() AS float)) AS datetime) AS dt,  g.groupname,  CAST (size*8.0/1024.0 AS int) AS ''size_mb'',  CAST((size - filePROPERTY(f.name,''SpaceUsed''))*8.0/1024.0 AS int) AS ''free_mb''  FROM sysfiles f  JOIN sysfilegroups g  ON f.groupID = g.groupID'    SELECT * FROM #output  drop table #output print '                             ' print '                             ' print '-------------------------------------------------------' print '28.Buffer Pool缓冲池里面修改过的页总数大小' print '-------------------------------------------------------' SELECT count(*) * 8/1024 as cached_pages_mb,convert(varchar(5),convert(decimal(5,2),(100-1.0*(select count(*) from sys.dm_os_buffer_descriptors b  where b.database_ID=a.database_ID and is_modifIEd=0)/count(*)*100.0)))+'%'modifIEd_percentage,CASE database_ID WHEN 32767 THEN 'ResourceDb' ELSE db_name(database_ID) END AS Database_name FROM sys.dm_os_buffer_descriptors a GROUP BY db_name(database_ID),database_ID ORDER BY cached_pages_mb DESC; /* 如果一个数据库的大部分(超过80%)是修改过的,那么这个数据库写 *** 作非常多。 反之如果这个比例接近0,那么该数据库的活动几乎是只读的。读写的比例对磁盘的安排是很重要的。 */ print '                             ' print '                             ' print '-------------------------------------------------------' print '29.查看 tempdb 大小和增长参数' print '-------------------------------------------------------' SELECT      name AS filename,      size*1.0/128 AS fileSizeinMB,    CASE max_size          WHEN 0 THEN 'autogrowth is off.'         WHEN -1 THEN 'autogrowth is on.'         ELSE 'Log file will grow to a maximum size of 2 TB.'     END,    growth AS 'GrowthValue',    'GrowthIncrement' =          CASE             WHEN growth = 0 THEN 'Size is fixed and will not grow.'             WHEN growth > 0 AND is_percent_growth = 0                  THEN 'Growth value is in 8-KB pages.'             ELSE 'Growth value is a percentage.'         END FROM tempdb.sys.database_files; GO print '                             ' print '                             ' print '-------------------------------------------------------' print '30.查看 客户端连接IP' print '-------------------------------------------------------' SELECT distinct clIEnt_net_address FROM sys.dm_exec_connections  WHERE session_ID >50 and session_ID != @@SPID and clIEnt_net_address  not like '%<local machine>%' go print '                             ' print '                             ' print '-------------------------------------------------------' print '31.查看消耗性能的存储过程名、存储过程内容' print '-------------------------------------------------------' select distinct procname,text,b.cached_time,b.last_execution_time,b.total_elapsed_time,b.avg_elapsed_time,b.last_elapsed_time,b.execution_count  from ( select top 1000 sql_text.text as text,sql_handle,plan_generation_num,execution_count,dbID,objectID from sys.dm_exec_query_stats a cross apply sys.dm_exec_sql_text(sql_handle) as sql_text where plan_generation_num > 1 order by plan_generation_num desc )  a,( SELECT top 1000 d.object_ID,d.database_ID,OBJECT_name(object_ID,database_ID) 'procname',  d.cached_time,d.last_execution_time,d.total_elapsed_time,d.total_elapsed_time/d.execution_count AS [avg_elapsed_time],d.last_elapsed_time,d.execution_count FROM sys.dm_exec_procedure_stats AS d ORDER BY [total_worker_time] DESC ) b where a.objectID=b.object_ID order by avg_elapsed_time,execution_count desc go 总结

以上是内存溢出为你收集整理的实战:sqlserver 日常检查脚本全部内容,希望文章能够帮你解决实战:sqlserver 日常检查脚本所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/sjk/1179398.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-06-02
下一篇 2022-06-02

发表评论

登录后才能评论

评论列表(0条)

保存