Wednesday, October 17, 2018
A modified and hardened process that updates statistics
SET NOCOUNT ON
DECLARE @name VARCHAR(128) = '' -- database name
, @update_stats varchar(max) = 'EXECUTE {DBNAME}..sp_updatestats'
, @sql_script varchar(max) = ''
, @errorMessage varchar(max) = ''
, @Recips VARCHAR (1024) = 'foo@foo.org; '
-- , @Subject VARCHAR (1024) = 'TESTING: Failure of maintenance plan for updating statistics '
, @Subject VARCHAR (1024) = 'Failure of maintenance plan for updating statistics '
-- DEV
-- , @mailProfile VARCHAR (64) = 'Mail'
-- QA
, @mailProfile VARCHAR (64) = 'Mail'
-- Prod
-- , @mailProfile VARCHAR (64) = 'Mail'
/*
SELECT @Recips += ISNULL (ap.pValue, 'Elmer; ')
FROM dbo.params ap
WHERE ap.pName = 'Elmer'
*/
DECLARE db_cursor CURSOR FOR
-- Force an error for testing ...
-- SELECT name
SELECT QUOTENAME (name)
FROM master.dbo.sysdatabases
WHERE name NOT IN ('master','model','msdb','tempdb','AspNetSqlSessionState')
OPEN db_cursor
BEGIN TRY
FETCH NEXT FROM db_cursor INTO @name
WHILE @@FETCH_STATUS = 0
BEGIN
SELECT @name
SELECT GETDATE () [Start]
SET @sql_script = REPLACE(@update_stats, '{DBNAME}', @name)
EXEC (@sql_script)
FETCH NEXT FROM db_cursor INTO @name
SELECT GETDATE () [End]
END
END TRY
BEGIN CATCH
SELECT @errorMessage += @name + ': ' + someDB.dbo.fnGetErrorTryCatch ()
END CATCH
CLOSE db_cursor
DEALLOCATE db_cursor
SELECT @errorMessage
IF ISNULL (@errorMessage, '') != ''
BEGIN
EXEC msdb.dbo.sp_send_dbmail
@profile_name = @mailProfile
, @recipients = @Recips
, @body_format = 'HTML'
, @body = @errorMessage
, @subject = @Subject
END
Tuesday, October 16, 2018
How to tell if statistics are actually being updated
Even if everything 'looks' good:
SELECT 'Index Name' = i.name,
'Statistics Date' = STATS_DATE(i.object_id, i.index_id)
FROM sys.objects o
JOIN sys.indexes i ON o.name = 'ORDERs' AND o.object_id = i.object_id;
GO
Subscribe to:
Posts (Atom)