MySQL database index FAQ

  I read a lot of relevant aspects of the database interview questions, but most of the answers are not very accurate, and therefore decided to conduct a summary in your own blog.

Q1: What are the database indexes? What advantages and disadvantages?

1.B tree index: the index most database used (innoDB uses b + trees). It can speed access to data, especially the Look of data very quickly. The disadvantage is only beginning to find the left-most column of the index, the index can not skip a column, if the query has a column uses a range query, the right of all columns can not use the Index Tuning search.

2. Hash Index: Based on the hash table. In MySQL, the only explicit support Memory hash engine search. Hash lookup speed is very fast, but only contains a hash value hash index and row pointers, the field value is not stored, it is not used to avoid the value of the index reading line can not be sorted. Since the hash index is used by the entire contents of the indexed column to calculate a hash value, it is part of all columns do not match to find support. Hash equivalence comparison only support, not support any range queries. Once the hash collision many words, the maintenance costs are very high. innoDB support "adaptive hash index" (adaptive hash index).

3. The full-text indexing: full-text index is a special type of index, it looks for keywords in the text, rather than the value of the comparison index. Initially available only on MyISAM, after 5.6.24 innoDB also supports full-text indexing. To use the full-text index queries Match .... against, to create a full-text search simultaneously on the same column and there will be no conflict based on B-Tree index values.

4. The spatial index (R-tree index), MyISAM supports R-tree index, the benefits without a prefix query, the index data will come from all latitudes, can be used as a memory geographic data; MySQL disadvantage is that the use of correlation functions such as GIS MBRCONTAINS (), etc. to maintain data, but because MySQL in GIS is not perfect, so most people will not use this feature.


Q2: Why not practical binary search tree or a red-black tree as a database index.

Binary Tree in dealing with huge amounts of data, the tree height is too high, though the index is high efficiency, reach logN, but it will be a lot of disk io, more harm than good. And delete or insert data may lead to structural changes become linked list data, the need to improve the balance algorithms. The red-black tree, insertion and deletion of elements when it will be discolored and frequent rotation (L, D), is a waste of time. However, when small amount of data when the can into a red-black tree, red-black tree at this time is lower than the time complexity of the b-tree. Therefore, the sum considerations, the final choice of the database as a b-tree index.


Q3: B tree and B + tree scenarios:

1.B tree commonly used in the file system, and a small number of database indexes, such as mongoDB.

2.B + tree index is mainly used for mysql database.


 

Q4: B + tree B tree Comparative advantage of

Each node B-tree index is stored in addition to sub-node, but also to store data domain, so a single child node node index points is not a lot higher height of the tree, the more the number of disk io. Lower B + height of the tree, and all data are stored in the leaf nodes, the leaf nodes are in the same level, thus stabilizing the query performance, easy to find the range.

Guess you like

Origin www.cnblogs.com/CNty/p/10960803.html