SQL database development -TSQL- Chapter IX index 2

9.4 clustered index and non-clustered index

9.4.1 clustered index concept

The clustered index key value data line sort and store the data rows in a table or view. Index definition contains a clustered index columns. Each table can have a clustered index, because the data line itself can only be stored in a sequence.

Only when the index table comprising aggregated data rows in the table was stored in sorted order. If the table has a clustered index, the table is called aggregation table. If no clustered index table, it is stored in the random data rows called a stack structure of.

1 clustered index to their own understanding

Ordering index is an aggregate of the actual data to disk re-organized in one or more columns specified value. Like Chinese dictionary we use is a clustered index, for example, to check the "sheets", we naturally turn to the back of the dictionary of hundred pages. Then alphabetically by now to find out. Here use Microsoft's balanced binary tree algorithm that first turn over about one-half position, if looking for the page number is smaller than the page number of the page, put forward at a quarter turn to the book, otherwise, put the book back where three-quarters turn, and so on, the page continued into smaller parts, until the correct page.

As the clustered index is to sort the data, there can be multiple rows of law, so a table can only build a clustered index. Science statistics to create such an index would need at least equivalent to 120% of additional space in the table, the table used to store a copy of the index and middle pages, but his performance is almost always faster than other indexes.

Since the clustered index, the data is physically in sequence on the data page, row together also duplicate values, and thus includes a range checking (bentween, <,> <=,> =) or group by or order by when the query, once found a key in the first row, will be back together, you do not have to search further, avoid scanning a wide range of friends, can greatly improve query speed.

 

 

9.4.2 non-clustered index

Having a non-clustered index row independent of the data structure. Non-clustered index comprises a non-clustered index key, and each key has a pointer to a data entry lines of the key value.

Directional data row from a non-clustered index row pointer called a row index locator. Locator structure depending on the data row is stored in the page heap or aggregate table. For a heap, a row locator pointing to row pointer. For aggregate table, a row locator clustered index key.

1 non-clustered indexes to their own understanding - Pointer

sqlserver default index is non-clustered index established under the circumstances, he does not reorganize data in the table, but store the index of each row and column values ​​using a pointer to the data page is located. He's like Chinese dictionary based on 'radical' to find the word're looking for, even if the data is not sorted, but he has a directory like a directory, check the efficiency of access to data also has room for improvement, without the need for full table scan.

A table may have a plurality of non-clustered index, each non-clustered index depending on the index of the column to provide a different sort order.

9.4.3 clustered index and non-clustered index differences and relations

Clustered index and non-clustered index

Action Description

Use clustered index 

 Using non-clustered index

 Foreign key column

 should

 should

 Primary key column

 should

 should

 Columns that are frequently ordering packets (order by)

 should

 should

 Return data within a certain range

 should

 Should not be

 A small number of different values

 should

 Should not be

 A large number of different values

 Should not be

 should

 Frequently updated columns

Should not be 

 should

 Frequently modified index column

 Should not be

 should

9.4.4 gather principles and methods of indexing

1 clustered index design principles

1.1 Query Notes has the following characteristics queries using clustered index

Using operators (e.g. BETWEEN,>,> =, <, and <=) returns a list of values.

Use ORDER BY or GROUP BY clause. Index of the column specified in the ORDER BY or GROUP BY clause, you can make the database engine does not have to sort the data, since these rows have been sorted. This can improve query performance.

2 Lie Notes

Column, the better use of time clustered index key definitions 2.1. Consider a column having one or more of the following attributes:

Single or contains many unique values

It is accessed sequentially

Defined as IDENTITY.

Often used in the table to sort the retrieved data.

Frequent changes column

2 Create a clustered index

The preferred way to create indexes on large tables is to create a clustered index

1 typical default implementation

Clustered index to achieve the following manner:

When you create a PRIMARY KEY constraint, clustered index if the table does not exist and does not specify a unique non-clustered index, it will automatically create a unique clustered index on one or more columns. Primary key column not allow nulls.

When you create a UNIQUE constraint, by default it creates a unique non-clustered index to enforce a UNIQUE constraint. If the clustered index of the table does not exist, you can specify a unique clustered index.

3 Limitations and Restrictions

After 1 Create a clustered index structure, the old (source) and the new structure of the respective files and file groups (target) structures require disk space. Upon completion of the transaction commits, it will release the old structure. Sort also require additional temporary disk space. 

2 If the clustered index is created on the stack has a plurality of existing non-clustered index, you must regenerate all non-clustered index key value so that they not contain aggregate row identifier (RID). Similarly, if you delete having a plurality of non-clustered index clustered index table, during operation, DROP, non-clustered index will be regenerated. For large tables, this can take a long time.

The preferred method of three pairs of large tables to create an index is to first create a clustered index, and then create any non-clustered indexes. When you create an index on an existing table, consider ONLINE option set to ON. If set to ON, it will not hold long-term table locks.

4 clustered index index key can not contain columns having varcharr ROW_OVERFLOW_DATA existing data allocation unit. If varchar column clustered index is created, and there is an existing data IN_ROW_DATA allocation unit, the data is pushed to perform a subsequent column inserted or updated in the outer row will fail.

How to create a clustered index 4

1 SQL Server Management Studio

2 Transact-SQL

CREATE UNIQUE CLUSTERED INDEX [ClusteredIndex-20190125-223354] ON [dbo].[A01_SCSDBInfo]

(

      [ServerIP] ASC

)

The only gather UNIQUE CLUSTERED

 

Published 37 original articles · won praise 0 · Views 2418

Guess you like

Origin blog.csdn.net/syjhct/article/details/86669650