SQL Server creates an index (index)

Index Description:

Index is divided into clustered index and non-clustered index, the database index is similar to catalog a book, you can quickly find the information you want in a book by catalog, without the need to read the book.

The main purpose of the index is to improve the performance of SQL Server system, speed up queries data and reduce the response time of the system.

But the index to improve query performance is not a panacea, nor is it the more established index, the better. Index built less, with a WHERE clause to find the low efficiency of the data, is not conducive to finding data. Index build more, is not conducive to add, modify, and delete operations because doing these operations, SQL SERVER In addition to updating the data table itself, but also jointly and severally Update Now all the index, and the index will waste too much hard disk space.

 

Index Category:

Chinese dictionary index is similar to the previous directory, according to the Ministry of pinyin or capital can quickly locate the word you are looking for.

The only index (UNIQUE): an index value for each row is unique (to create a unique constraint, the system will automatically create a unique index)

Primary key index: specified when creating a table primary key column, it will automatically create a primary key index, and has unique characteristics.

Clustered index (CLUSTERED): clustered index is equivalent to using a dictionary phonetic search, because records are stored on a clustered index physically continuous existence, that is a phonetic past behind is certainly the same as b.

Non-clustered index (NONCLUSTERED): non-clustered index is equivalent to the use of the dictionary lookup radical, non-clustered index is continuous, a continuous physical storage is not logical.

PS: a clustered index table can have only one clustered index instead of a table can be multiple.

Under what circumstances the use of the index:

grammar:

CREATE [ UNIQUE ] [ CLUSTERED | NONCLUSTERED ] INDEX index_name   
    ON <object> ( column_name [ ASC | DESC ] [ ,...n ] )   
    [ WITH <backward_compatible_index_option> [ ,...n ] ]  
    [ ON { filegroup_name | "default" } ]  
  
<object> ::=  
{  
    [ database_name. [ owner_name ] . | owner_name. ]   
    table_or_view_name  
}  
  
<backward_compatible_index_option> ::=  
{   
    PAD_INDEX  
  | FILLFACTOR = fillfactor  
  | SORT_IN_TEMPDB  
  | IGNORE_DUP_KEY  
  | STATISTICS_NORECOMPUTE   
  | DROP_EXISTING   
}

parameter:

UNIQUE: Create a unique index for the table or view. Unique index does not allow two rows with the same index key. Aggregate indexed view must be unique. If you want to build a unique index columns have duplicate values, you must first delete the duplicates.

CLUSTERED: indicates that the specified index created for the clustered index. Creating an index, the logical order of the key determines the physical order of the corresponding row in the table. Clustered index underlying (or leaf level) contains the actual data row of the table.

NONCLUSTERED: indicates that the specified index created by a non-clustered index. Creating a logical ordering of the specified table index. For non-clustered index, the physical order of the data lines independently to the index sort.

index_name: Specifies the index represents the created name.

database_name: indicates that the specified database name.

owner_name: indicates that the specified owner.

table: indicates that the specified name of the table to create the index.

view: represents the name of the view to create the index specified.

column: one or more columns in the index is based. Specifies two or more column names, a combination index can be created as a combination of the specified column.

[ASC | DESC]: indicates that the specified ascending or descending order of the column direction of a particular index. The default is ASC.

on filegroup_name: create an index to a specified file group. If the position is not specified and the table or view is not partitioned, the index is the base table or view the same document set. The document group must already exist.

on default: Create a file designated as the default index group.

PAD_INDEX = {ON | OFF}: Specifies whether the index is filled. The default is OFF.

  ON through the specified percentage of free space fillfactor is applied to the intermediate level index pages.

  OFF or fillfactor is not specified, taking into account the keyset on page intermediate stage, the intermediate level page to be filled close to its capacity, the extent to leave enough space so that it can hold at the maximum row index.

  PAD_INDEX option is only specified when FILLFACTOR useful because PAD_INDEX use a percentage specified by FILLFACTOR.

FILLFACTOR = fillfactor: a specified percentage when creating an index, each index page data representing the size of the index page, the fillfactor value of 1 to 100.

SORT_IN_TEMPDB = {ON | OFF}: intermediate sort result used when creating the specified index tempdb database stored. The default is OFF.

  Intermediate sort results are stored in the index for generating ON tempdb. This may reduce the time required to create only when the index is in a different disk set tempdb and user database. 

  OFF index intermediate sort results stored in the same database.

IGNORE_DUP_KEY = {ON | OFF}: Specifies the insert operation attempts to insert a duplicate key when the error response to a unique index. The default is OFF.

  ON warning message appears when duplicate key inserted into a unique index. Only the rows violating the uniqueness constraint will fail.

  Error message when OFF duplicate key inserted into a unique index. INSERT whole operation will be rolled back.

STATISTICS_NORECOMPUTE = {ON | OFF}: statistical index is used to specify whether the expired automatically recalculated. The default is OFF.

  ON does not automatically recalculate the outdated statistics.

  OFF Enables statistics auto-update feature.

DROP_EXISTING = {ON | OFF}: indicates if the index will still drop off the table and then create a new one. The default is OFF.

  ON you want to delete and rebuild an existing index, which must have the same name as a parameter index_name.

  OFF specify not to delete and rebuild the existing index. If the specified index name already exists, SQL Server will display an error.

ONLINE = {ON | OFF}: indicates whether to allow normal access for indexing, i.e., whether the table lock. The default is OFF.

  ON it forces the table remains in effect for general access, and does not create any index and prevent users from using / table lock.

  OFF operation of the index table will lock table, for full and efficient access to the table.

example:

Create a unique clustered index:

- Create a unique clustered index on 
the Create UNIQUE clustered         - means creating a unique clustered index 
index UQ_Clu_StuNo         - index name 
ON Student (S_StuNo)         - table name (column name to establish the index) 
with 
( 
    PAD_INDEX = ON, - indication filling 
    fillfactor = 50, -% expressed a fill factor of 50 
    ignore_dup_key = ON, - represents the insert duplicate values ignores duplicate values into a unique index 
    statistics_norecompute = OFF - indicate statistical information to enable the automatic update feature 
)

Create a unique non-clustered index:

- Create a unique non-clustered index on 
the Create UNIQUE NONCLUSTERED         - represents the creation of a unique non-clustered index 
index UQ_NonClu_StuNo         - index name 
ON Student (S_StuNo)         - table name (indexed column name) 
with 
( 
    PAD_INDEX = ON, - expressed use fill 
    fillfactor = 50, -% expressed a fill factor of 50 
    ignore_dup_key = ON, - represents the insert duplicate values ignores duplicate values into a unique index 
    statistics_norecompute = OFF - indicate statistical information to enable the automatic update feature 
)


 - to create a clustered index 
create Clu_Index index clustered 
ON Student (S_StuNo) 
with (DROP_EXISTING = ON)    

 - create a non-clustered index
create nonclustered index NonClu_Index
on Student(S_StuNo)
with (drop_existing=on)    

--创建唯一索引
create unique index NonClu_Index
on Student(S_StuNo)
with (drop_existing=on)

PS: When create index, if you do not specify clustered and nonclustered, then the default is nonclustered.

Create a non-clustered composite index:

- Create a non-clustered composite index 
the Create NONCLUSTERED index Index_StuNo_SName 
ON Student (S_StuNo, s_name) 
with (DROP_EXISTING = ON)

 - create a non-clustered composite index, no default non-clustered index on 
the Create index Index_StuNo_SName 
ON Student (S_StuNo, s_name) 
with (DROP_EXISTING = ON)

CREATE INDEX statement in clause INCLUDE, you can be defined non-key columns included (i.e. cover index) when creating an index, the following syntax structure:

CREATE NONCLUSTERED INDEX index name 
ON {table |} view names (column names [the ASC | DESC] [, ... n-]) 
the INCLUDE ( <1 column name>, <name Column 2> , [, ... n-])


 - - create a non-clustered index covering 
the create nONCLUSTERED index NonClu_Index 
ON Student (S_StuNo) 
the include (s_name, S_Height) 
with (DROP_EXISTING = ON)

 - create a non-clustered index coverage, no default non-clustered index on 
the create index NonClu_Index 
ON Student (S_StuNo) 
the include (s_name, S_Height) 
with (DROP_EXISTING = ON)

PS: You can not create a clustered index contains the index of non-key columns.

Create a filtered index:

- Create a non-clustered index screening 
the Create NONCLUSTERED index Index_StuNo_SName 
ON Student (S_StuNo) 
the WHERE S_StuNo > = 001 and S_StuNo <= 020 
with (DROP_EXISTING = ON)

 - create a non-clustered index screening, no default non-clustered index 
create index Index_StuNo_SName 
Student ON (S_StuNo) 
WHERE S_StuNo > S_StuNo = 001 and <= 020 
with (DROP_EXISTING = ON)

Modify the index:

- modify the index Syntax 
ALTER INDEX {index name | ALL} 
the ON <table | view name> 
{REBUILD   | the DISABLE |} the REORGANIZE [;]

REBUILD: indicates that the specified rebuild the index.

DISABLE: indicates that the specified index will be marked as disabled.

REORGANIZE: indicates that the specified index leaf level will be reorganized.

- Disable called NonClu_Index index 
alter index NonClu_Index on Student disable

Delete, and view the index:

- Check the index specified in the table Student 
Exec sp_helpindex Student    

 - delete the index for the specified table Student named Index_StuNo_SName the 
drop index Student.Index_StuNo_SName

 - Checklist Student UQ_S_StuNo index fragmentation information 
dbcc SHOWCONTIG (Student, UQ_S_StuNo)

 - Finishing index UQ_S_StuNo the Test database tables Student debris 
dbcc INDEXDEFRAG (Test, Student, UQ_S_StuNo)

 - all of the index table is updated Student statistics 
update statistics Student

The index is defined principles:

Avoid frequently updated tables too many indexes, and the index of the column as little as possible. While the field is often used to query the index should be created, but avoid adding unnecessary fields.

In the conditional expression often used to build the index on more different values ​​in the column, not indexed on fewer different values ​​in the column.

Frequent in the column index to sort or group (ie ORDER BY or GROUP BY operation), if the column to be sorted with a plurality of, can create a composite index on these columns.

In selecting an index key, as with a small column as the data type of the key so that each index page can accommodate as many keys and pointer index, in this way, a query can traverse the index page required to minimize in addition, as much as possible using an integer key, because access to the fastest integer.

reference:

https://www.cnblogs.com/Brambling/p/6754993.html

http://www.cnblogs.com/knowledgesea/p/3672099.html

https://msdn.microsoft.com/zh-cn/library/ms188783.aspx

Guess you like

Origin www.cnblogs.com/kkxwze/p/11307945.html