Description: Checks to see if anything weird is going on with the tables loads and sends an alert when something strange is found. Script looks for unusual load amounts and tables not being loaded.
Necessary Modification: There are some tables you do not want to monitor for various reasons. To remove these tables from the results set, get their table ID from vol.Tables and insert them into the UnwantedTables temporary table.
--Remove Rarely loaded tables from 0 load analysisINSERT INTO #UnWantedTables(TableID)SELECT135UNION ALLSELECT131UNION ALLSELECT118UNION ALLSELECT123
USE [ODS]GODROPPROCEDUREIFEXISTS [dbo].[usp_TableLoadMonitoring]GOSETANSI_NULLSONGOSETQUOTED_IDENTIFIERONGOCREATEPROCEDURE [dbo].[usp_TableLoadMonitoring] ASBEGIN--Update with your operator name.DECLARE @OperatorName sysname=N'YourOperatorName';DECLARE @OperatorEmailAddress NVARCHAR(100) = (SELECT email_address FROM msdb.dbo.sysoperators WHERE [name] = @OperatorName);
CREATETABLE #UnWantedTables(TableID INT)CREATECLUSTEREDINDEX CIDX_UNWANTEDTABLES_TABLEID ON #UnWantedTables(TableID)DECLARE @MostRecentDate DATETIMESELECT @MostRecentDate =MAX(DateOfCurrentObservation) FROM [vol].[LoadObservations]INSERT INTO #UnWantedTables(TableID)--Get rid of tables with no recordsSELECT t.TableIDFROM [vol].[Tables] tJOIN [vol].[LoadObservations] loON t.TableID = lo.TableIDGROUP BY t.TableIDHAVINGAVG(CAST(lo.[RowCount] ASBIGINT)) =0UNION--Get rid of tables with no load varianceSELECT t.TableIDFROM [vol].[Tables] tJOIN [vol].[LoadObservations] loON t.TableID = lo.TableIDGROUP BY t.TableIDHAVINGAVG(lo.CurrentThreeSDLevel) =0--Find Adverse LoadsSELECT t.TableIDFROM [vol].[Tables] tJOIN [vol].[LoadObservations] loON t.TableID = lo.TableIDWHERE t.TableID NOTIN (SELECT TableID FROM #UnWantedTables)AND lo.DateOfCurrentObservation = @MostRecentDateAND lo.ChangeFromLastObservation > lo.CurrentThreeSDLevel--ORDER BY t.TableNameIF @@RowCount >0BEGINEXEC msdb.dbo.sp_send_dbmail@profile_name ='Monitoring',@recipients = @OperatorEmailAddress,@subject ='Adverse Load Event',@body = 'One or more tables in the warehouse has experienced an anomalous load event. Check v_ShowAdverseTableLoads in the ODS database.' ;
END--Remove Rarely loaded tables from 0 load analysis--INSERT INTO #UnWantedTables(TableID)--SELECT 135--UNION ALL--SELECT 131--UNION ALL--SELECT 118--UNION ALL--SELECT 123--Find tables not being loadedSELECT t.TableIDFROM [vol].[Tables] tJOIN [vol].[LoadObservations] loON t.TableID = lo.TableIDWHERE t.TableID NOTIN (SELECT TableID FROM #UnWantedTables)AND lo.DateOfCurrentObservation BETWEENDATEADD(DD,-3,CURRENT_TIMESTAMP) AND CURRENT_TIMESTAMPGROUP BY t.TableIDHAVINGAVG(CAST(lo.ChangeFromLastObservation ASFLOAT)) =0IF @@RowCount >0BEGINEXEC msdb.dbo.sp_send_dbmail@profile_name ='Monitoring',@recipients = @OperatorEmailAddress,@subject ='Adverse Load Event',@body = 'One or more tables in the warehouse have experienced three straight days of loading zero records. Run sp_DisplayTablesNotLoading in ODS database.';
ENDDROPTABLE #UnWantedTablesENDGO