30. The database of index

index

1. What is the index

It is the index of a particular data structure, which stores the location information of the key data and details of the correspondence relationship.

2. Why do you need an index

Because when the amount of data is very large, one query data is very slow, we can use the index to speed up queries

3. Impact of the index

  1. Not to say that the index has been able to speed up, there is no need to look at the query correctly use the index
  2. Indexes also need to take extra data space
  3. After adding an index lead to changes in amendments to delete slow

When more query operations, when we use less write index, the index principle is essentially to filter out the final results you want by constantly want to narrow the scope of access to data, as far as possible to reduce the search range

4. disk IO

Disk reads data, a disk IO time is approximately equal to about 5 + 4.17 = 9ms, disk IO is a very expensive operation, the computer operating system to do some optimization, when the IO once, not just the current disk address of the data, but the adjacent data are also read into the memory buffer, because the local pre-reading principle tells us that when a computer to access data addresses, adjacent to the data will be accessed soon.

The index data structure

b + tree

In the b + tree leaf node is stored in the real data, the more the number of leaves, the higher the level of the tree, causes an increase in the number of IO To avoid this problem, store more data as possible in a leaf node, should be small amount of data as the index field

When the data item is a compound b + tree data structure, the number b + is from left to right in order to establish the search tree, the leftmost index having matching characteristics.

6. clustered index secondary indexes

Database B + tree index can be divided into clustered index (clustered index) and secondary indexes (secondary 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

In addition the table indexes are clustered index other secondary index, the index contains only the secondary current value of the primary key index field and

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

Back to the table query: refers to the required data can not be found in the current index structure, the need to go to the clustered index query by the primary key index values ​​found, slower than the clustered index

7. Conclusions

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

8. Create an index of grammar

create index index name on table name (field name)

Joint index

create index index name on table name (field names, field names)

​ create index union_index on usr(id,email,name,gender);

Delete the index:

drop index index name on table name;

Guess you like

Origin www.cnblogs.com/yellowcloud/p/11209556.html