Just deleted a database, in the cleanup database backup history, the execution of more than nearly 10 minutes has not completed, at any time to check the next, scared the baby, logic read operation unexpectedly up to 80 million times more!
When the database is deleted through the UI, the "delete database backup and restore history information" is selected by default, as the old driver for many years, delete the database should write a script to delete, even if the use of UI Delete, should not check this option, but lazy, direct point of execution, Causing the operation to consume a large amount of logical IO and CPU and last 10 minutes does not complete successfully.
When you tick "delete database backup and restore history information", the following statement is executed:
EXEC @database_name = N'monitor'GO
After the stored procedure is executed, the backup-related n tables are deleted in the msdb database, with one DELETE statement as follows:
DELETEmsdb.dbo.backupmediafamily frommsdb.dbo.backupmediafamily BMFWHEREbmf.media_set_idinch(SELECTmedia_set_id from @media_set_id ) and( (SELECT COUNT(*) fromMsdb.dbo.backupsetWHEREmedia_set_id=bmf.media_set_id)= 0)
When the backup and restore history information is high, the resource consumed by the delete operation grows in geometry, because the server is used as a log shipping server to host log shipping for many databases, so there is a large amount of data in the related backup table, which causes the deletion operation to be incomplete for a long time.
Workaround:
Periodically perform the following script to clean up the backup restore data:
--set History is saved for 1 daysDECLARE @keepMinutes BIGINTSET @keepMinutes= -* -DECLARE @expiredDT NVARCHAR( -)SELECT @expiredDT =Dbo.ufn_formatdate (DATEADD(MINUTE,0 - @keepMinutes, GETDATE()), 'Yyyy-mm-ddthh:mm:ss')EXECMsdb.dbo.sp_delete_backuphistory@expiredDTEXECMsdb.dbo.sp_purge_jobhistory@oldest_date = @expiredDTEXECMsdb.dbo.sp_maintplan_delete_logNULL,NULL,@expiredDT
The above script uses a date format conversion function with the following code:
/** * * * object:userdefinedfunction [dbo]. [Ufn_formatdate] Script DATE:2015/11/24 19:40:45 * * * * **/DROP FUNCTION [dbo].[ufn_formatdate]GO/** * * * object:userdefinedfunction [dbo]. [Ufn_formatdate] Script DATE:2015/11/24 19:40:45 * * * * **/SETAnsi_nulls onGOSETQuoted_identifier onGO--====================================--convert time into a formatted stringCREATE FUNCTION [dbo].[ufn_formatdate] ( @Datetime DATETIME , @FormatMask VARCHAR( +) )RETURNS VARCHAR( +) as BEGIN DECLARE @StringDate VARCHAR( +) SET @StringDate = @FormatMask IF(CHARINDEX('YYYY',@StringDate)> 0 ) SET @StringDate = REPLACE(@StringDate,'YYYY', Datename(YY,@Datetime)) IF(CHARINDEX('YY',@StringDate)> 0 ) SET @StringDate = REPLACE(@StringDate,'YY', Right(Datename(YY,@Datetime),2)) IF(CHARINDEX('MM',@StringDate)> 0 ) SET @StringDate = REPLACE(@StringDate,'MM', Right('0' + CONVERT(VARCHAR,DATEPART(MM,@Datetime)),2)) IF(CHARINDEX('DD',@StringDate)> 0 ) SET @StringDate = REPLACE(@StringDate,'DD', Right('0' + Datename(DD,@Datetime),2)) IF(CHARINDEX('HH',@StringDate)> 0 ) SET @StringDate = REPLACE(@StringDate,'HH', Right('0' + Datename(HH,@Datetime),2)) IF(CHARINDEX('mm',@StringDate)> 0 ) SET @StringDate = REPLACE(@StringDate,'mm', Right('0' + Datename(MM,@Datetime),2)) IF(CHARINDEX('SS',@StringDate)> 0 ) SET @StringDate = REPLACE(@StringDate,'SS', Right('0' + Datename(SS,@Datetime),2)) IF(CHARINDEX('Ms',@StringDate)> 0 ) SET @StringDate = REPLACE(@StringDate,'Ms', Right('0' + Datename(MS,@Datetime),2)) RETURN @StringDate END--====================================GO
View Code
Not much technical content, thick face to take out for beginners to learn!
============================================
sqlserver--regularly cleans up the history of maintenance jobs