Create Index:
CREATE NONCLUSTERED INDEX ix_IndexName ON dbo.TableName (Column1 ASC);
We can leave out some of the default values:
CREATE INDEX ix_IndexName ON dbo.TableName (Column1);
Create index with a unique constraint:
CREATE UNIQUE NONCLUSTERED INDEX ix_IndexName ON dbo.TableName (Column1);
Index with Included columns:
CREATE INDEX ix_IndexName ON dbo.TableName (Column1)
INCLUDE (Column2, Column3);
Disable and Re-enable Index:
Occasionally you may want to run a query without using an index, to compare how it runs without it. Or you may not want the indexes when you’re loading a lot of data. Instead of dropping the index, you can disable it, then re-enable with a REBUILD.
ALTER INDEX ix_IndexName ON dbo.TableName DISABLE; ALTER INDEX ix_IndexName ON dbo.TableName REBUILD;
You can use these commands to disable all indexes on a table.
ALTER INDEX ALL ON dbo.TableName DISABLE; ALTER INDEX ALL ON dbo.TableName REBUILD;
Drop Index:
SQL Server 2016 and later uses IF EXISTS, so it won’t try to drop an index if it does not exist.
DROP INDEX IF EXISTS ix_IndexName ON dbo.TableName;
Links: