Index 11-4

1.1 What is indexing

It can be understood as a search guide.

Index is a special data structure, which stores the location information of the key data and details of the correspondence relationship.

1.2 Why do we need an index

Speed ​​up queries

The impact of index 1.3

1.3.1 What are the implications

  1. Not to say that the index has been able to speed up, depends on your query has no right to use the index
  2. Indexes also need to take extra data space
  3. After adding the index will lead to slower write operations

1.3.2 how to speed up queries

The index is essentially the principle is to reduce the scope of the search as much as possible.

We want to speed up query, you must reduce the number of IO operations .

Data structure of the index is b + tree, the b + tree leaf node is the real data storage, the more leaves, the higher the level of the tree, resulting in increased number of IO.

To avoid this problem, it should be possible to store more data in the leaf nodes in, so it should be a small amount of data as the index field .

1.4 What is the leftmost matching principle

When the data item is a compound b + tree data structure, such as (name, age, sex) when (multi-column joint index), b + tree search tree will be established in accordance with the order from the left, such as when (Zhang, 20, F) so that the time to retrieve the data, b + tree name priority comparison determines the next search direction, if the same name and age Sex comparison in turn, finally obtained data retrieved; but (20, F) such data is not to name the time, b + tree node does not know what the next step to the investigation, because the time to establish the search tree name is the first comparative factor, you must first name according to the search query in order to know where to go next . For example, when (Zhang, F) ​​to retrieve such data, b + tree name can be used to specify the search direction, but the lack of age next field, so only the name is equal to the seating of the data is found, then the matching sex F of the data, this is a very important property, namely the left-most matching characteristics of the index.

1.5 What is a clustered index

Clustered index contains the values ​​of all fields, if the proposed development of a primary key, the primary key is the clustered index, if not then look for a non-empty and the only field as the clustered index, if not find, and automatically generate a field as the clustered index

Clustered index stores all data

1.6 What is the secondary index

In addition to the clustered index than it is called secondary indexes

Secondary index contains only the current value of the primary key index field and

1.7 What is covered inquiry

It refers to the need to find all the data in the current index structure, if you use a clustered index to query then it must cover the query, is the fastest

1.8 What is a back table query

Refers to the required data can not be found in the current index structure, the need to go through the clustered index query id, slow and clustered index

1.9 Summary conclusion

1. The minimum footprint fields as an index

2. Do not store too much data in a row, such as novels, video, if too many fields can be divided table

3. Try to use cover the query

4. If the field is case of low (high repetition), indexing does not make sense, on the other hand should be high distinction as an index field

5. fuzzy matching, try not EDITORIAL percent sign

6. Do not equate to the left to do arithmetic

For example: SELECT COUNT ( ) from usr WHERE ID . 3 =. 6; will traverse all records

7.and statement will automatically find the field with a compact edition of the first, it should contain at least we have an index in a field and statement

8.or statements should be avoided, if you use the index to ensure that all the fields have accelerated

9. The joint index, the order should be placed to the left of the highest distinction, the lowest on the right place,

Query must ensure that the leftmost index appear in the statement

Also note: If the amount of data to be queried very large index can not be accelerated

Summary: Instead of adding the index will be able to speed, consider adding the index is reasonable, sql statement is to use index

1.10 Syntax

# 创建索引的语法:
create index 索引的名字 on 表名称(字段); 

# 联合索引
create index 索引的名字 on 表名(字段名,字段名)


# 删除索引
drop index 索引名称 on 表名称;

Guess you like

Origin www.cnblogs.com/chenych/p/11227977.html