Slow query caused by incorrect MySQL prefix Index

Source: Internet
Author: User

The front-end time is related to a DB project. alanc reports a query. Using indexes is much slower than not using indexes. So I followed up and used explain to look at two different query results.

The results of queries without indexes are as follows. The actual query speed is relatively small.

Mysql> explain select * From rosterusers limit, 3;

+ ---- + ------------- + ------ + --------------- + ------ + --------- + ------- +
| ID | select_type | table | type | possible_keys | key | key_len | ref | rows | extra |
+ ---- + ------------- + ------ + --------------- + ------ + --------- + ------- +
| 1 | simple | rosterusers | all | null | 2010066 |
+ ---- + ------------- + ------ + --------------- + ------ + --------- + ------- +

 

However, the query results using the index order by are as follows, which results in an astonishing slow speed.

Mysql> explain select * From rosterusers order by username limit, 3;

+ ---- + ------------- + ------ + --------------- + ------ + --------- + ------------------ +
| ID | select_type | table | type | possible_keys | key | key_len | ref | rows | extra |
+ ---- + ------------- + ------ + --------------- + ------ + --------- + ------------------ +
| 1 | simple | rosterusers | all | null | 2010087 | using filesort |
+ ---- + ------------- + ------ + --------------- + ------ + --------- + ------------------ +

 

The difference is that the extra used for index query is changed to using filesort. External files are used for sorting. Of course this is slow.

However, the username in the data table is indeed indexed. How can we use using filesort instead?

Let's take a look at the data table definition. Is a table of ejabberd, an open-source chat server. Initially, I thought that the primary key I _rosteru_user_jid is username, and the joint index with jid should be available when order by username is used?

Create Table 'rosterusers '(

'Username' varchar (250) not null,

'Jid' varchar (250) not null,

Unique key 'I _ rosteru_user_jid' ('username' (75), 'jid' (75 )),

Key 'I _ rosteru_jid' ('jid ')

) Engine = InnoDB default charset = utf8;

Check carefully and suddenly find that the primary key definition is not the complete primary key name defined, but with a 75 length description, a slight glimpse, the original uses a prefix index, instead of indexing the entire field. (InnoDB does not support this feature in my memory. It is estimated that InnoDB is added to any version later than 4.0 ), prefix indexes use the first n bytes of data fields as indexes ..

After finding this problem, we began to suspect that slow queries are related to this index. The primary purpose of prefix indexes is sometimes the field process, and many of the index lengths supported by MySQL are limited.

First, the limit query without order by may be related to the primary key in nature, because the InnoDB operations of MySQL depend on the primary key (even if you have not created, the system will also have a default value), while the use of the primary key for limit queries can speed up (the rows returned by the explain statement should be a reference value ), although I have not seen any documentation clearly explaining this problem, the return results of limit queries without order by can basically prove this.

However, when we use order by username, we want to use the username sorting instead of the username (75) sorting, but the actual index is the prefix index, not the index of the complete field. Therefore, the index cannot be used when order by is created. (I add a mandatory index I _rosteru_user_jid in the SQL statement ). In fact, in use, the username field in the table cannot be used for 75 connections, not to mention the defined length of 250. It is all the trouble caused by self-tossing. Because this is a table of other products, we cannot change it. For the time being, we can only pay attention to the query without sorting.

Summary:

  • Prefix indexing is not a panacea. It does help us create an index on a field that is too long to write. However, the prefix index cannot be used for sorting (order by, group by) queries.
  • At any time, for DB schema definition, reasonable planning of their own field length, field type is the first thing.

[The author of this article is Yan du Han tan, in the spirit of freedom, you can be in the profit of the complete reprinted this document, reprinted Please attach blog link: http://www.cnblogs.com/fullsail/ or http://blog.csdn.net/fullsail, the negative is a dollar, every figure one hundred no. For the Baidu Library, the price of 360doc is doubled]

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.