Index Optimize is Ola Hallengren’s IndexOptimize procedure, which rebuilds or reorganises indexes only when they pass a fragmentation threshold and updates the statistics that have changed. It’s what I use for weekly index and statistics maintenance instead of a maintenance plan. Microsoft’s guide to optimising index maintenance covers the background.
Installing Index Optimize
This post used to hold a 2016 copy of Ola Hallengren’s IndexOptimize procedure. Ola keeps his Maintenance Solution up to date (version 2026-09-12 at the time of writing), so always install the current version from his site rather than an old copy:
- SQL Server Index and Statistics Maintenance – documentation and examples
- Downloads – MaintenanceSolution.sql installs IndexOptimize together with CommandExecute and CommandLog, which it needs
- GitHub – the same scripts, with release notes
Running Index Optimize
Once installed, this is how I run it from a SQL Agent job step (rebuild or reorganise fragmented indexes and update statistics that have changed):
EXECUTE dbo.IndexOptimize
@Databases = 'USER_DATABASES',
@FragmentationLow = NULL,
@FragmentationMedium = 'INDEX_REORGANIZE,INDEX_REBUILD_ONLINE,INDEX_REBUILD_OFFLINE',
@FragmentationHigh = 'INDEX_REBUILD_ONLINE,INDEX_REBUILD_OFFLINE',
@FragmentationLevel1 = 5,
@FragmentationLevel2 = 30,
@UpdateStatistics = 'ALL',
@OnlyModifiedStatistics = 'Y',
@LogToTable = 'Y';
The job that runs it is in the Index_job post. To check whether statistics are the real problem rather than fragmentation, see Reading Execution Plans.
From my SQL Server DBA handbook.
One thought on “Index Optimize”
Comments are closed.