3_job_AdaptiveIndexDefrag.sql 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  1. USE [msdb]
  2. GO
  3. IF EXISTS (SELECT job_id FROM msdb.dbo.sysjobs_view WHERE name = N'Daily Index Defrag')
  4. EXEC msdb.dbo.sp_delete_job @job_name=N'Daily Index Defrag', @delete_unused_schedule=1
  5. GO
  6. PRINT 'Creating Daily Index Maintenance job';
  7. GO
  8. BEGIN TRANSACTION
  9. -- Set the Operator name to receive notifications, if any. Set the job owner, if not sa.
  10. DECLARE @customoper sysname, @jobowner sysname
  11. SET @customoper = 'SQLAdmins'
  12. SET @jobowner = 'sa'
  13. DECLARE @ReturnCode INT
  14. SELECT @ReturnCode = 0
  15. IF NOT EXISTS (SELECT name FROM msdb.dbo.syscategories WHERE name=N'Database Maintenance' AND category_class=1)
  16. BEGIN
  17. EXEC @ReturnCode = msdb.dbo.sp_add_category @class=N'JOB', @type=N'LOCAL', @name=N'Database Maintenance'
  18. IF (@@ERROR <> 0 OR @ReturnCode <> 0) GOTO QuitWithRollback
  19. END
  20. DECLARE @jobId BINARY(16)
  21. IF EXISTS (SELECT name FROM msdb.dbo.sysoperators WHERE name = @customoper)
  22. BEGIN
  23. EXEC @ReturnCode = msdb.dbo.sp_add_job @job_name=N'Daily Index Defrag',
  24. @enabled=1,
  25. @notify_level_eventlog=2,
  26. @notify_level_email=3,
  27. @notify_level_netsend=0,
  28. @notify_level_page=0,
  29. @delete_level=0,
  30. @description=N'Inteligent defrag on one or more indexes for one or more databases.',
  31. @category_name=N'Database Maintenance',
  32. @owner_login_name=@jobowner,
  33. @notify_email_operator_name=@customoper,
  34. @job_id = @jobId OUTPUT
  35. IF (@@ERROR <> 0 OR @ReturnCode <> 0) GOTO QuitWithRollback
  36. END
  37. ELSE
  38. BEGIN
  39. EXEC @ReturnCode = msdb.dbo.sp_add_job @job_name=N'Daily Index Defrag',
  40. @enabled=1,
  41. @notify_level_eventlog=2,
  42. @notify_level_email=3,
  43. @notify_level_netsend=0,
  44. @notify_level_page=0,
  45. @delete_level=0,
  46. @description=N'Inteligent defrag on one or more indexes for one or more databases.',
  47. @category_name=N'Database Maintenance',
  48. @owner_login_name=@jobowner,
  49. @job_id = @jobId OUTPUT
  50. IF (@@ERROR <> 0 OR @ReturnCode <> 0) GOTO QuitWithRollback
  51. END
  52. EXEC @ReturnCode = msdb.dbo.sp_add_jobstep @job_id=@jobId, @step_name=N'DB Exceptions',
  53. @step_id=1,
  54. @cmdexec_success_code=0,
  55. @on_success_action=4,
  56. @on_success_step_id=2,
  57. @on_fail_action=2,
  58. @on_fail_step_id=0,
  59. @retry_attempts=0,
  60. @retry_interval=0,
  61. @os_run_priority=0, @subsystem=N'TSQL',
  62. @command=N'DECLARE @dbname NVARCHAR(128)
  63. DECLARE curDB CURSOR FOR SELECT name
  64. FROM sys.databases WHERE ([name] IN (
  65. --Semantic Search
  66. ''Semanticsdb'',
  67. --Biztalk 2009 / 2010 / 2013
  68. ''SSODB'',''BAMAnalysis'',''BAMArchive'',''BAMAlertsApplication'',''BAMAlertsNSMain'',''BAMPrimaryImport'',''BAMStarSchema'',''BizTalkMgmtDb'',''BizTalkMsgBoxDb'',''BizTalkDTADb'',''BizTalkRuleEngineDb'',
  69. --Biztalk 2006R2
  70. ''BAMPrimaryImport'',''BizTalkEDIDb'',''BizTalkHwsDb'',''TPM'',''BizTalkAnalysisDb'',
  71. --Biztalk 2004
  72. ''BAMPrimaryImportsuccessfully'',
  73. --Sharepoint 2007
  74. ''SSO'',''WSS_Search'',''WSS_Search_Config'',''SharedServices_DB'',''SharedServices_Search_DB'',''WSS_Content'',
  75. --SCOM 2012 / 2012R2
  76. ''OperationsManager'',''OperationsManagerDW'')
  77. --SharePoint Server 2007
  78. OR [name] LIKE ''WSS_Search%''
  79. OR [name] LIKE ''SharedServices_DB%''
  80. OR [name] LIKE ''SharedServices_Search_DB%''
  81. OR [name] LIKE ''SharedServices__DB%''
  82. OR [name] LIKE ''SharedServices__Search_DB%''
  83. OR [name] LIKE ''SharedServicesContent%''
  84. --SharePoint 2010
  85. OR [name] LIKE ''Secure_Store_Service_DB_%''
  86. OR [name] LIKE ''StateService%''
  87. OR [name] LIKE ''WebAnalyticsServiceApplication_StagingDB_%''
  88. OR [name] LIKE ''WebAnalyticsServiceApplication_ReportingDB_%''
  89. OR [name] LIKE ''Search_Service_Application_DB_%''
  90. OR [name] LIKE ''Search_Service_Application_CrawlStoreDB_%''
  91. OR [name] LIKE ''Search_Service_Application_PropertyStoreDB_%''
  92. OR [name] LIKE ''User Profile Service Application_ProfileDB_%''
  93. OR [name] LIKE ''User Profile Service Application_SyncDB_%''
  94. OR [name] LIKE ''User Profile Service Application_SocialDB_%''
  95. OR [name] LIKE ''Managed Metadata Service_%''
  96. OR [name] LIKE ''WordAutomationServices_%''
  97. --SharePoint 2013
  98. OR [name] LIKE ''SharePoint_Admin_Content%''
  99. OR [name] LIKE ''AppManagement%''
  100. OR [name] LIKE ''Search_Service_Application_AnalyticsReportingStoreDB_%''
  101. OR [name] LIKE ''Search_Service_Application_LinkStoreDB_%''
  102. OR [name] LIKE ''Secure_Store_Service_DB_%''
  103. OR [name] LIKE ''SharePoint_Logging_%''
  104. OR [name] LIKE ''SettingsServiceDB%''
  105. OR [name] LIKE ''SharePoint_Logging_%''
  106. OR [name] LIKE ''Managed Metadata Service Application_Metadata_%''
  107. OR [name] LIKE ''SharePoint Translation Services_%''
  108. OR [name] LIKE ''SessionStateService%''
  109. --SharePoint Foundation 2010
  110. OR [name] LIKE ''SharePoint_Config%''
  111. OR [name] LIKE ''SharePoint_AdminContent%''
  112. OR [name] LIKE ''WSS_Content%''
  113. OR [name] LIKE ''WSS_UsageApplication%''
  114. OR [name] LIKE ''Bdc_Service_DB_%''
  115. OR [name] LIKE ''Application_Registry_server_DB_%''
  116. OR [name] LIKE ''SubscriptionSettings_%''
  117. OR [name] LIKE ''SharePoint_AdminContent%''
  118. --FAST Search Server for SharePoint 2010
  119. OR [name] LIKE ''FASTSearchAdminDatabase%''
  120. )
  121. AND database_id NOT IN (SELECT DISTINCT(dbID) FROM msdb.dbo.tbl_AdaptiveIndexDefrag_Exceptions)
  122. OPEN curDB
  123. FETCH NEXT FROM curDB INTO @dbname
  124. WHILE @@FETCH_STATUS = 0
  125. BEGIN
  126. PRINT ''Excluding '' + @dbname
  127. EXEC msdb.dbo.usp_AdaptiveIndexDefrag_Exceptions @exceptionMask_DB = @dbname, @exceptionMask_days = NULL
  128. FETCH NEXT FROM curDB INTO @dbname
  129. END
  130. CLOSE curDB
  131. DEALLOCATE curDB',
  132. @database_name=N'msdb',
  133. @flags=4
  134. IF (@@ERROR <> 0 OR @ReturnCode <> 0) GOTO QuitWithRollback
  135. EXEC @ReturnCode = msdb.dbo.sp_add_jobstep @job_id=@jobId, @step_name=N'Exec',
  136. @step_id=2,
  137. @cmdexec_success_code=0,
  138. @on_success_action=4,
  139. @on_success_step_id=3,
  140. @on_fail_action=2,
  141. @on_fail_step_id=0,
  142. @retry_attempts=0,
  143. @retry_interval=0,
  144. @os_run_priority=0, @subsystem=N'TSQL',
  145. @command=N'EXEC msdb.dbo.usp_AdaptiveIndexDefrag @onlineRebuild = 1, @sortInTempDB = 1, @maxDopRestriction = 2',
  146. @database_name=N'msdb',
  147. @flags=4
  148. IF (@@ERROR <> 0 OR @ReturnCode <> 0) GOTO QuitWithRollback
  149. EXEC @ReturnCode = msdb.dbo.sp_add_jobstep @job_id=@jobId, @step_name=N'Purge Log',
  150. @step_id=3,
  151. @cmdexec_success_code=0,
  152. @on_success_action=1,
  153. @on_success_step_id=0,
  154. @on_fail_action=2,
  155. @on_fail_step_id=0,
  156. @retry_attempts=0,
  157. @retry_interval=0,
  158. @os_run_priority=0, @subsystem=N'TSQL',
  159. @command=N'EXECUTE msdb.dbo.usp_AdaptiveIndexDefrag_PurgeLogs;',
  160. @database_name=N'msdb',
  161. @flags=0
  162. IF (@@ERROR <> 0 OR @ReturnCode <> 0) GOTO QuitWithRollback
  163. EXEC @ReturnCode = msdb.dbo.sp_update_job @job_id = @jobId, @start_step_id = 1
  164. IF (@@ERROR <> 0 OR @ReturnCode <> 0) GOTO QuitWithRollback
  165. EXEC @ReturnCode = msdb.dbo.sp_add_jobschedule @job_id=@jobId, @name=N'Daily Index Defrag',
  166. @enabled=1,
  167. @freq_type=4,
  168. @freq_interval=1,
  169. @freq_subday_type=1,
  170. @freq_subday_interval=0,
  171. @freq_relative_interval=0,
  172. @freq_recurrence_factor=0,
  173. @active_start_date=20110629,
  174. @active_end_date=99991231,
  175. @active_start_time=230000,
  176. @active_end_time=235959
  177. IF (@@ERROR <> 0 OR @ReturnCode <> 0) GOTO QuitWithRollback
  178. EXEC @ReturnCode = msdb.dbo.sp_add_jobserver @job_id = @jobId, @server_name = N'(local)'
  179. IF (@@ERROR <> 0 OR @ReturnCode <> 0) GOTO QuitWithRollback
  180. COMMIT TRANSACTION
  181. GOTO EndSave
  182. QuitWithRollback:
  183. IF (@@TRANCOUNT > 0) ROLLBACK TRANSACTION
  184. EndSave:
  185. GO
  186. PRINT 'Daily Index Maintenance job created';
  187. GO