Sqlserver2008 [index]

There are two SQL indexes : clustered index, non-clustered index

  Objective: To improve the performance of sqlserver system, speed up the response time data query speed and reduce system

  Precautions: a table can have only one clustered index, but can have multiple non-clustered indexes

Index storage mechanisms :

    Clustered index and non-clustered indexes fundamental difference is whether the order table records the order and consistent with the index, in fact, very simple to understand, or cite the dictionary example: If the query in accordance with the alphabet, all from az then, and is a continuity, a is behind the back is b, b is C, a clustered index is such that he is, and the physical arrangement order of the table is the same, for example, a clustered index id is then definitely behind a back definitely 2,2 3, so that such a search order is the clustered index. 

    And in accordance with the non-clustered index on the same query is radical, it may be time concubine in accordance with the query, based on the radical side 'bow' word, the index out of two Chinese characters, and Zhang Hong, but this is actually a two page 100, one in 1000 page (here is just an example), their index order and the order of the database table is not the same, this is the kind of non-clustered index.

 

Establish the principle of indexing:

1) data defining the primary key columns must be indexed.

2) define the columns of the foreign key data must be indexed.

3) For data queries often the best index of the column.

4) the need for frequent or rapid data queries in the column within the specified range;

5) data columns are often used in the WHERE clause.

6) group by, behind the distinct fields often appear in keyword order by,, indexing. If you create a composite index, the index of the field order and order of fields to be behind these keywords consistent, otherwise the index will not be used.

7) For those columns in the query rarely involved, duplicate values ​​are more columns not indexed.

8) For defined as text, image and bit data types of columns not indexed.

9) For the avoidance frequently accessed column index

9) limit the number of indexes on tables. For a table of the presence of a large number of update operations, the number of construction index is generally not more than three, up to no more than five. Although the index to improve the access speed, but too much will affect the index update data.

10) of the composite index, index according to the frequency of the field appearing in the query condition. In the composite index, records sorted first according to the first field. For the same values ​​recorded in the first field, the system then sorted according to the value of the second field, and so on. Thus only the first field of the composite index appears in the query, the index can be used before, and therefore the application of high-frequency field, placed in front of a composite index, the system will make the maximum possible use of this index, the index play role.

 How to create an index:

  grammar

  CREATE [UNIQUE][CLUSTERED | NONCLUSTERED] INDEX index_name

  ON {table_name | view_name} [WITH [index_property [,....n]]

  

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 the available 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.

Create a unique clustered index:

Copy the code
- create a unique clustered index
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, - 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 - enable statistics indicate automatic update feature
)

Create a filtered index:

Copy the code
- 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 on
the Create index Index_StuNo_SName
ON Student (S_StuNo)
the WHERE S_StuNo> = 001 and S_StuNo <= 020
with (DROP_EXISTING = ON)

Create a filtered index:

Copy the code
- 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 on
the Create index Index_StuNo_SName
ON Student (S_StuNo)
the WHERE S_StuNo> = 001 and S_StuNo <= 020
with (DROP_EXISTING = ON)

 

Guess you like

Origin www.cnblogs.com/yhm9/p/11095156.html