I am writing this a follow up to Changing data type of column in SQL Server
My question earlier was if I need to remove all indexes and constraints and it was answered I do need to remove them.
So as I was surf internet on the topic I came across a few post saying its better to disable and enable an index, rather than removing and recreating them .
So which is better way of doing it? Does disabling of index allow you to change the data type of the column as well? What is the difference between both?
Statement with dropping and creating index
DROP INDEX UX_1_COMPUTATION ON dbo.Computation
ALTER TABLE dbo.Computation
ALTER COLUMN ComputationID NVARCHAR(25) not null
CREATE UNIQUE INDEX UX_1_COMPUTATION ON dbo.Computation (ComputationID);
Statement with disabling and enabling index
ALTER INDEX [UX_1_COMPUTATION ] ON dbo.Computation DISABLE
ALTER TABLE dbo.Computation
ALTER COLUMN ComputationID NVARCHAR(25) not null
ALTER INDEX [UX_1_COMPUTATION ] ON dbo.Computation REBUILD;
Disabling an index prevents user access to the index, and for clustered indexes to the underlying table data. The index definition remains in metadata, and index statistics are kept on nonclustered indexes. Disabling a clustered index on a view or a nonclustered index physically deletes the index data.
The dangers of deleting indexesRecreating the index might take long : if the table is big (or extremely big) it could take minutes to hours to recreate the index, impacting your application the entire time. Plus, the process of recreating the index adds additional CPU load.
Dropping a table removes the table definition from the data dictionary. All rows of the table are no longer accessible. All indexes and triggers associated with a table are dropped.
Use the DROP INDEX statement to remove an index or domain index from the database. When you drop an index, Oracle Database invalidates all objects that depend on the underlying table, including views, packages, package bodies, functions, and procedures.
Does disabling of index allow you to change the data type of the column as well?
It is based on the index type you choosed. You cannot modify a column data type when an clustered index on the table is disabled. If your index is a non clustered index then you can.
What is the difference between both?
The biggest difference between disabling and dropping an index is whether the metadata and statistics are persisted. If disabled, they are. If dropped, they are not. Make sure you carefully weigh your options before performing either action, and always have a way to recreate the index available.
which is better way of doing it?
In your case I would suggest drop and recreate indexes.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With