Blog do EzequielPortuguese PFE SQL Server Team
Hello all,
I admit I’m not a big fan of Maintenance Plans (the feature that goes by that name) mainly because of its “do or die” approach. So, it was not by chance that I wrote and keep updating the AdaptiveIndexDefrag procedure. Good maintenance (or lack thereof) may be the difference between good and bad performance. Following up on that train of though, I’ve been meaning to share 6 scripts that I always use to automate maintenance tasks in SQL Server. In a couple minutes I have deployed a comprehensive set of SQL Agent jobs that will get the work done.
So, the first script I execute is “0_database_server_options”. This will:
Second, and if there is no other monitoring system, I use the script “1_DatabaseMail_Config”. Just edit the script and enter the proper account information in the configuration variables near the top and run it. Replace with the information for your account.This script will:
Third, I deploy the AdaptiveIndexDefrag procedure, usually in MSDB unless there is already a DBA database.
Fourth, I use the script “3_job_AdaptiveIndexDefrag”. This will create a daily job for the AdaptiveIndexDefrag procedure, named “Daily Index Defrag”. It will also notify the previously created operator on the job outcome. The job will always:
Fifth, I use the script “4_job_AdaptiveCycleErrorlog”. As the name suggests, this creates a job named “Daily Cycle Errorlog”. Runs daily, but will only cycle the Errorlog when its size is over 20MB or its age over 15 days.
Sixth, using the script “5_job_Maintenance”, create the job “Weekly Maintenance”, and a view in MSDB that allows to quickly check the output for each job step, XML formatted so that it’s easier to view. This job will:
SELECT database_id FROM sys.databases WHERE is_read_only = 0 AND state = 0 AND database_id <> 2 to something like SELECT database_id FROM sys.databases WHERE is_read_only = 0 AND state= 0 AND database_id <> 2 AND database_id NOT IN (somedbid1, somedbid2, somedbid3)
SELECT database_id FROM sys.databases WHERE is_read_only = 0 AND state = 0 AND database_id <> 2
to something like
SELECT database_id FROM sys.databases WHERE is_read_only = 0 AND state= 0
AND database_id <> 2 AND database_id NOT IN (somedbid1, somedbid2, somedbid3)
Seventh, and final step (although optional), I use the script "6_Agent_Alerts" to create SQL Agent based alerts for important error messages, when there are no other monitoring tools to warn the DBA team of important events in SQL Server. The alerts cover the following error codes and severities:
Having these (or other) scripts to deploy on your SQL Servers saves you the trouble of implementing Maintenance Plan objects, and gives you much better control and flexibility on what is happening on your maintenance cycles. After these have been running on a regular basis, you will know that your SQL Server is being proactively managed on what are the most common maintenance tasks. Of course all the logs should be reviewed regularly so that issues can be corrected.
Hope you find this useful.
Here are the scripts:
2_AdaptiveIndexDefrag
Change log:
Version
Date
Comments
1
18-09-2012
Initial release;
1.1
25-10-2012
Added script "6_Agent_Alerts.sql";Fixed issue with "1_DatabaseMail_Config.sql" that gave an error when creating the operator;Added notifications by email to the operator created above.
Until next time!
Disclaimer: I hope that the information on these pages is valuable to you. Your use of the information contained in these pages, however, is at your sole risk. All information on these pages is provided "as -is", without any warranty, whether express or implied, of its accuracy, completeness, fitness for a particular purpose, title or non-infringement, and none of the third-party products or information mentioned in the work are authored, recommended, supported or guaranteed by Ezequiel. Further, Ezequiel shall not be liable for any damages you may sustain by using this information, whether direct, indirect, special, incidental or consequential, even if it has been advised of the possibility of such damages.
Handy toolset indeed.. Thanks Pedro
Hi Pedro,
Just a small typo correction and a small suggestion:
On the DataBase Server options script:
Line 19:
EXEC sys.sp_configure N'optimize for ad hoc workloads', N'1
should be:
EXEC sys.sp_configure N'optimize for ad hoc workloads', N'1'
On the Adaptative Index Defrag I think would be a good idea to check if dba_db exist and create it if necessary, otherwise we receive an error that dba_db does not exist and all objects are created on current db.
My suggestion is something like this at the beginning of the script:
IF NOT EXISTS(SELECT [database_id] FROM sys.databases WHERE [name] = 'dba_db')
CREATE DATABASE [dba_db]
CONTAINMENT = NONE
ON PRIMARY
( NAME = N'dba_db', FILENAME = N'C:\Program Files\Microsoft SQL Server\MSSQL10_50.VERVESQL\MSSQL\DATA\dba_db.mdf' , SIZE = 102400KB , FILEGROWTH = 102400KB )
LOG ON
( NAME = N'dba_db_log', FILENAME = N'C:\Program Files\Microsoft SQL Server\MSSQL10_50.VERVESQL\MSSQL\DATA\dba_db_log.ldf' , SIZE = 51200KB , FILEGROWTH = 102400KB )
GO
One again thanks for all your work.
Regards
Eduardo Barbosa
Hey Eduardo,
The 1st script is fixed now. Thanks for noticing the typo.
About the "dba_db" its just meant to be changed for whatever database a DBA chooses to create the logic. I came to realize most people that use it, deploy it in MSDB. Furthermore, the subsequent jobs, as they are scripted, will look for the sproc in MSDB, so that also needs to be changed to the corect database as needed.
Cheers
Gotcha,
Also check that the jobs are expecting the objects to be on the msdb.
Then I suggest a "use msdb" instead with some comment above it clarifying that.
Right, what I did just now was highlight the notes/warnings on that topic that were already in the above post. Also took you advice on changing the default context to msdb in AdativeIndexDefrag - but please note that it can be deployed anywhere.
Thanks!
PL
Very nice article
It's perfect now :) ...and working smoothly on SQL Server 2012
Cheer and Thanks
Great <a href="www.constantconcierge.com/">maintenance plan</a>.