Index QueryExplain
Index QueryExplain
Creates a relational index on a table or view. Also called a rowstore index because it is either a clustered
or nonclustered B-tree index.
Simple syntax:
CREATE [ UNIQUE ] [ CLUSTERED | NONCLUSTERED ] INDEX index_name
ON <object> ( column [ ASC | DESC ] [ ,...n ] );
<object> ::=
{ database_name.schema_name.table_or_view_name | schema_name.table_or_view_name |
table_or_view_name }
Example:
-- Create a nonclustered index on a table or view
CREATE INDEX index1 ON schema1.table1 (column1);
-- Create a clustered index on a table and use a 3-part name for the table
CREATE CLUSTERED INDEX index1 ON database1.schema1.table1 (column1);
An index is an on-disk structure associated with a table or view that speeds retrieval of rows from the
table or view. An index contains keys built from one or more columns in the table or view. These keys are
stored in a structure (B-tree) that enables SQL Server to find the row or rows associated with the key
values quickly and efficiently.
- Clustered:
o Clustered indexes sort and store the data rows in the table or view based on their
key values.
o There can be only one clustered index per table, because the data rows themselves
can be stored in only one order.
o The only time the data rows in a table are stored in sorted order is when the table
contains a clustered index.
o When a table has a clustered index, the table is called a clustered table. If a table has
no clustered index, its data rows are stored in an unordered structure called a heap.
- Nonclustered
o Nonclustered indexes have a structure separate from the data rows.
o A nonclustered index contains the nonclustered index key values and each key
value entry has a pointer to the data row that contains the key value.
- Clustered indexes only sort tables. Therefore, they do not consume extra storage. Non-
clustered indexes are stored in a separate place from the actual table claiming more storage
space.
- Clustered indexes are faster than non-clustered indexes since they don’t involve any
extra lookup step.
https://docs.microsoft.com/en-us/sql/relational-databases/indexes/clustered-and-nonclustered-indexes-
described?view=sql-server-ver15
- Indexes are automatically maintained for a table or view whenever the table data is modified.
2) Drop index:
Simple syntax:
DROP INDEX [ IF EXISTS] index_name ON <object>