Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Dropping/ Disabling index is better in sql server

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;
like image 706
Sam Avatar asked Dec 18 '12 17:12

Sam


People also ask

What happens if we disable index in SQL Server?

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.

Is it safe to drop index?

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.

Do I need to drop index if I drop table?

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.

When should I drop indexes?

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.


1 Answers

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.

like image 62
StackUser Avatar answered Sep 28 '22 10:09

StackUser