In layman's language index

    In the following table T, if we execute select * from T where k between 3 and 5, you need to perform a search operation several times tree, how much will the scan line?

mysql> create table T ( id int primary key, k int not null default 0, name varchar(16) default '', index (k)) engine=InnoDB; mysql>insert into T values(100,1,'aa'),(200,2,'bb'),(300,3,'cc'),(500,5,'ee'),(600,6,'ff'),(700,7,'gg');

The following figure shows the organizational structure of the index InnoDB

Now, we take a look at the flow of execution of this SQL query statement:

  • K = 3 to find the index recorded in the index tree k acquires ID = 300;
  • Then found primary key index tree record corresponding to ID = 300 R3;
  • K in the index tree to the next value k = 5, to obtain ID = 500;
  • Back to the primary key index tree corresponding to ID = 500 found R4;
  • Remove the index tree in a k value k = 6, the condition is not satisfied, the loop ends.

In this process, the primary key index back to the process of the search tree, we call back to the table. You can see, the query process read three records k index tree, back to the table twice, in this example, since the desired query result data only on the primary key index, so had to return to the table. So, is there likely to be optimized by the index back to the table to avoid the process of it?

Covering index

  If the statement is executed select ID from T where k between 3 and 5, then only need ID value, and the value of the index ID is already in the tree k, thus providing immediate results, you do not need back to the table. In other words, this query, the index k has covered the needs of our inquiry, we called a covering index.

  Since the covering index can reduce the number of searches of the tree, significantly improve query performance, so use a covering index is a common performance optimization tools . Note that, in the interior of the engine cover index using the actual read three records in the index k, but for a MySQL server layer, the two records it took to find the engine, so that the number of scanning lines is MySQL 2.

A discussion of the following issues: public information on a table, whether it is necessary to set up a joint index ID number and name?

CREATE TABLE `tuser` (
  `id` int(www.moyouylzcdl.cn) NOT NULL, `id_card` varchar(32) DEFAULT NULL, `name` varchar(32) DEFAULT NULL, `age` int(www.yuanyyleezc.cn) DEFAULT www.uedylezc.cn NULL, `ismale` tinyint(1) DEFAULT NULL, PRIMARY KEY (`id`), KEY `id_card` (`id_card`), KEY `name_age` (`name`,www.xinyiylzc.cn`age`) ) ENGINE=InnoDB

We know that the ID number uniquely identifies the public, that is to say, if there is public demand based on the query ID number, we just need to be indexed on the ID number is enough, and then create a (ID number, name) of the joint index, is not it a waste of space?

  If there is now a high-frequency needs to check his name according to the ID number, the full index-linking it makes sense. It can be used to cover this frequency index on request, it is no longer necessary to check the entire table rows back, reducing the statements are executed.

  Of course, maintaining an index field is always a price. In establishing a redundant indexes to support a covering index will need to consider the trade-offs.

The most left-prefix principles

This B + tree index structure index can be used to "leftmost prefix", to locate the record. To visually illustrate this concept, we use (name, age) to analyze the joint index, the index schematic diagram below shows (name, age) of:

It can be seen, the index entries are sorted in order of appearance in accordance with the index fields defined inside. When your needs are found in all logical name is "Joe Smith" who can quickly navigate to ID = 4, then iterate backwards to get all the results you want. If you want to check all the names of the first word is "Zhang" people, the condition of your SQL statement is "where name like 'Zhang%' ', then you can spend the index, find the first Matched record is ID3, then iterate backwards until the conditions are not satisfied.

It can be seen not only all the definitions of the index, as long as the most left-prefix, you can use the index to speed up retrieval. The most left-prefix can be a joint index of the most left-N fields, it can also be the most left-string index of M characters.

In establishing joint index, how to arrange the order of index fields

Here we assess the criteria are: Index of multiplexing capability. Because it can support the most left-prefix, so when've got (a, b) the joint index, generally do not need a separate index on a. Because some, the first principle is that if you can maintain an index less by adjusting the order, then this order of priorities is often used.

Index pushdown

We were still joint index City watches (name, age), for example. If there is a demand now Feng: check out the table to weigh each word in the name is Zhang, and everyone age 10, then this should be the first to write SQL:

select * from tuser where name like '张%' and age=10

You know the rules of the prefix index, so this statement at the time of the search tree, only "Zhang" find the first to meet the conditions of the recording ID3. Of course is good, good is better than a full table scan. It is then determined whether other conditions are satisfied. Before MySQL5.6, one can only start from ID3 back to the table. The primary key index to identify the data row, and then the field values. The index pushdown MySQL5.6 introduced optimized to index traversal in. To do first field contained in the index is determined, filter out direct recording condition is not satisfied, to reduce the number back to the table. The following flowchart is to perform two processes:

Figure 3. No index pushdown execution process

4. FIG execution flow index pushdown

Wherein each of the dotted arrow shows a back table.

In Figure 3, the (name, age) which specifically removed the index values ​​of age, and this process does not go to InnoDB values ​​of age, but the order of "name first word is 'sheets'" record a remove the round bar table. Therefore we need to return to the table four times.

Guess you like

Origin www.cnblogs.com/laobeipai/p/12004098.html