Sql-server – Rebuilding index

indexsql serversql-server-2008-r2transaction-log

I have a transaction database and one of nightly job is to rebuild indexes that followed by a log backup and then shrink the log.

Is this safe to do so ?

Thanks
Antony

Best Answer

Why exactly are you choosing the shrink the log routinely? Is it because you need to reclaim volume space? Hard disk space is so cheap that shrinking the log really shouldn't be a go-to maintenance item. Especially since you are seeing one of the main reasons not to shrink the log: Because your logged operations actually do need that additional log space.

Stop shrinking your log. Find the extra space somewhere so that your transaction log can remain that larger size. You haven't given us any numbers (initial size, size the log file eventually grows to, etc.) but the growth operation is expensive and can be time consuming. Log files can't even benefit from a file allocation optimization like Instant File Initialization.

If you are worried about the index rebuild, it can be a minimally logged operation and you could utilize the bulk logged recovery model. In that case, you could take a log backup prior to, then switch to the bulk logged recovery model, do your index rebuilds, then set your recovery model back to full. This could potentially prevent you from doing a point-in-time recovery during that time span that you are in the bulk logged recovery model, so that may be a DR consideration.

There are strategies here. But shrinking your log every night should be on the bottom of that list.

As for the rest of your question, rebuilding indexes (depending on their fragmentation level, your maintenance window, etc.) is a prudent and common maintenance task.