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

Más contenido relacionado

La actualidad más candente

Rest API using Flask & SqlAlchemy
Rest API using Flask & SqlAlchemyRest API using Flask & SqlAlchemy
Rest API using Flask & SqlAlchemyAlessandro Cucci
 
Inside Parquet Format
Inside Parquet FormatInside Parquet Format
Inside Parquet FormatYue Chen
 
R12 d49656 gc10-apps dba 20
R12 d49656 gc10-apps dba 20R12 d49656 gc10-apps dba 20
R12 d49656 gc10-apps dba 20zeesniper
 
Postgresql Database Administration- Day4
Postgresql Database Administration- Day4Postgresql Database Administration- Day4
Postgresql Database Administration- Day4PoguttuezhiniVP
 
R12 d49656 gc10-apps dba 25
R12 d49656 gc10-apps dba 25R12 d49656 gc10-apps dba 25
R12 d49656 gc10-apps dba 25zeesniper
 
R12 d49656 gc10-apps dba 10
R12 d49656 gc10-apps dba 10R12 d49656 gc10-apps dba 10
R12 d49656 gc10-apps dba 10zeesniper
 
Import and Export Big Data using R Studio
Import and Export Big Data using R StudioImport and Export Big Data using R Studio
Import and Export Big Data using R StudioRupak Roy
 
Import Database Data using RODBC in R Studio
Import Database Data using RODBC in R StudioImport Database Data using RODBC in R Studio
Import Database Data using RODBC in R StudioRupak Roy
 
Solr Application Development Tutorial
Solr Application Development TutorialSolr Application Development Tutorial
Solr Application Development TutorialErik Hatcher
 
R12 d49656 gc10-apps dba 12
R12 d49656 gc10-apps dba 12R12 d49656 gc10-apps dba 12
R12 d49656 gc10-apps dba 12zeesniper
 
R12 d49656 gc10-apps dba 11
R12 d49656 gc10-apps dba 11R12 d49656 gc10-apps dba 11
R12 d49656 gc10-apps dba 11zeesniper
 
AWR DB performance Data Mining - Collaborate 2015
AWR DB performance Data Mining - Collaborate 2015AWR DB performance Data Mining - Collaborate 2015
AWR DB performance Data Mining - Collaborate 2015Yury Velikanov
 
Apache Drill Workshop
Apache Drill WorkshopApache Drill Workshop
Apache Drill WorkshopCharles Givre
 
Oracle Open World Thursday 230 ashmasters
Oracle Open World Thursday 230 ashmastersOracle Open World Thursday 230 ashmasters
Oracle Open World Thursday 230 ashmastersKyle Hailey
 

La actualidad más candente (19)

Rest API using Flask & SqlAlchemy
Rest API using Flask & SqlAlchemyRest API using Flask & SqlAlchemy
Rest API using Flask & SqlAlchemy
 
Inside Parquet Format
Inside Parquet FormatInside Parquet Format
Inside Parquet Format
 
R12 d49656 gc10-apps dba 20
R12 d49656 gc10-apps dba 20R12 d49656 gc10-apps dba 20
R12 d49656 gc10-apps dba 20
 
Postgresql Database Administration- Day4
Postgresql Database Administration- Day4Postgresql Database Administration- Day4
Postgresql Database Administration- Day4
 
R12 d49656 gc10-apps dba 25
R12 d49656 gc10-apps dba 25R12 d49656 gc10-apps dba 25
R12 d49656 gc10-apps dba 25
 
R12 d49656 gc10-apps dba 10
R12 d49656 gc10-apps dba 10R12 d49656 gc10-apps dba 10
R12 d49656 gc10-apps dba 10
 
Using AWR for SQL Analysis
Using AWR for SQL AnalysisUsing AWR for SQL Analysis
Using AWR for SQL Analysis
 
Import and Export Big Data using R Studio
Import and Export Big Data using R StudioImport and Export Big Data using R Studio
Import and Export Big Data using R Studio
 
Import Database Data using RODBC in R Studio
Import Database Data using RODBC in R StudioImport Database Data using RODBC in R Studio
Import Database Data using RODBC in R Studio
 
Les 07 Rman Rec
Les 07 Rman RecLes 07 Rman Rec
Les 07 Rman Rec
 
Solr Application Development Tutorial
Solr Application Development TutorialSolr Application Development Tutorial
Solr Application Development Tutorial
 
R12 d49656 gc10-apps dba 12
R12 d49656 gc10-apps dba 12R12 d49656 gc10-apps dba 12
R12 d49656 gc10-apps dba 12
 
R12 d49656 gc10-apps dba 11
R12 d49656 gc10-apps dba 11R12 d49656 gc10-apps dba 11
R12 d49656 gc10-apps dba 11
 
AWR DB performance Data Mining - Collaborate 2015
AWR DB performance Data Mining - Collaborate 2015AWR DB performance Data Mining - Collaborate 2015
AWR DB performance Data Mining - Collaborate 2015
 
SQL Server Stored procedures
SQL Server Stored proceduresSQL Server Stored procedures
SQL Server Stored procedures
 
Store procedures
Store proceduresStore procedures
Store procedures
 
Apache Drill Workshop
Apache Drill WorkshopApache Drill Workshop
Apache Drill Workshop
 
Les 03 Catalog
Les 03 CatalogLes 03 Catalog
Les 03 Catalog
 
Oracle Open World Thursday 230 ashmasters
Oracle Open World Thursday 230 ashmastersOracle Open World Thursday 230 ashmasters
Oracle Open World Thursday 230 ashmasters
 

Destacado

SQL Server Performance Counters 101
SQL Server Performance Counters 101SQL Server Performance Counters 101
SQL Server Performance Counters 101Sascha Lorenz
 
SQL Server Transaction Log Deep Dive Session - PASS Hamburg
SQL Server Transaction Log Deep Dive Session - PASS HamburgSQL Server Transaction Log Deep Dive Session - PASS Hamburg
SQL Server Transaction Log Deep Dive Session - PASS HamburgSascha Lorenz
 
Analyzing SQL Server wait stats, hands-on!
Analyzing SQL Server wait stats, hands-on!Analyzing SQL Server wait stats, hands-on!
Analyzing SQL Server wait stats, hands-on!Red Gate Software
 
SQL Server ASYNC_NETWORK_IO Wait Type Explained
SQL Server ASYNC_NETWORK_IO Wait Type ExplainedSQL Server ASYNC_NETWORK_IO Wait Type Explained
SQL Server ASYNC_NETWORK_IO Wait Type ExplainedConfio Software
 
Geek Sync I Surviving the Holidays with SQL Server
Geek Sync I Surviving the Holidays with SQL ServerGeek Sync I Surviving the Holidays with SQL Server
Geek Sync I Surviving the Holidays with SQL ServerIDERA Software
 
Quack Chat | Partitioning - Black Magic or Silver Bullet
Quack Chat | Partitioning - Black Magic or Silver BulletQuack Chat | Partitioning - Black Magic or Silver Bullet
Quack Chat | Partitioning - Black Magic or Silver BulletIDERA Software
 
Geek Sync I SQL Server 2016 Performance Tricks You Need to Know
Geek Sync I SQL Server 2016 Performance Tricks You Need to KnowGeek Sync I SQL Server 2016 Performance Tricks You Need to Know
Geek Sync I SQL Server 2016 Performance Tricks You Need to KnowIDERA Software
 
Geek Sync | Avoid Corruption Nightmares within your Virtual Database
Geek Sync | Avoid Corruption Nightmares within your Virtual DatabaseGeek Sync | Avoid Corruption Nightmares within your Virtual Database
Geek Sync | Avoid Corruption Nightmares within your Virtual DatabaseIDERA Software
 
Geek Sync | Kick Start SQL Server 2016 Performance Tips and Tricks
Geek Sync | Kick Start SQL Server 2016 Performance Tips and TricksGeek Sync | Kick Start SQL Server 2016 Performance Tips and Tricks
Geek Sync | Kick Start SQL Server 2016 Performance Tips and TricksIDERA Software
 
Foro Universidades 2014. Pensando en la nube - SharePoint como Web Corporativa
Foro Universidades 2014. Pensando en la nube - SharePoint como Web CorporativaForo Universidades 2014. Pensando en la nube - SharePoint como Web Corporativa
Foro Universidades 2014. Pensando en la nube - SharePoint como Web Corporativawww.encamina.com
 
Geek Sync | Using PowerShell with Python and SQL Server
Geek Sync | Using PowerShell with Python and SQL ServerGeek Sync | Using PowerShell with Python and SQL Server
Geek Sync | Using PowerShell with Python and SQL ServerIDERA Software
 
Geek Sync - Cloud Considerations
Geek Sync - Cloud ConsiderationsGeek Sync - Cloud Considerations
Geek Sync - Cloud ConsiderationsIDERA Software
 
Geek Sync I The Importance of Data Model Change Management
Geek Sync I The Importance of Data Model Change ManagementGeek Sync I The Importance of Data Model Change Management
Geek Sync I The Importance of Data Model Change ManagementIDERA Software
 
Quack Chat: Diving into Data Governance
Quack Chat: Diving into Data Governance Quack Chat: Diving into Data Governance
Quack Chat: Diving into Data Governance IDERA Software
 

Destacado (14)

SQL Server Performance Counters 101
SQL Server Performance Counters 101SQL Server Performance Counters 101
SQL Server Performance Counters 101
 
SQL Server Transaction Log Deep Dive Session - PASS Hamburg
SQL Server Transaction Log Deep Dive Session - PASS HamburgSQL Server Transaction Log Deep Dive Session - PASS Hamburg
SQL Server Transaction Log Deep Dive Session - PASS Hamburg
 
Analyzing SQL Server wait stats, hands-on!
Analyzing SQL Server wait stats, hands-on!Analyzing SQL Server wait stats, hands-on!
Analyzing SQL Server wait stats, hands-on!
 
SQL Server ASYNC_NETWORK_IO Wait Type Explained
SQL Server ASYNC_NETWORK_IO Wait Type ExplainedSQL Server ASYNC_NETWORK_IO Wait Type Explained
SQL Server ASYNC_NETWORK_IO Wait Type Explained
 
Geek Sync I Surviving the Holidays with SQL Server
Geek Sync I Surviving the Holidays with SQL ServerGeek Sync I Surviving the Holidays with SQL Server
Geek Sync I Surviving the Holidays with SQL Server
 
Quack Chat | Partitioning - Black Magic or Silver Bullet
Quack Chat | Partitioning - Black Magic or Silver BulletQuack Chat | Partitioning - Black Magic or Silver Bullet
Quack Chat | Partitioning - Black Magic or Silver Bullet
 
Geek Sync I SQL Server 2016 Performance Tricks You Need to Know
Geek Sync I SQL Server 2016 Performance Tricks You Need to KnowGeek Sync I SQL Server 2016 Performance Tricks You Need to Know
Geek Sync I SQL Server 2016 Performance Tricks You Need to Know
 
Geek Sync | Avoid Corruption Nightmares within your Virtual Database
Geek Sync | Avoid Corruption Nightmares within your Virtual DatabaseGeek Sync | Avoid Corruption Nightmares within your Virtual Database
Geek Sync | Avoid Corruption Nightmares within your Virtual Database
 
Geek Sync | Kick Start SQL Server 2016 Performance Tips and Tricks
Geek Sync | Kick Start SQL Server 2016 Performance Tips and TricksGeek Sync | Kick Start SQL Server 2016 Performance Tips and Tricks
Geek Sync | Kick Start SQL Server 2016 Performance Tips and Tricks
 
Foro Universidades 2014. Pensando en la nube - SharePoint como Web Corporativa
Foro Universidades 2014. Pensando en la nube - SharePoint como Web CorporativaForo Universidades 2014. Pensando en la nube - SharePoint como Web Corporativa
Foro Universidades 2014. Pensando en la nube - SharePoint como Web Corporativa
 
Geek Sync | Using PowerShell with Python and SQL Server
Geek Sync | Using PowerShell with Python and SQL ServerGeek Sync | Using PowerShell with Python and SQL Server
Geek Sync | Using PowerShell with Python and SQL Server
 
Geek Sync - Cloud Considerations
Geek Sync - Cloud ConsiderationsGeek Sync - Cloud Considerations
Geek Sync - Cloud Considerations
 
Geek Sync I The Importance of Data Model Change Management
Geek Sync I The Importance of Data Model Change ManagementGeek Sync I The Importance of Data Model Change Management
Geek Sync I The Importance of Data Model Change Management
 
Quack Chat: Diving into Data Governance
Quack Chat: Diving into Data Governance Quack Chat: Diving into Data Governance
Quack Chat: Diving into Data Governance
 

Similar a Sql Automation 20090610

Sql storeprocedure
Sql storeprocedureSql storeprocedure
Sql storeprocedureftz 420
 
Prog1 chap1 and chap 2
Prog1 chap1 and chap 2Prog1 chap1 and chap 2
Prog1 chap1 and chap 2rowensCap
 
Php classes in mumbai
Php classes in mumbaiPhp classes in mumbai
Php classes in mumbaiaadi Surve
 
Instrumenting plugins for Performance Schema
Instrumenting plugins for Performance SchemaInstrumenting plugins for Performance Schema
Instrumenting plugins for Performance SchemaMark Leith
 
Sql server performance tuning
Sql server performance tuningSql server performance tuning
Sql server performance tuningJugal Shah
 
Leveraging Open Source to Manage SAN Performance
Leveraging Open Source to Manage SAN PerformanceLeveraging Open Source to Manage SAN Performance
Leveraging Open Source to Manage SAN Performancebrettallison
 
SQL Server 2008 Development for Programmers
SQL Server 2008 Development for ProgrammersSQL Server 2008 Development for Programmers
SQL Server 2008 Development for ProgrammersAdam Hutson
 
SQL Server - High availability
SQL Server - High availabilitySQL Server - High availability
SQL Server - High availabilityPeter Gfader
 
Using Apache as an Application Server
Using Apache as an Application ServerUsing Apache as an Application Server
Using Apache as an Application ServerPhil Windley
 
Ultimate Free SQL Server Toolkit
Ultimate Free SQL Server ToolkitUltimate Free SQL Server Toolkit
Ultimate Free SQL Server ToolkitKevin Kline
 
SQLMAP Tool Usage - A Heads Up
SQLMAP Tool Usage - A  Heads UpSQLMAP Tool Usage - A  Heads Up
SQLMAP Tool Usage - A Heads UpMindfire Solutions
 
MySQL Scaling Presentation
MySQL Scaling PresentationMySQL Scaling Presentation
MySQL Scaling PresentationTommy Falgout
 
Sgf15 tips and tricks for sas program automation-final post1
Sgf15 tips and tricks for sas program automation-final post1Sgf15 tips and tricks for sas program automation-final post1
Sgf15 tips and tricks for sas program automation-final post1Adam Hood
 
Stored-Procedures-Presentation
Stored-Procedures-PresentationStored-Procedures-Presentation
Stored-Procedures-PresentationChuck Walker
 

Similar a Sql Automation 20090610 (20)

SQL Injection
SQL InjectionSQL Injection
SQL Injection
 
Sql storeprocedure
Sql storeprocedureSql storeprocedure
Sql storeprocedure
 
Prog1 chap1 and chap 2
Prog1 chap1 and chap 2Prog1 chap1 and chap 2
Prog1 chap1 and chap 2
 
Php classes in mumbai
Php classes in mumbaiPhp classes in mumbai
Php classes in mumbai
 
Instrumenting plugins for Performance Schema
Instrumenting plugins for Performance SchemaInstrumenting plugins for Performance Schema
Instrumenting plugins for Performance Schema
 
Sql server performance tuning
Sql server performance tuningSql server performance tuning
Sql server performance tuning
 
Leveraging Open Source to Manage SAN Performance
Leveraging Open Source to Manage SAN PerformanceLeveraging Open Source to Manage SAN Performance
Leveraging Open Source to Manage SAN Performance
 
SQL Server 2008 Development for Programmers
SQL Server 2008 Development for ProgrammersSQL Server 2008 Development for Programmers
SQL Server 2008 Development for Programmers
 
SQL Server - High availability
SQL Server - High availabilitySQL Server - High availability
SQL Server - High availability
 
PGDay India 2016
PGDay India 2016PGDay India 2016
PGDay India 2016
 
Using Apache as an Application Server
Using Apache as an Application ServerUsing Apache as an Application Server
Using Apache as an Application Server
 
Ultimate Free SQL Server Toolkit
Ultimate Free SQL Server ToolkitUltimate Free SQL Server Toolkit
Ultimate Free SQL Server Toolkit
 
11i Logs
11i Logs11i Logs
11i Logs
 
SQLMAP Tool Usage - A Heads Up
SQLMAP Tool Usage - A  Heads UpSQLMAP Tool Usage - A  Heads Up
SQLMAP Tool Usage - A Heads Up
 
Sherlock holmes for dba’s
Sherlock holmes for dba’sSherlock holmes for dba’s
Sherlock holmes for dba’s
 
MySQL Scaling Presentation
MySQL Scaling PresentationMySQL Scaling Presentation
MySQL Scaling Presentation
 
Mysql
MysqlMysql
Mysql
 
Sgf15 tips and tricks for sas program automation-final post1
Sgf15 tips and tricks for sas program automation-final post1Sgf15 tips and tricks for sas program automation-final post1
Sgf15 tips and tricks for sas program automation-final post1
 
Stored-Procedures-Presentation
Stored-Procedures-PresentationStored-Procedures-Presentation
Stored-Procedures-Presentation
 
PHP - Intriduction to MySQL And PHP
PHP - Intriduction to MySQL And PHPPHP - Intriduction to MySQL And PHP
PHP - Intriduction to MySQL And PHP
 

Sql Automation 20090610

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