Mirror-TSQL에서 미러 상태 및 미러 관련 스토리지 프로세스를 보는 방법

28308 단어 저장 프로세스
--====================================================
--      
SELECT
DB_NAME(database_id) AS DatabaseName,
dm.mirroring_role AS MirroringRole,
(CASE dm.mirroring_role
   WHEN 1 THEN '  '
   WHEN 2 THEN '  '
   END) AS MirroringRoleDesc,
dm.mirroring_partner_name AS MirroringPartnerName,
(CASE WHEN dm.mirroring_witness_name IS NULL
   THEN '--'
   ELSE dm.mirroring_witness_name
END)AS MirroringWitnessName,
dm.mirroring_state AS MirroringState,
(CASE dm.mirroring_state
   WHEN 0 THEN '   '
   WHEN 1 THEN '       '
   WHEN 2 THEN '    '
   WHEN 3 THEN '      '
   WHEN 4 THEN '   '
   WHEN 5 THEN '     '
   WHEN 6 THEN '     '
   WHEN NULL THEN '   '
END) AS MirroringStateDesc,
dm.mirroring_safety_level AS MirroringSafetyLevel,
(CASE dm.mirroring_safety_level
   WHEN 0 THEN '  '
   WHEN 1 THEN '  '
   WHEN 2 THEN '  '
   WHEN NULL THEN '   '
END) AS MirroringSafetyLevelDesc,
dm.mirroring_witness_state AS MirroringWitnessState,
(CASE dm.mirroring_witness_state
   WHEN 0 THEN '    '
   WHEN 1 THEN '    '
   WHEN 2 THEN '    '
   WHEN NULL THEN '   '
END) AS MirroringWitnessStateDesc
FROM sys.database_mirroring dm
WHERE dm.mirroring_guid IS NOT NULL
 
--============================================================
--         
sp_dbmmonitorresults database_name
   , rows_to_return
    , update_status
 
database_name
             。
rows_to_return
       :
0 =     
1 =        
2 =        
3 =        
4 =       
5 =       
6 =    100  
7 =    500  
8 =    1,000  
9 =    1,000,000  
update_status
        ,  :
0 =          。           ,               。
1 =             sp_dbmmonitorupdate
         。  ,     15         ,      sysadmin           ,  sp_dbmmonitorresults
   ,      。
 
--============================================================
--            ,                          。
sp_dbmmonitoraddmonitoring [ update_period ]
update_period:      (  )。        1   1201--============================================================
--     
--sp_dbmmonitoraddmonitoring      dbm_monitor_data
 
WITH tmp AS( SELECT ROW_NUMBER()OVER(
PARTITION BY dm.database_id
ORDER BY dm.[local_time] DESC) AS RID,
 *
FROM msdb.dbo.dbm_monitor_data dm)
SELECT * FROM tmp WHERE RID=1
 
 
--------------------------------------------------------------------------------
--    
--============================================
--MSDN: http://technet.microsoft.com/zh-cn/library/ms173768.aspx
--sp_dbmmonitorupdate          ,            。
--===================================================================
--            ,                          。
--sp_dbmmonitoraddmonitoring [ update_period ]
--update_period
--      (  )。       1  120       。    1   。
--    sysadmin               ,           
 
EXEC msdb.sys.sp_dbmmonitoraddmonitoring 3;
 
 
--===================================================================
--
--sp_dbmmonitorchangemonitoring parameter, value
--parameter
--            。  ,        :
--1 =     ,                  。     1   。
--value
--            。   1  120    ,          (  )。
 
exec msdb.sys.sp_dbmmonitorchangemonitoring 1,2
 
 
--===================================================================
--         
--sp_dbmmonitorhelpmonitoring
 
exec msdb.sys.sp_dbmmonitorhelpmonitoring
 
 
--===================================================================
--sp_dbmmonitorresults database_name , rows_to_return, update_status
 
--database_name
--
 
--rows_to_return
--
--0 =     
--1 =        
--2 =        
--3 =        
--4 =       
--5 =       
--6 =   100  
--7 =   500  
--8 =   1,000  
--9 =   1,000,000  
 
--update_status
--        ,  :
--0 =          。           ,               。
--1 =            sp_dbmmonitorupdate          。
--  ,    15         ,     sysadmin           ,
-- sp_dbmmonitorresults    ,      。
 
EXEC msdb.sys.sp_dbmmonitorresults DB1, 2, 0;
 
 
 
--===================================================================
--
--    sysadmin             。
 
EXEC msdb.sys.sp_dbmmonitordropmonitoring
 
--===================================================================
--  dbm_monitor_data            ,   ,   
--status:       :=    1 =    2 =     3 =       4 =    
--send_queue_size:                 (KB)。
--redo_queue_size:          (KB)。
--role:            :=   1 =   ,
--witness_status:    :=   1 =    2 =    
 
use msdb;
GO
IF (OBJECT_ID('tempdb.dbo.#MirrorResult') IS NOT NULL)
BEGIN
DROP TABLE #MirrorResult
END
GO
WITH tmp AS(
SELECT
ROW_NUMBER()OVER(PARTITION BY Database_id ORDER BY local_time DESC) AS RID,
*
FROM msdb.dbo.dbm_monitor_data
)
SELECT * INTO #MirrorResult FROM tmp
WHERE RID=1
AND (
([status]<>2 AND [status]<>4)
OR send_queue_size>30000
OR redo_queue_size>30000)
 
--
IF EXISTS(SELECT 1 FROM #MirrorResult)
BEGIN
DECLARE @databaseNames NVARCHAR(MAX);
DECLARE @errorMessage NVARCHAR(MAX);
SET @databaseNames='';
SELECT @databaseNames=@databaseNames+DBS.name+'/' FROM #MirrorResult MR
INNER JOIN master.sys.databases DBS
ON MR.database_id=DBS.database_id
 
set @errorMessage= ''+@databaseNames+'                   '
 
--    
PRINT @errorMessage
 
END
--===================================================================
 
 

 
--=============================================================================
--                    
WITH tmp AS(
SELECT
ROW_NUMBER()OVER(PARTITION BY Database_id ORDER BY local_time DESC) AS RID,
*
FROM msdb.dbo.dbm_monitor_data
)
SELECT * INTO #MirrorResult FROM tmp
WHERE RID=1
AND (
([status]<>2 
AND [status]<>4) 
OR send_queue_size>30000 
OR redo_queue_size>30000)

 

좋은 웹페이지 즐겨찾기