Character Index Tuning
Introduced
If you have a system that is used as a mailbox account, each landing check the mailbox, if you do not, then indexed to the mailbox, and each time the query is whole table query, add it to the mailbox index is required, but if you give E-mail plus the general index of the words and feel a waste of space
Prefix index
Add index
普通索引
alter table SUser add index index1(email);
前缀6的索引
alter table SUser add index index2(email(6));
Index Structure
Search differences
select id,email from SUser where email='[email protected]'
Use index1
1. Direct search index1 index tree to find [email protected] this record
2. Then check the next record, if the condition is satisfied directly return a result set (cover index)
Use index2
1. Direct search index index2 tree, and find this record zhangs
2. check the mailbox back to the table meets
3. Continue to next record
note
Use the prefix index can not use a covering index
Summary index of the prefix
1. You can save storage space of the index, and will not add much to the cost of a query (the premise, already operating there back to the table, and set up the appropriate index prefix length)
2. can not be used to optimize coverage index
How to set a reasonable index prefix
1. The length of the prefix index
Through case we know of unreasonable extra if the prefix length is set index increased many additional queries
select count(distinct email) as L from SUser;
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 SUser;
The above statement can query prefix repetition rate so as not to repeat the rate of 95 per cent limit, the shorter the better prefix
2. Reverse
Let's say the ID number, the first few provinces are encoded, there is a lot of repetition, but it is the last few disorderly, we can save backwards, so that you can more effectively the
3. Calculation of the string hash value unique algorithms
Need to add a new field, but there may be repeated, but the shorter the length of the index
to sum up
If you must use the characters for the index, you need to take into account a lot of things, say the amount of data, the data is less, then it is not optimized, reliable and secure, and simple
Reference links
https://time.geekbang.org/column/article/71492