Mysql Performance Optimization: How to string indexed?

Original: Mysql Performance Optimization: How to string indexed?

REVIEW

  • Most modern systems support Login E-mail, mobile phone number to log in two ways, then how to ensure the best performance indexing on a mailbox or phone number string it?

  • Today article're going to explore how to add a string index to achieve the best performance in the Mysql.

  • This article first appeared in the author's public micro-channel number [ code technology ape column ], the original is not easy, like a friend to support you, thank you! ! !
  • Chen will be from what is a prefix of the index , the comparison index and the general index of the prefix , such as the prefix index He Jianli best performance , the impact on the index prefix covered by the index of these paragraphs is concerned.

 

Prefix index

  • As the name suggests, for long column values, such as , , , we "must" use the prefix index , the index soon as the first part of the value. Because the index of storage space is required, the same index is also more difficult to maintain them too long.BLOBTEXTVARCHAR

  • For example, we have to Useradd a prefix index table mailbox, as follows:

   alter table user add index index1(email(7));
  • The above statement is the first seven characters of email as an index.

 

Compare prefix index and general index

  • We will separately as an index to see if there is any difference in the performance of the index as a whole and the first seven characters. Indexed statement is as follows:email

  alter table user add index index1(email);

  alter table user add index index2(email(7));
  • Suppose usertable has revealed the following data (ID, name, In Email): , , , .(1,"陈某","chenmou1993@xxx")(2,"张某","chenmou1994@xxx")(3,"李某","chenmou1995@xxx")(4,"王某","chenmou1996@xxx")

  • Index2 and index1 corresponding to the index tree below two FIG:

 

  • If you perform the following query, Mysql how to use the index to query it?

  select * from user where email="chenmou1995@xxx";

 

[1] execution of a regular index

  1. Index1 index tree found in the index value is satisfied chenmou1995@xxxin this record, achieved id=2value;

  2. To the primary key value is found in the primary key id=2of the row, email determination value is correct, these rows added result set;

  3. Take index1the next record index tree just found the location, found has not satisfied email=chenmou1995@xxxthe conditions, the end of the cycle.

This process, only need to take one back to the primary key index data, the system considers only scan one line .

 

Implementation process [2] prefix index

  1. Find the index value satisfies index2 index tree is chenmourecorded, the first one found is id = 1;

  2. Found on the primary key is the primary key row id = 1, it is determined that the value is not email chenmou1995@xxx, which rows to discard;

  3. Taking the next record just found index2 position, still found chenmou, remove id = 2, then rounding the row index ID is then determined, this value, the addition of these rows the result set;

  4. Repeat the previous step until the value is not to take idxe2 chenmouthe loop ends.

  In this process, going back to the primary key index takes four times the data is scanned four lines.

 

  • By comparing the above query, it is easy to find, after the use of the prefix index, the query may cause the number of read data becomes large.

  • But for this query, the length of the prefix index of 13 if it is established? So meet chenmou1995record of only one, so that you can locate directly to id=2, this time not only reduces the space, the number of scanning lines is also reduced.

  • Then came the conclusion: use the prefix index, as long as the definition of a good length, it can be done not only save space, it does not add much additional query cost.

  • So how to establish the correct prefix index to achieve the best performance? Read on ................

 

How to build the best performance of the prefix index

  • Through the above comparison, we can draw a conclusion that the establishment of the prefix index of distinguishing the higher the better, the less means that duplicate key values .

  • So how statistical discrimination, is actually very simple, just need a database to determine the number of repetitions can be. sql as follows:

  select 
   count(distinct left(email,4))as L4,
   count(distinct left(email,5))as L5,
   count(distinct left(email,6))as L6,
   count(distinct left(email,7))as L7,
  from user;
  • But if you use a prefix for discrimination is not a good situation, for example, our national ID number, a total of 18, of which six are former address code, so the people of the county of the same ID number is usually before 6 identical. At this time if the ID number to make a length of 6 words prefixed index, the index of discrimination is very low.

  • According to the way we said earlier, you may need to create a length of more than 12 prefix index, to be able to meet the requirements of discrimination.

  • However, the longer the selected index, the greater the amount of disk space, the less the same data page can put down an index value search efficiency also will be lower.

  • So, if we can determine which business requirements in accordance with the ID card needs only the equivalent of a query, there is no other treatment methods? In this way, not only can take up less space, but also to achieve the same query efficiency. Now a brief way to solve this problem, of course, certainly more than one method, as follows:

 

  Reverse memory

  If you store ID number when it upside down deposit, per query, you can write:

   field_list SELECT from T WHERE id_card = Reverse ( 'ID number entered.');

  Since the ID number of the last 6 no such logical address code is repeated, so that the last 6 is likely to provide a sufficient degree of distinction. Of course, in practice you do not forget to use the method to do a verification.count(distinct)

 

The impact on the index prefix covered by the index

  • Prefix index would lead to failure of covering indexes, the query statement is as follows:

  select id,name from user where email="chenmou1995@xxx";
  • The use of the prefix index, it must be back to the table to verify the correct query to time, it is used here to cover the index is invalid.

  • In other words, use the prefix index covering indexes do not have access to optimize query performance, and this is a factor in the choice of whether or not you use the prefix index needs to be considered.

 

to sum up

  • How to add a string index is a need to consider the issue, Chen here give the following suggestions:

  1. If the string length is very short, it is recommended directly entirety index.

  2. Note the use of the prefix index analysis discrimination, discrimination better.

  3. Use the prefix index covering issues need to be considered invalid index.

 

Guess you like

Origin www.cnblogs.com/lonelyxmas/p/12630081.html