5 Index 2

Then the contents of the last chapter of this chapter, to explain.
The implementation of "select from T where k between 3 and 6", need to search for a few trees, how many rows will be scanning?

//初始化
mysql> create table T (
ID int primary key,
k int NOT NULL DEFAULT 0, 
s varchar(16) NOT NULL DEFAULT '',
index k(k))
engine=InnoDB;

insert into T values(100,1, 'aa'),(200,2,'bb'),(300,3,'cc'),(500,5,'ee'),(600,6,'ff'),(700,7,'gg');

Here Insert Picture Description
Its index structure in FIG.

Implementation process
Record k = 3 k-tree to find an index acquires ID = 300
Then the index tree ID corresponding to ID = 300 found R3
Then remove the tree index k value k = 5, to obtain ID = 500
Back to the index tree to find ID corresponding to ID = 500 R4
Remove the index tree in a k value k = 6, the condition is not satisfied, the end of cycle

Throughout the process, the process back to the primary key index of the search tree, called back to the table. The above query the process of reading the index k tree three records back to the table twice.

Data query the desired results only on the primary key index has, so had to return to the table. So, it is there likely to go through index optimization, process back to the table to avoid it

Covering index

If the statement is executed the SELECT ID from the WHERE T k the BETWEEN 3 and 5 , then only need to check the ID value, and the value of the ID has the index k in the tree , so you can direct the query results, you do not need back to the table. In other words, this query inside, the index k has been "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 tool
  • On a public information table, whether it is necessary to set up a joint index ID number and name?

CREATE TABLE `tuser` (
  `id` int(11) NOT NULL,
  `id_card` varchar(32) DEFAULT NULL,
  `name` varchar(32) DEFAULT NULL,
  `age` int(11) DEFAULT NULL,
  `ismale` tinyint(1) DEFAULT NULL,
  PRIMARY KEY (`id`),
  KEY `id_card` (`id_card`),
  KEY `name_age` (`name`,`age`)
) ENGINE=InnoDB

If the high-frequency request occurs, according to public inquiries name ID number, a joint index makes sense. Here the use of covering indexes, no longer need to return to query the entire table rows.

The most left-prefix principles

Alone is not a frequent requests to create an index (ID number, address) and a little wasted, how should I do?
B + tree index can be used to "leftmost prefix", to locate the record .
To visually illustrate this concept, we use (name, age) this joint index to analyze.
FIG:
Here Insert Picture Description
Index Entry field order is sorted according to the index definition appear inside

demand operating
All found the name "John Doe" who Quickly navigate to ID4, and then iterate backwards to get all the results you want.
Conditions SQL statement is "where name like 'Zhang%'" Find the first matching record is ID3, then iterate backwards until the condition is not satisfied

Leftmost prefix can leftmost joint index N fields, and may be the leftmost character string index M

In the establishment of the joint index, how to arrange the order of the fields in the index.

  • Evaluation criteria, the index multiplexing capability. When have (a, B) the joint index, generally does not require a separate indexing on a
  • The first principle is that if by adjusting the order, you can maintain a low index, then the order of priorities is often used.
  • Both the joint inquiry, but also based on a, b each query it?
    Query which only b statements can not be used (a, b) the joint index, this time you have to maintain another index, which means you need to maintain (a, b), (b) two index.
  • Space considerations. Which field a small space, the index of the field to create a separate

Index pushdown

//检索出表中“名字第一个字是张,而且年龄是 10 岁的所有男孩
mysql> select * from tuser where name like '张%' and age=10 and ismale=1;

1 using the index of the most left-prefix, navigate to the ID3 position. Then determine the other conditions?
2 Prior to MySQL 5.6, ID3 start only from one back to the table. The primary key index to identify the data row, then compare the field value.
Pushdown optimization index (index condition pushdown) 3 MySQL 5.6 is introduced, the index may traverse process to do first field contained in the index determination, was filtered off direct recording condition is not satisfied, return the table to reduce the number of
Here Insert Picture Description
free index pushdown
Here Insert Picture Description
Index push down

无索引下推图中,在 (name,age) 索引里面我特意去掉了 age 的值,这个过程 InnoDB 并不会去看 age 的值,只是按顺序把“name 第一个字是’张’”的记录一条条取出来回表。因此,需要回表 4 次。

Question: DBA Xiao Lu in the entry of the new company when he found himself taking over the maintenance of the library inside, there is such a table, the table structure is defined like this:


CREATE TABLE `geek` (
  `a` int(11) NOT NULL,
  `b` int(11) NOT NULL,
  `c` int(11) NOT NULL,
  `d` int(11) NOT NULL,
  PRIMARY KEY (`a`,`b`),
  KEY `c` (`c`),
  KEY `ca` (`c`,`a`),
  KEY `cb` (`c`,`b`)
) ENGINE=InnoDB;

公司的同事告诉他说,由于历史原因,这个表需要 a、b 做联合主键,这个小吕理解了。
但是,学过本章内容的小吕又纳闷了,既然主键包含了 a、b 这两个字段,那意味着单独在字段 c 上创建一个索引,就已经包含了三个字段了呀,为什么要创建“ca”“cb”这两个索引?同事告诉他,是因为他们的业务里面有这样的两种语句:

select * from geek where c=N order by a limit 1;
select * from geek where c=N order by b limit 1;

为了这两个查询模式,这两个索引是否都是必须的?为什么呢?

This article is by learning geeks time "MySQL 45 combat stress", do study notes, wrong place, please users suggested that we learn together, follow another update! Scanning the next Fanger Wei code, you can learn together.

Here Insert Picture Description

Released five original articles · won praise 0 · Views 52

Guess you like

Origin blog.csdn.net/qq_34220643/article/details/104921687