Performance optimization of the Mysql Index

Performance optimization of the Mysql Index

First, the basic introduction

Index : Index value is a structure of the database table has a column or columns are sorted, index-specific information can quickly access the database tables (from Baidu Encyclopedia). Popular talk is similar to the index directory on the books, you can quickly find an article in the book. So the index is mainly used to improve the retrieval speed.

1, Mysql Performance Analysis

In Mysql by SHOW STATUS LIKE 'value' command to view the performance parameters of Mysql. value value finishing list is as follows:

/**查看链接次数**/
SHOW STATUS LIKE "Connections"; 
/**服务器上线时间**/
SHOW STATUS LIKE "Uptime";
/**慢查询次数**/
SHOW STATUS LIKE "Slow_queries";
/**查询操作次数**/
SHOW STATUS LIKE "Com_select";
/**插入操作次数**/
SHOW STATUS LIKE "Com_insert";
/**更新操作次数**/
SHOW STATUS LIKE "Com_update";
/**删除操作次数**/
SHOW STATUS LIKE "Com_delete";

The above command, the most used is estimated to be slow queries (SHOW STATUS LIKE 'Slow_queries') performance analysis command. Use this command in conjunction with the slow query log analysis of search queries slower sql statement, and then make the appropriate optimization;

2, Mysql slow query log

Slow query log, which is mainly used to analyze specific SQL statements generated resulting in slow query, so targeted optimization system Mysql query. The following summarizes how to configure the service slow query log, and see how slow query log;

/**查看慢查询日志状态信息**/
SHOW VARIABLES LIKE '%slow_query_log%';
/**开启慢查询日志:1表示开启,0表示关闭**/
SET GLOBAL slow_query_log=1;
/**查看慢查询定义的时间**/
SHOW VARIABLES LIKE 'long_query_time%';
/**设置慢查询定义时间。单位:秒**/
SET long_query_time=4;

Two, Mysql learning index

(1), the basic classification

  • General index: any field can be indexed in the table, without any restrictions. It is our most used index
  • Unique index: Field set is preferably the primary key Primary key, data is guaranteed to be unique.
  • Full-text indexes: Only MyISAM table under the mode, Mysql data is IndexDB default mode, so the use of full-text indexing in this mode is invalid. While the full-text index will consume more space to improve query performance for large data more than, less maneuverability table this index is the right way;
  • Composite index: Now the SQL WHERE clause is typically a combination of statements, so we also need to create an index for the combined index to increase query efficiency portfolio;

(2) Basic Operation

  • Ordinary Index:
/**创建索引**/
CREATE INDEX index_name ON table(col_name);
/**OR**/
ALTER TABLE table ADD INDEX index_name(col_name);
/**删除索引**/
DROP INDEX index_name ON table;
/**查看索引**/
SHOW INDEX FROM table;
  • Unique index:
/**创建索引**/
CREATE UNIQUE INDEX index_name ON table(col_name);
/**OR**/
ALTER TABLE table ADD UNIQUE INDEX index_name(col_name);
/**同上**/
  • Full-text index:
/**创建索引**/
CREATE FULLTEXT INDEX index_name ON table(col_name);
/**OR**/
ALTER TABLE table ADD FULLTEXT INDEX index_name(col_name);
/**同上**/
  • Composite index:
/**创建索引**/
CREATE INDEX index_name ON table(col_name1,col_name2,...);
/**OR**/
ALTER TABLE table ADD INDEX index_name(col_name,col_name2,...);
/**同上**/

(3), the index effectiveness analysis

Here to do to demonstrate the general index to analyze performance optimization index caused by demonstrating SELECT query; while fuzzy query the validity of the index will have some impact. The following will summarize;

  • Before unused indexes
EXPLAIN SELECT * FROM table WHERE col_name='xxx';
EXPLAIN SELECT * FROM table WHERE col_name LIKE '%xxx%';

Here Insert Picture Description

  • After using the index - (exact queries)
EXPLAIN SELECT * FROM table WHERE col_name='xxx';

Here Insert Picture Description

  • After using the index - (fuzzy search)
EXPLAIN SELECT * FROM table WHERE col_name LIKE '%xxx%';

Here Insert Picture Description

By summarizing the above shows, the index performance is a good upgrade to the query. For while in the case of fuzzy query, the general index did not bring performance improvement ( here defined in terms of demo, the total does not feel right. Who knows positive solution, looking out promptly ).

(4), the index summary

Summary : The index is a mechanism to optimize the use of space for time, establish a reasonable index can greatly improve the efficiency of data query the database; however excessive use of indexing mechanism will waste more time to maintain the index, take up unnecessary space, influence Mysql the INSERT, UPDATE, and DELETE statements operating speed of the data;

  1. To create the index and join in on the field where used;
  2. The following symbol can use the index: <, <=, =,>,> =, BETWEEN, IN, LIKE; does not begin%, NOT IN may be replaced by NOT exists;
  3. When using the max () min () is preferably indexed and;
  4. To create a single index where really needed. Multi-column indexes have the best left prefix properties. So try to the left of the field is the most common;
  5. Index does not include the NULL value. There are NULL, the index will fail
  6. Using short index, a field word too, can build an index part, only the leading ten words, to save space;
  7. explain select * from myz to test the efficiency of the statement ...

Guess you like

Origin blog.csdn.net/u012475786/article/details/90753856