SQL SERVER [index]

In doing development process often come into contact database indexes not only need to know the index only DBA knowledge and understanding of the index allows us to write higher quality code.

  1. Index Overview
  2. Clustered index
  3. Non-clustered index
  4. The only index
  5. Screening Index
  6. Non-clustered index contains column

Index Overview

       The main index exists in order to improve the speed of data retrieval, indexing and efficient design is extremely important to get a good database and application performance.

       The index is a database table values ​​in one or more columns will be specific information a structure using an index for quick access to database tables sorted, as usually we use the Xinhua Dictionary directory, if no directory Xinhua Dictionary has to find a word must have turn the last page from the first page, this is how desperate things.

  The index is out of space occupied, is a typical practice space for time, so treat the index must maintain awe, to be built on the conditions of regular screening, query using the index data should always think, actually have to spend extra space to store the index, you can not let it go to waste.

 

Clustered index

Gathering data based on a key index in the table rows sorted and stored in the data rows. Each table can have a clustered index, because the data line itself can only be stored in a sequence.  

Is simply the order of the clustered index and store the data is consistent, clustered index leaf node is the data.

Guidelines for Creating

Clustered index key used for the better column 1. Definitions

Or 2. The only contain many unique values ​​(columns that uniquely identifies a unique property if not used to create a clustered index, SQL Server automatically adds a 4-byte)

3. often need to access data sequentially

4. often used in the table to sort the data retrieved

The column size does not exceed 900 bytes

For the case of using the clustered index

1. Use the operator (e.g. BETWEEN,>,> =, <, and <=) returns a series of values

2. Return large result sets

3. JOIN clauses; In general, use of the clause is the foreign key column

4. Sorting ORDER BY or GROUP BY / packet data (packet data is first sorted as)

Not suitable for use clustered index case

Column 1. Frequent changes, each change will lead to the index page continues to rebuild.

2. A number of columns or a combination of a large number of columns, each build sorting computationally demanding

TSQL to create a clustered index

- unique clustered index 
the CREATE  UNIQUE  the CLUSTERED  the INDEX  [ IX_TableName_Name ]  the ON  [ the dbo ] . [ The TableName ] ( [ FieldName ]  the ASC )

- not only clustered index 
the CREATE  the CLUSTERED  the INDEX  [ IX_TableName_Name ]  the ON  [ the dbo ] . [ The TableName ] ( [ FieldName ]  the ASC )

 

Non-clustered index

Non-clustered index key values and pointers table contains the index data storage location row locator. You can create multiple non-clustered index on the table or indexed view. Typically, non-clustered index is designed to improve the often used, did not establish performance clustered index queries.

It simply is not a clustered index is non-clustered index. Forehead, did not seem to explain the same. . . Non-clustered index leaf node is locators.

Guidelines for Creating

1. does not exceed 1700 bytes (900 bytes other documents say, I tested SQL Server 1700 byte)

2. Avoid adding unnecessary columns, because of increased index maintenance costs

3. contains many unique values, so as to make better use of the index query efficiency

4. The length of the column as small as possible

For using non-clustered index

1. JOIN or GROUP BY clause

2. Do not return a large result set of a query

3. often included in the search query conditions in the column

Not suitable for non-clustered index case

1. The column duplicate values ​​less

Column 2. Search conditions are not often query

3. The length of the column is too long, because of the increased index maintenance costs

Clustered index and non-clustered index difference (limited knowledge, not necessarily all, would like to add)

1. clustered index is based on the physical order, a non-clustered index logical order

2. clustered index a table can have only one non-clustered index may be more

3. The leaf nodes are clustered index data, a leaf node is a non-clustered index locator

4. The additional information can not be clustered index, non-clustered index may contain additional information

TSQL to create non-clustered index

CREATE INDEX IX_TableName_FieldName ON DataTable(FieldName DESC)

 

The only index

Unique index to ensure that the index key is not included in duplicate values so that each row of the table is unique from some way. Only when the unique characteristics of the data itself, specify a unique index makes sense.

The index key value is not guaranteed duplicate (contains a NULL value)

Guidelines for Creating

 1. Use only the case only need to ensure the uniqueness of the data

Consider using this unique index

1. The need to ensure the uniqueness of data

Not suitable for the unique index case 

1. The data need not guaranteed to be unique

TSQL create a unique index

CREATE UNIQUE INDEX IX_TableName_FieldName ON DataTable(FieldName ASC) 

 

Screening Index

 Filtered index is an optimized nonclustered index, especially for covering select data from a well-defined subset of data queries. Use filtered index filter predicates on the part of rows in the table is indexed.

Only to meet the requirements of data indexing, indexing is equivalent to a subset of a table. So as to achieve several benefits below

1. improves query performance because the index page of data is smaller than a full-table index

2. Reducing index maintenance costs, because only meet the conditions will be maintained on the index page

3. Reduce storage costs index, the reason like above

Guidelines for Creating

1. Screening conditions must be explicit value

2. Conditions In general queries often occur with filter conditions are met

3. subset of data is retrieved frequently

For the case of using a filtered index

1. Regular screening subset of the table data, for example, usually we only valid query orders, small orders invalid check, or substantially no investigation, this case for the establishment of a filtered index

2. Check only the most recent data, for example, just check the record last month, accelerate query efficiency can be achieved through periodic re-screening index maintained in the database idle time

Not suitable for screening index case

1. Often the query contains the filter values, but this has led to take full table scan (provided no other indexes cover)

2. The query is not fixed

TSQL create a unique index

CREATE INDEX IX_TableName_FieldName ON [dbo].[TableName](FieldName ASC)  where State > 1

 

Non-clustered index contains column

 Add columns to the leaf level of the non-clustered index by non-key, non-clustered index extension. By including non-key columns, you can create queries cover more non-clustered index

By contained in a column while maintaining the index page, to the time when the query data are included in the index data, because all data in the index to find the page, you do not need access to the table data pages, thus reducing I / O operations, this is usually referred to as "coverage query"

Guidelines for Creating

1. The column must define at least one key

2. Define the non-key columns in the INCLUDE clause of the CREATE INDEX statement

3. only non-key columns defined non-clustered index on the table or indexed view

4. allows all types of data other than text, ntext and image

The precise or imprecise deterministic calculation column may be a column comprising

6. You can not specify the column names in the INCLUDE list and the list of key columns

7. INCLUDE column names in the list can not be repeated

8. The index key columns (not including the non-key) must comply with the limitations of existing index size

9. The total size of all non-key columns only by the INCLUDE clause column specified size limit; e.g., varchar (max) column is limited to 2 GB

Suitable for non-clustered index contains column

1. Screening column is a column index key && queries are included in column

Not suitable for non-clustered index contains column

2. Screening column is not a column index key || queries are not included in the column

TSQL to create a filtered index

CREATE  INDEX IX_TableName_FieldName ON DataTable(Field1 ASC) INCLUDE(Field2)

 

Guess you like

Origin www.cnblogs.com/WilsonPan/p/12625364.html