Epic Zone Technology

Month: July 2021

Enabling and Disabling an Index on Microsoft SQL Server

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

Microsoft SQL Server Üzerinde Bir Index’i Pasife Çekmek ve Aktifleştirmek

Yazının İngilizcesi için lütfen tıklayınız.

Bazen büyük bir insert ya da update sorgusu çalıştırmadan önce ya da sistem istatistiklerine bakarak artık ihtiyaç olmadığına karar verdiğimizde bir index’i pasife çekmek isteriz. Bir index’i pasife çekmek için DISABLE anahtar kelimesi kullanırız.

——————————————————————————
USE EpicZoneDatabase
GO

ALTER INDEX [IX_Production_CategoryId] ON [dbo.Production] DISABLE
GO
——————————————————————————

Geçen hafta yaptığımız bir danışmanlık çalışması sırasında bir geliştirici, aynı index’i aktifleştirmek için ENABLE anahtar kelimesinin neden hata verdiğini ve bunun yerine ne kullanmak gerektiğini sordu.

Çünkü Microsoft SQL Server üzerinde bir index tek başına aktifleştirilemez; o index’i rebuild etmemiz gerekir. Aşağıda sorgu, ilgili index’i rebuild ederek yeniden aktifleştirecektir.

——————————————————————————
USE EpicZoneDatabase
GO

ALTER INDEX [IX_Production_CategoryId] ON [dbo.Production] 
REBUILD WITH (ONLINE = ON);
GO
——————————————————————————

Önemli bir ek not olarak eklemek isteriz ki, eğer sisteminiz için aktif saatlerde bu işlemi yapıyorsanız, index’i rebuild ederken ONLINE opsiyonunu eklemek iyi olacaktır. ONLINE opsiyonu, diğer kullanıcıların veriyi güncellemesine ve erişmesine izin verir. Eğer rebuild işlemini (daha az disk yeri kaplayarak ve işlemin daha hızlı tamamlanması için) offline yapmayı tercih ederseniz, tablodaki veriye ve ilgili index’e özel kilit koyulur. Bu durum, index işlemi tamamlanana kadar ilgili veride güncelleme yapılmasını (update) ve verinin elde edilmesinin (select) önüne geçer; nihayetinde de SQL Server üzerinde deadlock’lar ile karşılaşabiliriz.

Powered by WordPress & Theme by Anders Norén