For Turkish version, please click here.
We sometimes need to disable an index; either before a bulk and large insert/update or we won’t use that index anymore according to the system statistics. To disable an index on a table, we use DISABLE keyword.
——————————————————————————
USE EpicZoneDatabase
GO
ALTER INDEX [IX_Production_CategoryId] ON [dbo.Production] DISABLE
GO
——————————————————————————
To enable the same index, I’ve seen a developer last week using the ENABLE keyword, which results in error.
Because an index cannot be only enabled after it is disabled; we must rebuild it. The following syntax will enable and rebuild the index.
——————————————————————————
USE EpicZoneDatabase
GO
ALTER INDEX [IX_Production_CategoryId] ON [dbo.Production] REBUILD WITH (ONLINE = ON);
GO
——————————————————————————
As an important side note, we better use ONLINE keyword while rebuilding an index, if it is an active hour for your system. ONLINE option allows other users can continue to update and query the data. If we perform rebuild offline, it holds exclusive locks on the table data and related indexes. This prevents modifications and queries to the underlying data until the index operation is complete; so you may encounter deadlocks on your SQL Server