Using Automation to Simplify SQL Server Management <br />Greg RobidouxEdgewood Solutionsgregr@edgewoodsolutions.com<br />B...
Idera Solutions  for SQL Server<br />Performance & Availability<br />Compliance & Security<br />Change Management  <br />B...
Agenda<br />Why Automate<br />What to Automate<br />How to Automate<br />SQL Server Tools<br />OtherTools<br />Questions /...
Why automate<br />Redundant tasks<br />Only want to know when there are issues<br />Automatic recovery<br />Reduce manual ...
What to automate<br />Backups<br />Integrity Checks<br />Index Maintenance<br />Link Server Status<br />Free Disk Space<br...
What to collect<br />Backups – failed and successful<br />Replication Errors<br />Maintenance Tasks<br />Failed Logins<br ...
Process<br />Setup<br />Setup key components such as Database Mail, Operators, etc…<br />Scripts<br />Create scripts or se...
Notifications<br />Database Mail<br />SQL Agent Notifications<br />Alerts<br />Operators<br />Other Tools<br />8<br />
Database Mail<br />Uses SMTP<br />Setup Default Profile<br />sp_send_dbmail<br />http://www.mssqltips.com/tip.asp?tip=1736...
SQL Agent Alert System<br />To send out notifications for scheduled jobs you need to enable a mail profile.<br />10<br />
Automate<br />Options<br />Maintenance Plans<br />Scripting<br />T-SQL<br />PowerShell<br />Windows Script<br />SQLCMD<br ...
Maintenance Plans<br />Allows you to automate routine tasks such as: <br />Backups<br />Index maintenance<br />Integrity c...
Maintenance Plan Tasks<br />Backup Database Task<br />Check Database Integrity Task<br />Execute SQL Server Agent Task<br ...
Maintenance Plans<br />Maintenance Plans Use<br />master.dbo.xp_create_subdir<br />msdb.dbo.sp_delete_backuphistory<br />m...
Maintenance Plans – Other Options<br />Subplans<br />Reporting<br />Scheduling<br />15<br />
Scripting<br />More work, but gives you more control.<br />Stored Procedures (T-SQL or CLR)<br />SSIS Packages<br />VBScri...
Scripting Examples<br />In addition to Maintenance Plans<br />Check Free Disk Space<br />Transaction Log Usage<br />Last B...
Backup All Databases Script<br />DECLARE @name VARCHAR(50) -- database name  DECLARE @path VARCHAR(256) -- path for backup...
Log Space Usage Script<br />CREATE TABLE ##logspace<br />(databaseNamesysname,<br />logsize decimal(10,5),<br />logused de...
 Free Drive Space Script<br />Use sys.xp_fixeddrives<br />CREATE TABLE #drivespace<br />(drive varchar(20),<br />freespace...
Scripts that Generate Scripts<br />Use system Meta Data<br />Stored procedures<br />Index drops and creations<br />Constra...
Reading Error Logs	<br />Type of System Logs<br />Database Mail<br />SQL Agent<br />SQL Server<br />Windows<br />22<br />
SP_ReadErrorLog<br />EXEC sp_readerrorlog<br />Parameters<br />Value of error log file you want to read: <br />0 = current...
XP_ReadErrorLog<br />EXEC xp_readerrorlog<br />Parameters<br />Value of error log file you want to read: 0 = current, 1 = ...
Scripting<br />Another way to read the error logs is to read line by line and searching for keywords.<br />Can be done usi...
Scheduling<br />SQL Agent<br />Windows Scheduled Tasks<br />VisualCron<br />Other Third Party Tools<br />SQL Express – doe...
SQL Agent<br />Can Run<br />ActiveX, CmdExec, Replication, SSAS, SSIS, T-SQL<br />Run when SQL Agent starts<br />SQL Agent...
Scheduling – Frequency<br />Backups - Daily<br />Integrity Checks - Weekly<br />Index Maintenance - Weekly<br />Other Task...
Multi Server Administration<br />Master / Target<br />Manage all scheduled jobs from one server<br />29<br />
Alerts<br />Replication<br />Database Mirroring<br />Backups<br />User Defined<br />Etc…<br />Response<br />Execute a Job<...
Central Management Server<br />New in SQL 2008<br />Allows you to register servers and fire off same query on all servers<...
Other Tools<br />Trace<br />Perfmon<br />DMVs<br />http://www.mssqltips.com/tip.asp?tip=1035<br />http://www.mssqltips.com...
Startup Procedures<br />USE MASTERGOEXEC SP_PROCOPTION ‘SP_LOG_SERVER_START’, &apos;STARTUP&apos;, &apos;ON&apos;GO<br />U...
Questions and Wrap-up<br />Thanks to our sponsor: Idera<br />Next webcast in the series:  <br />“Under the Hood with SQL S...
Upcoming SlideShare
Loading in...5
×

Sql Automation 20090610

944

Published on

0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total Views
944
On Slideshare
0
From Embeds
0
Number of Embeds
0
Actions
Shares
0
Downloads
11
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

Sql Automation 20090610

  1. 1. Using Automation to Simplify SQL Server Management <br />Greg RobidouxEdgewood Solutionsgregr@edgewoodsolutions.com<br />Bullet ManaleIderawww.idera.com<br />
  2. 2. Idera Solutions for SQL Server<br />Performance & Availability<br />Compliance & Security<br />Change Management <br />Backup & Recovery<br />Administration<br />
  3. 3. Agenda<br />Why Automate<br />What to Automate<br />How to Automate<br />SQL Server Tools<br />OtherTools<br />Questions / Wrap Up<br />3<br />
  4. 4. Why automate<br />Redundant tasks<br />Only want to know when there are issues<br />Automatic recovery<br />Reduce manual steps<br />Create a repeatable process<br />4<br />
  5. 5. What to automate<br />Backups<br />Integrity Checks<br />Index Maintenance<br />Link Server Status<br />Free Disk Space<br />Transaction Log Size<br />Replication Alerts<br />Database Mirroring Alerts<br />Reading SQL Server Error Log<br />Scripts to Create Scripts<br />High Availability<br />Gathering Performance Statistics<br />Trace<br />Perfmon<br />DMVs<br />5<br />
  6. 6. What to collect<br />Backups – failed and successful<br />Replication Errors<br />Maintenance Tasks<br />Failed Logins<br />SQL Server Errors<br />Server Status<br />6<br />
  7. 7. Process<br />Setup<br />Setup key components such as Database Mail, Operators, etc…<br />Scripts<br />Create scripts or sets of code to gather data<br />Automate<br />Schedule process to run and collect data. This could be something you schedule or something that occurs automatically within SQL Server.<br />Analyze<br />Analyze data and determine what to do.<br />Notify<br />Send notification to either a person or another process.<br />7<br />
  8. 8. Notifications<br />Database Mail<br />SQL Agent Notifications<br />Alerts<br />Operators<br />Other Tools<br />8<br />
  9. 9. Database Mail<br />Uses SMTP<br />Setup Default Profile<br />sp_send_dbmail<br />http://www.mssqltips.com/tip.asp?tip=1736<br />http://www.mssqltips.com/tip.asp?tip=1100<br />http://www.mssqltips.com/tip.asp?tip=1261<br />9<br />
  10. 10. SQL Agent Alert System<br />To send out notifications for scheduled jobs you need to enable a mail profile.<br />10<br />
  11. 11. Automate<br />Options<br />Maintenance Plans<br />Scripting<br />T-SQL<br />PowerShell<br />Windows Script<br />SQLCMD<br />SSIS<br />Etc…<br />Scheduling<br />11<br />
  12. 12. Maintenance Plans<br />Allows you to automate routine tasks such as: <br />Backups<br />Index maintenance<br />Integrity checks<br />Cleanup tasks<br />Creates SSIS Packages<br />Limited control<br />12<br />
  13. 13. Maintenance Plan Tasks<br />Backup Database Task<br />Check Database Integrity Task<br />Execute SQL Server Agent Task<br />Execute T-SQL Statement Task<br />History Cleanup Task<br />Maintenance Cleanup Task<br />Notify Operator Task<br />Rebuild Index Task<br />Reorganize Index Task<br />Shrink Database Task<br />Update Statistics Task<br />13<br />
  14. 14. Maintenance Plans<br />Maintenance Plans Use<br />master.dbo.xp_create_subdir<br />msdb.dbo.sp_delete_backuphistory<br />msdb.dbo.sp_purge_jobhistory<br />msdb.dbo.sp_maintplan_delete_log<br />master.dbo.xp_delete_file<br />msdb.dbo.sp_notify_operator<br />BACKUP…<br />ALTER INDEX…<br />DBCC CHECKDB<br />DBCC SHRINKDATABASE<br />msdb.dbo.sp_start_job<br />UPDATE STATISTICS<br />14<br />
  15. 15. Maintenance Plans – Other Options<br />Subplans<br />Reporting<br />Scheduling<br />15<br />
  16. 16. Scripting<br />More work, but gives you more control.<br />Stored Procedures (T-SQL or CLR)<br />SSIS Packages<br />VBScript<br />VB.Net or C#<br />PowerShell<br />SMO (SQL Management Objects)<br />SQLCMD<br />Use of DMVs<br />16<br />
  17. 17. Scripting Examples<br />In addition to Maintenance Plans<br />Check Free Disk Space<br />Transaction Log Usage<br />Last Backup Info<br />Reading Error Logs<br />Selectively Rebuild or Reorganize Indexes<br />Scheduled Job Failures<br />Backup Failures<br />Login Failures<br />Scripts that Generate Scripts<br />17<br />
  18. 18. Backup All Databases Script<br />DECLARE @name VARCHAR(50) -- database name  DECLARE @path VARCHAR(256) -- path for backup files  DECLARE @fileName VARCHAR(256) -- filename for backup  DECLARE @fileDate VARCHAR(20) -- used for file name SET @path = &apos;C:Backup&apos;  SELECT @fileDate = CONVERT(VARCHAR(20),GETDATE(),112) DECLARE db_cursor CURSOR FOR  SELECT name FROM master.dbo.sysdatabases WHERE name NOT IN (&apos;master&apos;,&apos;model&apos;,&apos;msdb&apos;,&apos;tempdb&apos;)  OPEN db_cursor   FETCH NEXT FROM db_cursor INTO @name   WHILE @@FETCH_STATUS = 0   BEGIN          SET @fileName = @path + @name + &apos;_&apos; + @fileDate + &apos;.BAK&apos;         BACKUP DATABASE @name TO DISK = @fileName         FETCH NEXT FROM db_cursor INTO @name   END   CLOSE db_cursor   DEALLOCATE db_cursor<br />http://www.mssqltips.com/tip.asp?tip=1070<br />18<br />
  19. 19. Log Space Usage Script<br />CREATE TABLE ##logspace<br />(databaseNamesysname,<br />logsize decimal(10,5),<br />logused decimal(10,5),<br />status int)<br />INSERT INTO ##logspace<br />EXEC (&apos;dbccsqlperf(logspace)&apos;)<br />EXEC msdb.dbo.sp_send_dbmail<br /> @profile_name = &apos;SQLMail Profile&apos;,<br /> @recipients = &apos;gregr@edgewoodsolutions.com&apos;,<br /> @query = &apos;SELECT * FROM ##logspace WHERE logused &gt; 75&apos; ,<br /> @subject = &apos;Log Space Usage‘<br />DROP TABLE ##logspace<br />19<br />
  20. 20. Free Drive Space Script<br />Use sys.xp_fixeddrives<br />CREATE TABLE #drivespace<br />(drive varchar(20),<br />freespacebigint)<br />INSERT INTO #drivespace<br />EXEC sys.xp_fixeddrives<br />SELECT * FROM #drivespace<br />WHERE freespace &lt; 10000<br />DROP TABLE #drivespace<br />20<br />
  21. 21. Scripts that Generate Scripts<br />Use system Meta Data<br />Stored procedures<br />Index drops and creations<br />Constraints drops and creations<br />Create insert statements<br />SSMS Scripting<br />http://www.mssqltips.com/tip.asp?tip=1376<br />http://vyaskn.tripod.com/code.htm#inserts<br />21<br />
  22. 22. Reading Error Logs <br />Type of System Logs<br />Database Mail<br />SQL Agent<br />SQL Server<br />Windows<br />22<br />
  23. 23. SP_ReadErrorLog<br />EXEC sp_readerrorlog<br />Parameters<br />Value of error log file you want to read: <br />0 = current, 1 = Archive #1, 2 = Archive #2, etc... <br />Log file type: <br />1 or NULL = error log, 2 = SQL Agent log <br />Search string 1: <br />String one you want to search for <br />Search string 2: <br />String two you want to search for to further refine the results<br />EXEC sp_readerrorlog 0, 1, ‘Error’<br />23<br />
  24. 24. XP_ReadErrorLog<br />EXEC xp_readerrorlog<br />Parameters<br />Value of error log file you want to read: 0 = current, 1 = Archive #1, 2 = Archive #2, etc... <br />Log file type: 1 or NULL = error log, 2 = SQL Agent log <br />Search string 1: String one you want to search for <br />Search string 2: String two you want to search for to further refine the results<br />Search from  start time <br />Search to end time<br />Sort order for results: N&apos;asc&apos; = ascending, N&apos;desc&apos; = descending<br />http://www.mssqltips.com/tip.asp?tip=1476<br />http://www.mssqltips.com/tip.asp?tip=1735<br />24<br />
  25. 25. Scripting<br />Another way to read the error logs is to read line by line and searching for keywords.<br />Can be done using any programming language.<br />This tip shows how it can be done using Windows Scripting http://www.mssqltips.com/tip.asp?tip=1307<br />Another tool is the Log Parser tool from Microsoft<br />http://www.microsoft.com/downloads/details.aspx?FamilyID=890cd06b-abf8-4c25-91b2-f8d975cf8c07&displaylang=en<br />25<br />
  26. 26. Scheduling<br />SQL Agent<br />Windows Scheduled Tasks<br />VisualCron<br />Other Third Party Tools<br />SQL Express – does not include SQL Agent<br />26<br />
  27. 27. SQL Agent<br />Can Run<br />ActiveX, CmdExec, Replication, SSAS, SSIS, T-SQL<br />Run when SQL Agent starts<br />SQL Agent Alert System<br />sp_start_job<br />System Tables<br />Notifications<br />Logging<br />Alerts Can Start Job<br />27<br />
  28. 28. Scheduling – Frequency<br />Backups - Daily<br />Integrity Checks - Weekly<br />Index Maintenance - Weekly<br />Other Tasks – Depends<br />Reports – Daily, Weekly<br />28<br />
  29. 29. Multi Server Administration<br />Master / Target<br />Manage all scheduled jobs from one server<br />29<br />
  30. 30. Alerts<br />Replication<br />Database Mirroring<br />Backups<br />User Defined<br />Etc…<br />Response<br />Execute a Job<br />Notify Operator<br />http://www.mssqltips.com/tip.asp?tip=939<br />http://www.mssqltips.com/tip.asp?tip=1564<br />30<br />
  31. 31. Central Management Server<br />New in SQL 2008<br />Allows you to register servers and fire off same query on all servers<br />Uses Windows Authentication Only<br />Can be used for SQL 2000, 2005 and 2008<br />http://www.mssqltips.com/tip.asp?tip=1767<br />31<br />
  32. 32. Other Tools<br />Trace<br />Perfmon<br />DMVs<br />http://www.mssqltips.com/tip.asp?tip=1035<br />http://www.mssqltips.com/tip.asp?tip=1211<br />http://www.mssqltips.com/tip.asp?tip=1722<br />http://www.mssqltips.com/category.asp?catid=31<br />32<br />
  33. 33. Startup Procedures<br />USE MASTERGOEXEC SP_PROCOPTION ‘SP_LOG_SERVER_START’, &apos;STARTUP&apos;, &apos;ON&apos;GO<br />USE MASTERGOEXEC SP_PROCOPTION ‘SP_LOG_SERVER_START’, &apos;STARTUP&apos;, ‘OFF&apos;GO<br />http://www.mssqltips.com/tip.asp?tip=1574<br />33<br />
  34. 34. Questions and Wrap-up<br />Thanks to our sponsor: Idera<br />Next webcast in the series: <br />“Under the Hood with SQL Server Fundamentals”<br />Don Jones<br />July 8th, 2009, 4pm EDT<br />https://www2.gotomeeting.com/register/449103978<br />Download SQL diagnostic manager<br />
  1. A particular slide catching your eye?

    Clipping is a handy way to collect important slides you want to go back to later.

×