Index Reorganization and Rebuilding for Success

32
INDEX REORGANIZATION AND REBUILDING FOR SUCCESS An end-to-end Enterprise re-indexing process

Transcript of Index Reorganization and Rebuilding for Success

Page 1: Index Reorganization and Rebuilding for Success

INDEX REORGANIZATION AND REBUILDING FOR SUCCESSAn end-to-end Enterprise re-indexing process

Page 2: Index Reorganization and Rebuilding for Success

ABSTRACTDean will introduce a process and accompanying tools to make Index reorganization/rebuilding successful. Applicable on small databases and multi-Tb databases with over 20,000 tables (including examples of what can go horribly wrong without the right preparation). Bonus: PowerShell monitoring script included.

Page 3: Index Reorganization and Rebuilding for Success

AGENDA• Indexing - What can go wrong?• Process Overview• Pre-requisites (initial setup/configuration)• Pre-index preparation• Running Index optimization• Index progress monitoring (awesome PowerShell script)

Page 4: Index Reorganization and Rebuilding for Success

WHAT COULD POSSIBLY GO WRONG?

Page 5: Index Reorganization and Rebuilding for Success

WHAT COULD POSSIBLY GO WRONG?

Lose your job BAD• Disk out of space

• Tlog or Database files• Tlog backup files

• Exponential Tlog growthTemporarily pain• Tlog or database growth initialization causing application timeout

Page 6: Index Reorganization and Rebuilding for Success

CONSIDERATIONS• Enterprise Edition

• Online Index rebuild• Tlog auto-growth settings

• % or size• Size planning

• Full backups during re-index• Tlog will not truncate during a full backup even if log backups are being taken• Big database (4 hr backup + recovery) use differentials during re-index?• TODO: Add log_reuse_wait_desc to Powershell monitoring tool when log used percent is >

warning threshold.• Re-Index vs. Re-Organize• MaxDOP?• TempDb

Page 7: Index Reorganization and Rebuilding for Success

PROCESS OVERVIEW

Page 8: Index Reorganization and Rebuilding for Success

PROCESS OVERVIEW

Page 9: Index Reorganization and Rebuilding for Success

INITIAL SYSTEM SETUP/CONFIGURATIONPre-requisites to using the proposed (demo) process

Page 10: Index Reorganization and Rebuilding for Success

OLA HALLENGREN’S INDEX MAINTENANCE SCRIPT

• https://ola.hallengren.com/ • Optional, but recommended• CommandLog table

Page 11: Index Reorganization and Rebuilding for Success

OLA’S INSTALL SCRIPTS• Demo uses a DBA AdminUtility

database (instead of master)• @CreateJobs set to Yes• @LogToTable set to Yes

Page 12: Index Reorganization and Rebuilding for Success

SQL AGENT JOBS• Ola’s scripts will add these jobs• The IndexOptimize –

USER_DATABASES job is the one of interest for indexing

Page 13: Index Reorganization and Rebuilding for Success

ADMINISTRATIVE DATABASE OR TABLES

Page 14: Index Reorganization and Rebuilding for Success

SP_WHOISACTIVE• Adam Machanic’s sp_WhoIsActive script is a new and improved version of

sp_who2• http://

sqlblog.com/blogs/adam_machanic/archive/tags/who+is+active/default.aspx

• Shows current connections, % progress of index re-organization, and much more

Page 15: Index Reorganization and Rebuilding for Success

SCHEDULED TASK

Page 16: Index Reorganization and Rebuilding for Success

SCHEDULED TASK - TRIGGER• How often to run during indexing

Page 17: Index Reorganization and Rebuilding for Success

SCHEDULED TASK - ACTION• ExecutionPolicy Bypass

Page 18: Index Reorganization and Rebuilding for Success

SCHEDULED TASK - RUN

Page 19: Index Reorganization and Rebuilding for Success

PRE-INDEX PREPARATION

Page 20: Index Reorganization and Rebuilding for Success

PRE-INDEX PREPARATION FOR VLDB

• Tlog backup schedule• Shorten duration

• Full backup schedule• Temporarily switch to differential backups

• Open monitoring tools or schedule the Sweet Powershell script

Page 21: Index Reorganization and Rebuilding for Success

INDEX MAINTENANCE

Page 22: Index Reorganization and Rebuilding for Success

INDEX MAINTENANCE• Ola Hallengren Index Maintenance Solution (recommended)

• Queryable maintenance database (read: Sweet PowerShell monitoring)• Post maintenance review (% fragmentation, re-org vs. rebuild, etc.)

• Maintenance Plans

Page 23: Index Reorganization and Rebuilding for Success

PROGRESS MONITORING

Page 24: Index Reorganization and Rebuilding for Success

INDEX MONITORING – OLD SCHOOL

• DMV – Disk Usage• Windows Explorer – .trn backup file sizes• Windows Explorer – tlog backup duration• sp_who2

Page 25: Index Reorganization and Rebuilding for Success

INDEX MONITORING – LIKE A BOSS

• Powershell Script• sp_WhoIsActive (by Adam Mechanic)• Queries

• Completed indexes• Current index rebuild progress (%, rows remaining)• Current index reorg progress (%)• Disk space used (Database files and Tlog• Tlog backup size and backup duration

• Windows Scheduler• Email

Page 26: Index Reorganization and Rebuilding for Success

TLOG ABOUT TO GROW• Transactions are paused while

TLOG file is growing• Truncation should prevent growth

depending on initial size• Autogrowth settings…

Page 27: Index Reorganization and Rebuilding for Success

DATA FILE ABOUT TO GROW

Page 28: Index Reorganization and Rebuilding for Success

TLOG ACTIVITY

Re-org (~1 Gb/minute) Re-build (~200Kb/minute)

Page 29: Index Reorganization and Rebuilding for Success

TLOG BACKUP DURATION• Durations shown in the query

don’t include the –WithVerify time• I usually use a 3X multiplier for

estimating actual tlog backup time• Look at start time of next backup

Page 30: Index Reorganization and Rebuilding for Success

INDIVIDUAL INDEX PROGRESS

Re-Org Re-Build

Page 31: Index Reorganization and Rebuilding for Success

WARNING SIGNS• VLDB data file about to grow• TLOG file about to grow• TLOG backup duration + Verify

taking longer than backup schedule schedule

• Log file not truncating (excessive growth)

• Wait times on sp_WhoIsActive• File sizes growing larger than

available disk space

Page 32: Index Reorganization and Rebuilding for Success

DEAN WILLSON• Director of Application Development, Aunt Millie’s Bakeries • Long time member of NUFW• Former President of fwPASS• Twitter: @deanwillson• Blogs: dean-o.blogspot.com, practicalhoshin.blogspot.com• LinkedIn: http://linkedin.com/in/deanwillson