38
© 2012 House of Brick Technologies, LLC Maintain Your DBs (with Help from Ola Hallengren) SQL Saturday #154 St. Louis09/15/2012 Andy Galbraith Technical Consultant @DBA_ANDY

Sqlsat154 maintain your dbs with help from ola hallengren

Embed Size (px)

Citation preview

© 2012 House of Brick Technologies, LLC

Maintain Your DBs

(with Help from Ola Hallengren)

SQL Saturday #154 – St. Louis– 09/15/2012

Andy Galbraith – Technical Consultant @DBA_ANDY

© 2012 House of Brick Technologies, LLC

About House of Brick Technologies

14 year old Omaha-based company

Leader: Tier-1 VMware, Database Performance

Rock-solid reputation for optimizing the entire system

stack to maximize Tier-1 performance

House of Brick key service value components

Hybrid/private cloud architectures for complex Tier-1 workloads

Legacy to virtualization, and private/hybrid cloud system replatforming

SQL Server and Oracle virtualization specialties

Short term assessments and proof-of-concept projects

Long-term project analysis, PM, implementation, & validation

© 2012 House of Brick Technologies, LLC

About Me

SQL Server Consultant

10+ years of experience in

SQL Server, clustering,

performance tuning

Andy Galbraith

@DBA_Andy

© 2012 House of Brick Technologies, LLC

What is Database Maintenance?

© 2012 House of Brick Technologies, LLC

Why Do Database Maintenance?

© 2012 House of Brick Technologies, LLC

http://www.flickr.com/photos/damienmaloney/6967718013/sizes/z/in/photostream/

http://www.flickr.com/photos/z287marc/3189567558/sizes/m/in/photostream/

© 2012 House of Brick Technologies, LLC

Doesn’t SQL Server Have Maint Plans?

© 2012 House of Brick Technologies, LLC

#EPIC_FAIL

© 2012 House of Brick Technologies, LLC

The Ola Hallengren Maintenance Solution

DatabaseBackup

IndexOptimize

DatabaseIntegrityCheck

Cleanup Jobs

FREE!

© 2012 House of Brick Technologies, LLC

Hallengren Basics

Download the script

Full Script – MaintenanceSolution.sql – creates all procedures

and jobs

Individual Scripts for each procedure

Run the Script

Preferable to run into master or “tools” db if you have one

Schedule the Jobs

It’s just that easy…. (Did I mention *FREE*?)

© 2012 House of Brick Technologies, LLC

Hallengren Gotcha’s

Must be sysadmin to run MaintenanceSolution.sql

Only runs on SQL2005 and up

Set the @BackupDirectory near the top of the script before you run the script!

© 2012 House of Brick Technologies, LLC

Hallengren Logging

One of the greatest strengths of solution

By default, writes log files with explicit descriptions to default log path

Option to “LogToTable”

Parameter in stored procedures

Writes to CommandLog table in base database

© 2012 House of Brick Technologies, LLC

DatabaseBackup - Basics

dbo.DatabaseBackup Can configure directory path(s)**, etc.

Also:

Backup Software – Quest LiteSpeed, Red Gate SQLBackup, Red Gate HyperBAC, Idera SQLSafe

Number of Files

Compression

Encryption

BufferCount & MaxTransferSize

** Just Released Feature – can stripe backup across multiple directories!

© 2012 House of Brick Technologies, LLC

DatabaseBackup - Syntax

EXEC dbo.DatabaseBackup

@Databases=‘USER_DATABASES –BigArchiveDB’,

@Directory=‘F:\BACKUP’,

@BackupType=‘FULL’,

@Verify=‘Y’,

@Compress=‘Y’,

@CleanupTime=‘36’,

@LogToTable=‘Y’

© 2012 House of Brick Technologies, LLC

DatabaseBackup - Jobs

Four jobs by default – can always modify or create additional jobs!

DatabaseBackup - SYSTEM_DATABASES – FULL

DatabaseBackup - USER_DATABASES – FULL

DatabaseBackup - USER_DATABASES – DIFF

DatabaseBackup - USER_DATABASES – LOG

© 2012 House of Brick Technologies, LLC

DatabaseBackup – Job Syntax

Agent jobs use sqlcmd by default:

sqlcmd -E -S $(ESCAPE_SQUOTE(SRVR)) -d master -Q "EXECUTE [dbo].[DatabaseBackup] @Databases = 'USER_DATABASES', @Directory = N'D:\SQLDEVELOPER\Backup', @BackupType

= 'FULL', @Verify = 'Y', @CleanupTime = 24, @CheckSum = 'Y', @LogToTable = 'Y'" -b

© 2012 House of Brick Technologies, LLC

DatabaseIntegrityCheck - Basics

CHECKDB on selected databases

Same options as DBCC CHECKDB

© 2012 House of Brick Technologies, LLC

DatabaseIntegrityCheck - Syntax

EXEC dbo.DatabaseIntegrityCheck

@Databases = ‘SYSTEM_DATABASES’,

@Physical_Only = ‘N’,

@LogToTable = ‘Y’

© 2012 House of Brick Technologies, LLC

DatabaseIntegrityCheck - Jobs

Two jobs by default

DatabaseIntegrityCheck- USER_DATABASES

DatabaseIntegrityCheck- SYSTEM_DATABASES

© 2012 House of Brick Technologies, LLC

DatabaseIntegrityCheck - DEMO

© 2012 House of Brick Technologies, LLC

IndexOptimize - Basics

Check fragmentation levels of each index (sys.dm_db_index_physical_stats)

Reacts based on that level (low, medium, high)

Thresholds are user-configurable

Actions at each level are user-configurable

© 2012 House of Brick Technologies, LLC

IndexOptimize - Syntax

EXEC dbo.IndexOptimize

@Databases = ‘USER_DATABASES’,

@Update_Statistics = ‘ALL’,

@MAXDOP = 2,

@FillFactor = 80,

@LogToTable = ‘Y’

© 2012 House of Brick Technologies, LLC

IndexOptimize - Jobs

One job by default

IndexOptimize - USER_DATABASES

© 2012 House of Brick Technologies, LLC

IndexOptimize - DEMO

© 2012 House of Brick Technologies, LLC

Cleanup Jobs - Basics

sp_delete_backuphistory

sp_purge_jobhistory

Output File Cleanup

CommandLog Cleanup

© 2012 House of Brick Technologies, LLC

Scheduling

None of it matters if you don’t go back and

SCHEDULE the jobs!

I like these (but everywhere is different):

DatabaseIntegrityCheck – nightly

DatabaseBackup – nightly fulls/diffs with periodic

logs

IndexOptimize – weekly

Cleanup Jobs – monthly

Create a schedules script (script it from SSMS) and then you can run that script as a base after each MaintenanceSolution.sql install

© 2012 House of Brick Technologies, LLC

What if I run SQL Express?

Recommended Method:

Scheduled Tasks

Use sqlcmd similar to Agent Jobs

Configurable with same schedules, etc.

Still performs logging etc (stored proc parameters)

© 2012 House of Brick Technologies, LLC

What if I run SQL Express?

Not-Recommended Method (but it can work):

Scheduling Server

Install MaintenanceSolution.sql on Express instance

Create Hallengren jobs on a “real” SQL instance

with –S(erver) parameter modified to point to the

Express instance, like this:

sqlcmd -E -S ExpressServer$SQLEXPRESS -d master -Q

"EXECUTE dbo.DatabaseBackup @Databases =

'USER_DATABASES', @Directory = ‘F:\Backup',

@BackupType = 'FULL'" -b

© 2012 House of Brick Technologies, LLC

Hallengren Tips & Tricks

Can run DatabaseIntegrityCheck and exclude individual large tables or filegroups

EXECUTE dbo.DatabaseIntegrityCheck @Databases = 'AdventureWorks', @CheckCommands = 'CHECKALLOC,CHECKTABLE,CHECKCATALOG', @Objects = 'ALL_OBJECTS,-AdventureWorks.Person.Address‘

http://sqlblog.com/blogs/greg_low/archive/2012/07/02/run-database-checks-but-omit-large-tables-or-filegroups-new-option-in-ola-hallengren-s-scripts.aspx

© 2012 House of Brick Technologies, LLC

Hallengren Tips & Tricks

Can run DatabaseIntegrityCheck and exclude individual large tables or filegroups

EXECUTE dbo.DatabaseIntegrityCheck @Databases = 'AdventureWorks', @CheckCommands = 'CHECKALLOC,CHECKTABLE,CHECKCATALOG', @Objects = 'ALL_OBJECTS,-AdventureWorks.Person.Address‘

http://sqlblog.com/blogs/greg_low/archive/2012/07/02/run-database-checks-but-omit-large-tables-or-filegroups-new-option-in-ola-hallengren-s-scripts.aspx

© 2012 House of Brick Technologies, LLC

Hallengren Tips & Tricks

Use @UpdateStatistics and @OnlyModifiedStatistics to manage statistics on columns and/or indexes not being rebuilt

ALTER INDEX REBUILD (via IndexOptimize) automatically updates

stats on rebuilt indexes only (Remember REORG does not update

stats at all!)

To update other statistics, you *must* use the @UpdateStatistics

parameter on IndexOptimize

Uses UPDATE STATISTICS to force update

Can specify columns, indexes, or both

To only update statistics “that need it” (i.e., that have new data)

you can use the @OnlyModifiedStatistics parameter

Checks rowmodctr in sys.indexes to see if there are new rows since

the last statistics update

© 2012 House of Brick Technologies, LLC

Hallengren Tips & Tricks

“BACKUP LOG cannot be performed because there is no current database backup”

Now what?

DatabaseBackup will verify that an appropriate “parent” backup exists, and if it does not, will take a full backup rather than a

log/differential if the @ChangeDatabaseType parameter is set to ‘Y’

Be Careful (You may not want a FULL in the middle of the day!)

© 2012 House of Brick Technologies, LLC

Contact Ola

[email protected]

Ola is very responsive (but remember he does this for free on his own time!)

http://ola.hallengren.com

Frequently updated

Not on Twitter (yet)

© 2012 House of Brick Technologies, LLC

References

http://ola.hallengren.com/

http://www.simple-talk.com/sql/database-administration/automate-and-improve-your-database-maintenance-using-ola-hallengrens-free-script/

© 2012 House of Brick Technologies, LLC

Questions?

© 2012 House of Brick Technologies, LLC

Maintain Your DBs

(with Help from Ola Hallengren)

SQL Saturday #154 – St. Louis – 09/15/2012

Andy Galbraith – Technical Consultant @DBA_ANDY