BAT essential interview, MySQL quality analysis topics

Disclaimer: This article is a blogger original article, follow the CC 4.0 BY-SA copyright agreement, reproduced, please attach the original source link and this statement.
This link: https://blog.csdn.net/qq_39662660/article/details/100547712

BAT essential interview, MySQL quality analysis topics

 

 

1. What is the index?

The index is a data structure that can help us to find data quickly.

2. The index is what kind of data structure?

Data structure and specific storage engine indexes related to use more of the index in MySQL there Hash index, B + tree indexes, and the default storage engine InnoDB index we often use to achieve: B + tree index.

3. Hash B + tree index and what is the difference or all the pros and cons of it?

Hash index must first know and B + tree index underlying implementation principles:

hash index is the underlying hash table, when to find, call a hash function can get to the appropriate key, followed by the query back to the table to get the actual data .B + tree underlying implementation is more than balanced search trees. For every query starting from the root, find leaf node parties may obtain the search key, and then back to the table query data according to the query whether it is necessary. the Java architecture community

We can see that they have the following differences:

  • hash index equivalent queries faster (in general), but it can not be the scope of the query.

Since then indexed through hash function hash index, the index of the sequence with the original order can not be consistent, can not support range queries. And all the nodes B + tree's are to follow (left node is less than the parent node, right node is greater than the parent, and more tree is similar), native support range.

  • hash indexes do not support the use of indexes sort principle above.
  • hash indexes do not support fuzzy queries and multi-column index of the most left-prefix match principle but also because of the unpredictable hash function. AAAA and AAAAB index of no relevance.
  • hash index can not be avoided at all times to query the data back to the table, while the B + tree queries can be done when certain conditions are met (clustered indexes, covering indexes, etc.) only by the index.
  • Although faster hash index on the equivalence queries, but unstable. unpredictable performance, when there are a large number of duplicate keys when hash collision occurs, then efficiency may be poor. The query efficiency B + tree is relatively stable, for all queries are from the root node to the leaf node, and low height of the tree.

Therefore, in most cases, to directly select B + tree index can obtain stable and better query speed without the need to use hash indexes.

4. The above mentioned B + tree does not need to query the data back to the table in time to meet clustered indexes and covering indexes, what is the clustered index?

The index B + tree leaf node may store the current key value, may also be stored in the current key value and the entire row of data, which is clustered index and non-clustered index. In InnoDB, only the primary key index is clustered index, if no primary key, then choose a unique key to establish a clustered index. If there is no unique key, then implicitly generated to build a clustered index key.

When a query using a clustered index, the corresponding leaf node, you can get the entire row of data, so do not be back to the table this time.

5. Non-clustered index table query will return it?

Not necessarily, this involves a query whether all the required fields hit the index, if the index all hit, then you do not have to be back to the table query.

Here is a simple example, suppose we have created an index on the age of the employee table, then when the select age from employee where age Queries <20, in the leaf nodes of the index, already contains the information age, not again back to the table query.

6. When indexed, what are the factors to consider it?

When the general index to take into account the frequency of use fields, often query field as conditions are suitable. If you need to establish a joint index, it is also the order of the joint index needs to be considered. In addition also consider other aspects, such as to prevent excessive All cause too much pressure on the table. these are the actual table structure and query-related. the Java architecture community

7. What index is the joint? Why pay attention to the order of the joint index?

MySQL can use multiple fields while building an index, called the joint index. In the joint index, the index if you want to hit, field need to follow the order of indexing one by one, otherwise it can not hit the index.

Specific reasons are:

MySQL use to index index orderly Suppose now that the establishment of a "name, age, school" joint index, the index of the sort: first name in accordance with the order, if the same name, then sort according to age, if the value of age are equal then sorted according to the school.

When queried, only this time the index name in accordance with strict and orderly, it must first be equivalent query using the name field, and then for a match to the column, according to their age fields strict and orderly, then you can use fields with age index lookup ,,, do so. Therefore, when establishing joint index should be noted that the order of the index column, under normal circumstances, the query field frequently demand or high selectivity columns on the front. Also a special case can query based or separate adjustment table structure.

8. Create the index has not been used to? Or how can know the reason for this statement is running very slow?

MySQL provides explain command to view the execution plan statement, MySQL before executing a statement, that statement go over the query optimizer, will get after the analysis of the sentence, that is, the execution plan, which contains a lot of information. and index information by which to analyze whether the relevant index hit, for example possilbe_key, key, key_len and other fields, respectively, illustrate this statement may use the index, the index actually used and the length of the index used.

9. So what happens in the index is created but not used when the query it for this column?

  • Use is not equal to the query,
  • Column involved in math or function
  • When the string is a wildcard like left Like '% aaa'.
  • When a full table scan analysis mysql does not use index than when using index fast.
  • When used in conjunction index, a condition for the front range queries, even the most left-prefix in line with the principles behind, you can not use the index.

The above case, MySQL can not use indexes.

Fans Welfare

BAT essential interview, MySQL quality analysis topics

 

BAT essential interview, MySQL quality analysis topics

Plus Java architecture community : receive data, which will share some video recordings senior architect: There are Spring, MyBatis, Netty source code analysis, the concept MySQL, high concurrency, high performance, distributed micro-service architecture, JVM performance optimization of these became necessary information architect

Guess you like

Origin blog.csdn.net/qq_39662660/article/details/100547712