The use of four common indexes in Mysql

When it comes to mysql optimization, index optimization is essential. One of the optimization methods is index optimization. Adding a suitable index can significantly improve the concurrency and pressure resistance of the project.

We know that the bottleneck of project performance is mainly in the "select" statement. To improve the performance of "check", the mysql index is essential. Next, summarize the four common indexes of mysql


1. Four kinds of indexes (primary key index/common index/full-text index/unique index)

1. Add index 

1.1 Addition of primary key index

When a table sets a column as the primary key, the column is the primary key index

[sql]  view plain copy  
  1. createtable a(   
  2. id intprimarykey auto_increment,    
  3. namevarchar(20) notnulldefault''      
  4. );  
  5. //Here id is the primary key of the table  

If the primary key index is not specified when the table is created, it can also be added after the table is created:

alter table table_name add primary key (column name);


1.2 Ordinary index

Ordinary indexes are generally added after the table is built.

create index 索引名 on table_name(column1,column2);

alter table table_name add index 索引名(column1,column2);


 
   

1.3 Full-text indexing

First of all, the full-text index is mainly for text files, such as articles, titles, and the full-text index is only valid for MyISAM (InnoDB also supports full-text index after mysql5.6)

[sql]  view plain copy  
  1. createtable c(   
  2. id intprimarykey auto_increment ,    
  3. title varchar(20),  
  4. content text,  
  5. fulltext(title,content)  
  6. )engine=myisam charset utf8;  
  7.   
  8. insertinto c(title,content) values   
  9.     ('MySQL Tutorial','DBMS stands for DataBase ...'),  
  10.     ('How To Use MySQL Well','After you went through a ...'),  
  11.     ('Optimizing MySQL','In this tutorial we will show ...'),  
  12.     ('1001 MySQL Tricks','1. Never run mysqld as root. 2. ...'),  
  13.     ('MySQL vs. YourSQL','In the following database comparison ...'),  
  14.     ('MySQL Security','When configured properly, MySQL ...');  

Common mistakes with full-text indexing:

select * from c where content like "%mysql%";

The full-text index will not be used here, you can view it with explain. Correct usage:

select *  from c where match(title,content) against ('MYSQL');

Remark:

1. The fulltext index in mysql is only valid for myisam

2. The   fulltext provided by mysql takes effect for English -> sphinx (coreseek) technology handles Chinese

3. The usage method is match(field name..) against('keyword')


1.4 Unique Index

[sql]  view plain copy  
  1. createtable d(id intprimarykey auto_increment , namevarchar(32) unique)      
The name in the d table is the unique index. The unique index can have multiple nulls and cannot be duplicate content.

Compared with the primary key index, the primary key field cannot be null and cannot be repeated


2. Query the index

show indexes from table_name;

show keys from table_name;

3. Delete the index

alter table table_name drop index index name;


2. The mechanism of indexing

2.1 Why does the query speed become faster after we add the index?

The traditional query method is to traverse in the order of the table. No matter how many pieces of data are queried, MySQL needs to traverse the data of the table from beginning to end.

After we add the index, mysql generally generates an index file through the BTREE algorithm. When querying the database, it finds the index file for traversal ( half search is very efficient ), and finds the corresponding key to obtain the data.

2.2 The cost of indexing

1. Creating an index is for generating index files and takes up disk space

2. The index file is a binary tree type file. It is conceivable that our dml operation will also modify the index file, so the performance will decrease.

2.3 On which columns are indexes used?

1. More frequent as a query condition field should create an index 

2. Fields with poor uniqueness are not suitable for creating indexes, although they are frequently used as query conditions, such as gender fields

3. Fields that are updated very frequently are not suitable as indexes

4. Fields that do not appear in the where clause should not be indexed

Summary: Only fields that meet the following conditions should be indexed.

a: It must be used frequently in the where bar b: The content of the field is not the only few values ​​c: The content of the field does not change frequently


3. Precautions for the use of indexes

1. For the created multi-column index, as long as the query condition uses the leftmost column, the index will generally be used .

For example, we have added a composite index to title and content

select * from table_name where title = 'test'; will use the index

select * from table_name where content = 'test'; no index will be used

2. For the query using like , if the query is ' %a ' , the index will not be used  , while the query like 'a%' will use the index. Change values ​​such as % and _ cannot be used in the first place 

3. If there is or in the condition, it will not be used even if there is an index in it.

4. If the column type is a string, be sure to quote the data in the condition .


4. How to check the index usage:

show status like‘Handler_read%’;

Notice: 

handler_read_key: The higher the value, the better, the higher the number of times the index is queried.

handler_read_rnd_next: The higher the value, the less efficient the query.


Copyright statement: This article is an original article by the blogger and may not be reproduced without the blogger's permission. https://blog.csdn.net/u013927110/article/details/46636765

Guess you like

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