database-based index

1. Definition: An index is a structure (B-tree) that sorts the values ​​of one or more columns in a data table or view, which can speed up retrieval.

2. Function: Speed ​​up search and sorting (reason: Balanced tree structure is used to avoid full table scan)

3. Disadvantages: ① Affects the speed of additions, deletions and modifications (re-organization of the index is required) ② Occupies more disk space

4. Syntax:
4.1 Create index:
CREATE [UNIQUE] [CLUSTERED| NONCLUSTERED ]
INDEX index_name ON { table | view } ( column [ ASC | DESC ] [ ,...n ] )
[with[PAD_INDEX][[,]FILLFACTOR =fillfactor] The parameters of the CREATE INDEX command to create an index are described as follows: UNIQUE: Used to specify the creation of a unique index for a table or view, that is, two rows with the same index value are not allowed. CLUSTERED: Used to specify that the created index is a clustered index. NONCLUSTERED: Used to specify that the created index is a non-clustered index. index_name: Used to specify the name of the created index. table: Used to specify the name of the table where the index is created. view: Used to specify the name of the view to create the index. ASC|DESC: Used to specify the ascending or descending sorting direction of a specific index column.









Column: Used to specify the column to be indexed.

4.2 Delete index:
DROP INDEX table_name.index_name[,table_name.index_name]

4.3 Display index information:
Use the system stored procedure: sp_helpindex to view the index information of the specified table. Exec sp_helpindex table_name;

5. Classification:
clustered index and non-clustered index (similar to pinyin and radical search in Chinese dictionary)
unique index and non-unique index
single-column index and multi-column index

6. Index data structure : b+tree (b tree, balanced tree, not binary tree)
A table without a primary key, its data is placed on the disk storage in disorder, and the rows are arranged neatly, as I know the "table" very close. If the primary key is added to the table, the storage structure of the table on the disk is changed from a neatly arranged structure to a tree-like structure, which is the "balanced tree" structure mentioned above. In other words, the entire table becomes an index. That's right, again, the entire table becomes an index, the so-called "clustered index". This is why a table can only have one primary key, and a table can only have one "clustered index", because the function of the primary key is to convert the data format of the "table" into the format of "index (balanced tree)". As shown below:
After talking about the clustered index, let's talk about the non-clustered index, which is the conventional index that we often mention and use.
A non-clustered index, like a clustered index, also uses a balanced tree as the index data structure. The value of each node in the index tree structure comes from the index field in the table. If an index is added to the name field of the user table, the index is composed of the value in the name field. When the data changes, the DBMS needs to maintain the index structure all the time. correctness. If you add indexes to multiple fields in the table, there will be multiple independent index structures, and each index (non-clustered index) is not related to each other. As shown below

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325763155&siteId=291194637