SQL Server creates an index implementation code

Source: Internet
Author: User

is an internal method of SQL Server orchestration data. It provides a way for SQL Server to orchestrate query data what is an index
Take the Catalogue page (index) of the Chinese dictionary for example: Just as Chinese characters are stored on pages, the data records in SQL Server are stored as pages, and the capacity of each page is typically 4K. In order to speed up the search, Chinese word (word) code generally have by pinyin, strokes, radicals and other categories of the directory (index), we can choose by pinyin or strokes to find the way to quickly find the desired word (word).
Similarly, SQL Server allows users to create indexes in a table, specifying that they are pre-ordered by a column, which greatly improves query speed.
? Data in SQL Server is also stored by page (4KB)
? Index: is an internal method of SQL Server orchestration data. It provides a way for SQL Server to orchestrate query data.
? Index page: The data page in the database where the index is stored; The index page is similar to a catalog page sorted by phonetic or stroke in the Chinese word (word) dictionary.
? The role of indexes: By using indexes, the retrieval speed of the database can be greatly improved and the database performance improved.

Index Type
? Unique index: Unique index does not allow two rows to have the same index value
? Primary KEY index: Defining a primary key for a table will automatically create a primary key index, which is a special type of unique index. The primary key index requires that each value in the primary key is unique and cannot be empty
? Clustered index (Clustered): The physical order of the rows in the table is the same as the logical (indexed) Order of the key values, and each table can have only one
? Nonclustered indexes (non-clustered): Nonclustered indexes Specify the logical order of tables. The data is stored in one place, the index is stored in a different location, and the index contains a pointer to the location of the data store. can have multiple, less than 249

Index type: Again, in the Chinese dictionary, I hope you can understand the two concepts of clustered and nonclustered indexes.

Unique index:
A unique index does not allow two rows to have the same index value.
If duplicate key values exist in the existing data, most databases do not allow the newly created unique index to be saved with the table. The database also rejects this data when the new data duplicates the key values in the table. For example, if you create a unique index on the learner ID number (STUID) column in the Stuinfo table, the ID number of all learners cannot be duplicated.
Tip: A unique constraint is created and a unique index is created automatically. Although a unique index can help you find information, it is recommended to use a PRIMARY KEY constraint or a unique constraint for best performance.

PRIMARY KEY index:
Defining a primary key for a table in a database diagram automatically creates a primary key index, which is a special type of unique index. The primary key index requires that each value in the primary key be unique. When a primary key index is used in a query, it also allows for fast access to the data.

Clustered Indexes (clustered index)
In a clustered index, the physical order of the rows in the table is the same as the logical (indexed) Order of the key values. A table can contain only one clustered index. For example, the Chinese word (word) code defaults to alphabetical order of each page number in the dictionary. Phonetic Alphabet A,b,c,d......x,y,z is the logical order of the index, and the page number is ... is the physical order. The dictionary, which is sorted by phonetic Alphabet by default, has the same index order and logical order. That is, the phonetic order of the word (word) corresponding to the page number is also larger. If the phonetic "ha" corresponding to the word (Word) page number is more than the phonetic "ba" corresponding to the word (Word) page number.

Nonclustered indexes (non-clustered)
If it is not a clustered index, the physical order of the rows in the table does not match the logical order of the key values. Clustered indexes have faster data access speeds than nonclustered indexes (nonclustered index). For example, an index sorted by strokes is a nonclustered index, and the word "1" (word) corresponds to a page number that corresponds to the number of words (the word) that the "3" is drawn on (back).
Tip: In SQL Server, a table can only create 1 clustered indexes, multiple nonclustered indexes. Set a column's primary key, which defaults to a clustered index

How to create an index
Syntax for creating indexes using T-SQL statements:
CREATE [UNIQUE] [clustered| Nonclustered]INDEXon[withfillfactor=x ]

Q Unique represents a unique index, optional
Q CLUSTERED, nonclustered represents a clustered or nonclustered index, optional
Q FillFactor represents a fill factor, specifying a value between 0 and 100 that indicates the percentage of space that the index page fills up

Create an index on the Writtenexam column of the Stumarks table:

 UseStudbGO IF EXISTS(SELECTName fromsysindexesWHEREName= 'Ix_writtenexam') DROP INDEXStumarks.ix_writtenexam/*--Written test column create nonclustered index: Fill factor is 30%--*/ CREATE nonclustered INDEXIx_writtenexam onStumarks (Writtenexam) with FILLFACTOR=  - GO /*-----Specifies the query by index ix_writtenexam----*/ SELECT *  fromStumarks (INDEX=Ix_writtenexam)WHEREWrittenexambetween  -  and  - 

Although we can specify which index SQL Server will query for data, we do not normally need to specify it manually. SQL Server automatically optimizes the query based on the index that we create.   The advantages and disadvantages of the

index  
? advantages  
– Speed up access  
– strengthens the uniqueness of the row  
? disadvantage  
– indexed tables require more storage space in the database  
– commands that manipulate data require longer processing time because they need to update the index  

Guidelines for creating indexes  
? Select the indexed column according to the following criteria.  
– This column is used for frequent searches of  
– This column is used to sort the data  
? Do not use the following column to create an index:  
– Only a few different values are included in the column.  
– The table contains only a few rows. Creating an index on a small table may not be a good deal because SQL Server spends more time searching for data in the index than it does in a row-by-line search in the table

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.