"Mysql - string index should be how to build? "

I. Overview

  - I have a mailbox needs is the need to log in,

  - mysql> select f1, f2 from SUser where email='xxx';

  - We know that, if the index is not on email, it will take a full table scan.

  - So, we have two ways to establish

    -  MySQL> Table suser ALTER index index1, the Add (In Email); // common index

    -  MySQL> Table suser the Add index ALTER index2 (In Email (. 6)); // prefix index

 

Two: the difference between the ordinary index and the index of the prefix?

  - we see them, they establish what is the difference of the index tree

     -                     

  

  -  As you can see from the chart

    - Due to (6) This index structure for each mailbox email field only take the first 6 bytes (ie: zhangs), so the footprint will be smaller, this is the advantage of using a prefix of the index.

    - However, it also brought the loss is likely to record the number of additional scans. 
 

Three: general index and the index query prefixes different processes?

    - Examples

      - select id,name,email from SUser where email='[email protected]';

 

    - General Index

      - from the index tree to find an index value satisfies '[email protected]' of this record, to obtain the value of ID2;

      - to the primary key value is found in the primary key of the row ID2, email determination value is correct, these rows added result set;

      - Take the next record index tree just found the location and found the conditions have been satisfied email='[email protected] 'of the loop ends.

      - this process, only need to take one back to the primary key index data, so the system only scans a line of thought. 

 

    - Prefix index

      - index tree index value is found to meet the 'zhangs' record, the first one found is IDl;

      - to the primary key value is found in the primary key of the row ID1, email determines that the value is not '[email protected]', which rows to discard;

      - to take to the next record found in the location just found is still 'zhangs', taken ID2, then rounding the row index ID is then determined, this value, the addition of these rows the result set;

      - Repeat the previous step until the value is not accessible to 'zhangs', the end of the cycle.

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

 

    - in conclusion

      - Through this comparison, you can easily find, use the prefix index, the query may cause the number of read data becomes large. 

 

Four: discrimination

  - Through the above test, we know, it will lead to inquiries increased, mainly based prefix index of selectivity distinction .

  -   SELECT COUNT(DISTINCT LEFT(column_name, $length)) / COUNT(*) FROM table_name;  // 查询区分度

 

Five: the impact on the index prefix covered by the index

  -  After using the prefix index, the index can not be covered in use , the face of the query, you may need to return to the operating table .

 

Six: the face of a string, we can also take other ways to store

  - hash

  - bit bit

  - reverse

  - and many more

 

Guess you like

Origin www.cnblogs.com/25-lH/p/10978727.html