Detailed explanation and principle analysis of MySQL index

Detailed use of MySQL index, principle analysis

[Advance notice]
article by author: Zhang Yaofeng in conjunction with their own experience in the production of finishing, forming easy to understand article
writing is not easy, reproduced please specify, thank you!
Big Data Code Case Address: https://github.com/Mydreamandreality / sparkResearch


In the innodb engine, there are a total of four index types, two index methods, I mainly talk about the choice of index in production, the choice of data structure

Index hit rate, SQL execution plan can view my previous article

Index type:

  • 1. Normal index
  • 2. Unique index
  • 3. Full Text Index
  • 4. SPATIAL spatial index

Index method:

  • 1. The btree index method
  • 2. Hash index method

Detailed index types:

  • 1. First of all, the index type Normal is the most commonly used index type, and it is also the most basic index type. It does not have any restrictions. You can set Normal for a field or do joint indexing. When doing single-condition query, multi-condition query, or other left join operations, the MySQL optimizer will automatically use this index type to optimize the query speed, then if we want to know whether this index is effective or not, we can use the explaincommand Let's check the SQL execution plan. For a detailed explanation of the SQL execution plan, please see my previous blog.
  • 2. Unique, as the name implies, is a unique index. The unique index is to add a unique constraint on the basis of the ordinary index. I tested it under a million-level data, and there is not much performance difference compared with Normal. The application scenario of this index is also quite good. Many, for example, your data has a field that needs to be indexed, and the value of this field must be unique, then using a unique index is a better choice
  • 3. Full Text full-text indexing makes sense. Generally speaking, we all need Elasticsearch. Full-text indexing in MySQL is not used much, but the basic unit of full-text indexing is definitely a word. MySQL's tokenizer is not Know if it is as powerful as es. In short, the application scenario of this index is the large text stored in a certain field, you can use the Full Text index
  • 4. SPATIAL spatial index, to be honest, this has never been used, because this index can not be used in the innodb engine, it needs to be used on the spatial data type field in the MYISAM engine. There are 4 spatial data types in MYSQL, which are GEOMETRY, POINT, LINESTRING, POLYGON, MYSQL use SPATIAL keywords to expand, so that it can be used to create a regular index type of syntax to create a spatial index, to create a spatial index column, it must be declared as NOT NULL

Detailed index method:

Index method This MySQL supports two

  • 1. BTREE is a B + tree, which should be the most used
  • 2. Hash is the data structure of K and V. This kind of range search and node search does not support very well.

What is the index?

Have you ever thought about what the index is? Why the query efficiency becomes so high after adding the index reasonably

In fact, to put it bluntly, the index is a data structure, and its role is to optimize our query, just like the index implemented by the B + tree in MySQL. The B + tree is a data structure. When you do not add an index, query a data. You need to scan the entire table, and then do a full table scan before matching the data. Is this a big amount of data? The index is equivalent to the sequence table of the dictionary. If you want to check a certain data, you can find k directly in the sequence table. The efficiency improvement is very high.

So why do we recommend using the B + tree data structure, hash index and B tree, is it not possible to completely balance the binary tree?

First of all, let ’s talk about hash indexes. This kind of index search speed is fast, but it is uncomfortable because it does not support range search, such as greater than or equal to this, you think, how does kv support this kind of query, so it is rarely used in general cases Hash index

Then b-tree, in fact, just look at the name. There is a gross difference between b-tree and b + tree, not just multiple +, in fact, b + tree non-leaf nodes are redundant into leaf nodes, and there are pointers between b + tree leaf nodes. Finding what is greater than or less than will be particularly fast, is a solution of space for time

Balanced binary tree supports range search, why not use it, because b + number (b number can also be) a node can store multiple elements, what causes this? Think about it abstractly, we have two trees. Since a node of b + tree stores multiple elements, is the tree longer than the binary tree, so the element search should be faster, reducing certain io operations

. . . Wait, wait, the index can say too many points, just know a little, just go deeper when you are free

Index usage note

  • 1. The index will have obvious effect when the amount of data is large. If the amount of data in the system is not large, there is no need to generate an index, wasting disk space
  • 2. Although the index will increase the query speed, it will reduce the update speed
  • 3. I have heard some dangerous ideas. . I don't know if the index is effective, anyway, I added indexes to all the fields. . . This is too scary, indexing will take up disk space in the index file, you can build the index normally, it ’s okay, you ca n’t overuse the index
  • 4. Choose the appropriate index according to your business scenario
Published 59 original articles · Like 364 · Visitors 100,000+

Guess you like

Origin blog.csdn.net/youbitch1/article/details/105431071