An index rebuild, also called reindexing, is a maintenance operation that drops and recreates an index structure from the current table data, producing a compact, freshly ordered index without the fragmentation, empty space and dead entries that accumulate through ongoing writes. In databases, it is a core task of indexing maintenance alongside statistics updates and lighter reorganization.
Rebuilding applies mainly to B-tree index structures, but most engines can also rebuild hash, GIN, GiST, full-text and spatial indexes. The operation is resource intensive, so administrators weigh its benefits against locking, I/O load and log growth.
How an index rebuild works internally
An index rebuild reads every indexed key from the table, sorts the keys and writes a new set of index pages in order, filled to a configured fill factor. When the new structure is complete, the engine swaps it in for the old index and releases the old pages. Because the new pages are written sequentially, the logical order matches the physical order on disk.
The rebuild repairs two kinds of degradation. Internal fragmentation means pages are only partly full after deletions and page splits, so more pages must be read for the same number of keys. External fragmentation means pages are out of physical sequence, which hurts range scans on spinning disks and, to a smaller degree, read-ahead on solid-state storage. In MVCC engines such as PostgreSQL, a third problem appears: index bloat from entries that point to dead row versions.
The table below lists the main steps and what each consumes.
| Phase | Work performed | Main resource used |
|---|---|---|
| Table scan | Read all rows and extract key values | Disk I/O |
| Sort | Order keys, often spilling to temporary storage | Memory and temp space |
| Page build | Write leaf and branch pages at the fill factor | Disk I/O and log |
| Swap and cleanup | Replace the old index and free its pages | Short lock on metadata |
| Statistics refresh | Update index statistics in some engines | CPU |
Rebuild compared with other index maintenance
A rebuild is the heaviest of several maintenance options, and choosing the lightest effective one avoids unnecessary load. Microsoft SQL Server distinguishes REBUILD from REORGANIZE; PostgreSQL offers REINDEX and relies on VACUUM for routine cleanup; Oracle provides ALTER INDEX REBUILD and a lighter COALESCE.
| Operation | What it does | Typical impact |
|---|---|---|
| Full rebuild | Recreates the index from table data | High I/O, may block writes |
| Online rebuild | Rebuilds while allowing reads and writes | Longer runtime, extra space |
| Reorganize or coalesce | Compacts and reorders existing leaf pages | Low impact, always online |
| Statistics update | Refreshes distribution data for the query planner | Light, no structural change |
| Vacuum | Marks dead entries reusable in MVCC engines | Light, does not shrink files |
Many apparent fragmentation problems are actually stale statistics. A rebuild in SQL Server also refreshes statistics with a full scan, so a performance gain after a rebuild is sometimes caused by better query plans rather than less fragmentation. Updating statistics alone is the cheaper first test.
When an index rebuild is justified
An index rebuild is justified when measured degradation affects real queries, not merely when a fragmentation percentage crosses a threshold. Widely cited guidance once suggested reorganizing between roughly 5 and 30 percent fragmentation and rebuilding above 30 percent, but these figures were rules of thumb, and current advice treats them as a starting point only.
The decision table summarizes situations where reindexing is commonly warranted.
| Situation | Recommended action | Reason |
|---|---|---|
| Heavy bloat after mass deletes | Rebuild the affected index | Reclaims pages that vacuum cannot shrink |
| Index corruption reported | Rebuild immediately | Restores a consistent structure |
| Fill factor change needed | Rebuild with the new setting | Only a rebuild applies a new fill factor |
| Mild fragmentation, stable latency | Leave as is | Cost exceeds measurable benefit |
| Slow plans after data shifts | Update statistics first | Plan quality, not structure, is the issue |
Size also matters. Fragmentation in an index of a few hundred pages rarely affects performance, because the whole structure fits in memory. Rebuilding small indexes adds load without benefit.
Online and offline rebuild options
The locking behavior of a rebuild determines whether it can run during business hours. An offline rebuild holds a lock that blocks writes, and sometimes reads, on the table for the full duration. It is faster and needs less extra space, but it suits only maintenance windows.
An online rebuild, available as REBUILD WITH (ONLINE = ON) in SQL Server editions that support it and as REINDEX CONCURRENTLY in PostgreSQL 12 and later, builds a new copy while tracking concurrent changes. It needs roughly the index size again in free space, runs longer and still takes brief locks at the start and end. A failed concurrent rebuild in PostgreSQL can leave an invalid index that must be dropped manually.
Resumable rebuilds, offered by recent SQL Server versions, allow a long operation to be paused and resumed, which helps fit large indexes into short maintenance windows.
Risks and common mistakes in reindexing
Reindexing carries operational risks that are easy to underestimate. Many problems come from scheduled jobs that rebuild everything regardless of need.
- Transaction log growth can be large, which affects log shipping, replication lag and backup sizes.
- Blanket nightly rebuilds waste I/O and can evict hot data from the buffer cache.
- Insufficient free disk space causes online rebuilds to fail partway through.
- A fill factor of 100 on a write-heavy index causes rapid page splits and immediate refragmentation.
- Replica lag grows when large rebuilds generate heavy write-ahead log traffic.
A sound practice is to monitor fragmentation, bloat and query latency together, rebuild only the indexes that show a measurable problem, and schedule large operations when replication and backup jobs can absorb the extra load.
In everyday language
A database index works like the index at the back of a book. Over time, as pages are added and removed, that index gets messy: entries are scattered, some point to pages that no longer exist, and there are gaps everywhere.
An index rebuild is like throwing out the messy index and writing a clean one from the current book. Lookups become quick again. The catch is that rewriting takes effort, so it is worth doing only when the mess actually slows people down.
Seen in the real world
An online retailer runs PostgreSQL with an orders table of about 400 million rows. After a data retention job deletes three years of old orders, the main index on order date still occupies about 38 GB, although the remaining data needs far less. Range queries for monthly reports have slowed noticeably.
The database administrator confirms the bloat with an extension that measures index density and runs REINDEX INDEX CONCURRENTLY on a weekday evening. The operation takes about two hours, needs extra disk space during the build and generates heavy write-ahead log traffic, which the team watches on the replicas.
Afterward, the index shrinks to about 11 GB and the monthly report query drops from 40 seconds to under 9. The team then replaces its old nightly rebuild-everything job with targeted reindexing based on bloat measurements.