Index Design Principles

1. Set indexes on frequently used fields
Fields are frequently used in conditions such as where and order.
After the data table is created, to estimate which fields are frequently used, create indexes for them
. 2. Consider designing indexes for SQL statements with long execution time.
You can use the "slow query log" to collect such SQL statements and optimize the design of
the index 3. SQL design index with very important logic
For example , in the mall system, it is more important for members to recharge their accounts.
There are also members who place an order for shopping, and it is also more important to make payment.
4. The content of the field is sufficiently varied, and you can consider designing an index,
such as gender, which is not suitable for indexing

prefix index

If the first n bits of information in the content of a field are enough to identify the current field content, the first n bits of the field can be obtained and an index can be created. The index created by the first n bits of the field content is called a prefix index
. Advantages: The physical space occupied by the index is relatively small. Such an index runs fast and has high efficiency, which is of great help to improve the overall performance of mysql.

alter table 表名 add key(字段(位数)) 

It must be determined in advance that the first few bits can mark the current content.
Get the first n bits of the field: substring(field, start position (starting from 1), end position (starting from 1)

select count(*)/count(distinct substring(字段,startIndex,endIndex))from 表名 

write picture description here
According to the above calculation, the index bit information can be considered. Index can be set.

full text index

What is a full-text index:
Other indexes are designed to index the content of the field as a whole. Full-text indexing is similar to that of a composition, and some keywords in the composition are obtained as index content.
The specific understanding is to do like fuzzy query
to get some keyword words in the introduction and make an index.
write picture description here
Note for full-text index:
1. The field type must be varchar/char/text type
2. Before mysql 5.6.4, only Myisam supports it, then both Myisam and innodb support
3. The full-text index in mysql currently only supports English (not Chinese) , you can use sphinx if you need to
support Chinese

创建索引:alter table t_user_auth
add FULLTEXT 
index index_crad_no (crad_no);
删除索引: 
alter table t_user_auth
drop    index  index_crad_no 

Full-text index queries are treated specially:

select * from t_user_auth where   
crad_no like '%1234%'

select * from t_user_auth where   
match(crad_no) against('1234')

Composite full-text index

Create a composite full-text index:

alter table t_user_auth add fulltext index real_name_and_status (real_name,status);

Query the composite full-text index:

select * from t_user_auth where match(real_name,status) against('1,2');

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325785949&siteId=291194637