Donnerstag, 12. Juni 2014

Deaktivieren und Aktivieren von Indizes per SQL-Script

(SQL-Server: Disable or Enable Index per SQL-Script)

Manchmal ist es sinnvoll vor bestimmten Operationen (zum Beispiel Massenverarbeitungen per Bulk Insert oder Bulk Copy) Indizes auszublenden.Eine Möglichkeit wäre hier das Löschen und Erstellen des betroffenen Index. Sollte hierbei aber ein Fehler auftreten und man arbeitet nicht in einer Transaktion, dann kann es passieren, dass der Index komplett gelöscht bleibt.

USE [Playground]
GO

-- Drop the Index "ProductIndex1"
DROP INDEX [ProductIndex1] ON [Product]
GO

-- Create the Index "ProductIndex1"
CREATE NONCLUSTERED INDEX [ProductIndex1] ON [Product]
(
[ID] ASC
)
WITH
(
PAD_INDEX = OFF,
STATISTICS_NORECOMPUTE = OFF,
SORT_IN_TEMPDB = OFF,
DROP_EXISTING = OFF,
ONLINE = OFF,
ALLOW_ROW_LOCKS = ON,
ALLOW_PAGE_LOCKS = ON
) ON [PRIMARY]
GO


Meiner Meinung nach ist die schönere Variante, das Deaktivieren mit anschließendem Reaktivieren bzw. Neu-Aufbau.


USE [Playground]
GO

-- Diable the Index "ProductIndex1"
ALTER INDEX [ProductIndex1] ON [Product] DISABLE
GO

-- Enable and Rebuild the Index "ProductIndex1"
ALTER INDEX [ProductIndex1] ON [Product] REBUILD
GO

Keine Kommentare:

Kommentar veröffentlichen