mysql index (reproduced) http://blog.csdn.net/wenniuwuren

A trick of MySql index
2013-01-21 12:02 1953 people read comments (8) Collection report
Category  : Written test interview (102) mysql (58)
Copyright statement: This article is an original article by the blogger, and may not be reproduced without the blogger's permission .

The establishment of the index will directly affect the query performance.

Look at the following query:

select * from ddd where id>1 order by score;

we query the scores of each subject of students whose student number is greater than 1.

Then according to the general idea, the index is established like this (id, score).

Explain:

[sql] view plain copy
mysql> explain select * from ddd where id>1 order by score; 
+----+-------------+------ -+-------+---------------+------+---------+------+ ------+------------------------------------------------------+ 
| id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra | 
+----+-------------+-------+-------+-------------- -+------+---------+------+------+----------------- -------------------------+ 
| 1 | SIMPLE | ddd | range | id | id | 4 | NULL | 12 | Using where; Using index ; Using filesort | 
+----+-------------+-------+-------+---------- -----+------+---------+------+------+------------- -----------------------------+ 
1 row in set (0.00 sec) 
Looking at the result of explain, we found that the

query process uses order by, using filesort appears. That is to say, MySQL sorts the data that meets the conditions after querying it.

【------------------------------------------------- --------]

This is because, when using the (id, score) index, the query where id > 1 order by score uses a non-constant to limit the first half of the index, so only the index of the index is used. The first half and the second half are not used. Therefore, sorting also needs to be done by mysql. If the query here is where id = 1 order by score, then there will be no filesort.
【------------------------------------------------- --------】

How to optimize the sorting process?

We delete the (id, score) index and create a new (score, id) index.

[sql] view plain copy
mysql> alter table ddd drop index id; 
Query OK, 0 rows affected (0.98 sec) 
Records: 0 Duplicates: 0 Warnings: 0 
 
mysql> alter table ddd add index(score,id); 
Query OK, 0 rows affected (0.11 sec) 
Records: 0 Duplicates: 0 Warnings: 0 Explain 
again:

[sql] view plain copy
mysql> explain select * from ddd where id>1 order by score; 
+----+---- ---------+-------+-------+---------------+-------+ ---------+------+------+---------------------------- + 
| id | select_type | table | type | possible_keys | key | key_len | ref | rows 
| ------+---------------+-------+---------+------+-- ----+----------------------------+ 
| 1 | SIMPLE | ddd | index | NULL | score | 9 | NULL | 28 | Using where; Using index | 
+----+-------------+-------+-------+------- --------+-------+---------+------+------+--------- -----------------+ 
1 row in set (0.00 sec) 
We can see that the explain result has no using filesort.
This is because we want to obtain the scores of students whose id is greater than 1, and finally sort by score, then we scan the (score, id) index to find students whose id is greater than 1, and then directly extract the information. Since the (score, id) index is already sorted, the sorting process is omitted.

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326653179&siteId=291194637