MySQL- Technology - Performance Optimization shorthand

Introduction to Optimization

  MySQL database optimization is multifaceted, the principle is to reduce system bottlenecks and reduce the footprint, increasing the response speed of the system. In MySQL, you can SHOW STATUS query a number of MySQL performance parameters statement. As the number of inquiries connected, you can perform the following statement: 

SHOW STATUS LIKE 'Connections'; 

If you want to query the number of slow queries, you can execute the following statement: 

SHOW STATUS LIKE 'Slow_queries'; 

Some commonly used performance parameters are as follows: 

- Connections: number of connection MySQL server; 

- Uptime: MySQL server on-line time; 

- Slow_queries: the number of slow queries; 

- Com_select: the number of query operations ( selectcan change insert, , update, deleterespectively insert query, update, delete the number).

  MySQL database optimization can consider three aspects, namely, query optimization, optimization of the database structure, optimize the MySQL database.

Query Optimization

  1. Analysis query.
  2. Use indexes to speed up queries.
  3. Sub-query optimization.

Analysis of query

  By analyzing the query, you can understand the implementation of the query statement. MySQL provides EXPLAIN and DESCRIBE to analyze the query. 

Examples are as follows: 

EXPLAIN SELECT * FROM `user` WHERE username = 'admin'; 

The results are shown: 


Several parameters explain: 

- id: SELECT identifier. This is a SELECT query serial number. 
- select_type: Indicates the type of SELECT statement. 
- table: a table showing the query. 
- type: indicates the connection type of the table. ALLRepresentation of a full table scan. This is the worst result. 
- possible_keys: indicates which indexes MySQL could use to find the rows in the table. If NULL, indicating that no relevant index. 
- key: indicates that the query actually used to index. 
- key_len: indicates the length of the selected index field by MySQL bytes. 
- ref: indicate which columns or constants and query records using the index together. 
- rows: MySQL displays the number of rows in the table must be checked when queried. 
- Extra: For more information about MySQL in dealing with representation of the query.

Use indexes to speed up queries

  MySQL to improve performance in a most effective way is to design a reasonable data table index. Index provides an efficient way to query data and speed up the query speed. Analytical query statements, type of display ALL is the representation of a full table scan, without the use of an index. To username add a named username_index general index, let's analyze query results as shown: 

  type to type ref, which means that all matching rows read from a table, an index is neither nor is the case UNIQUE PRIMARY KEY, or the query using the left column subset index, i.e. the index portion of the left column in combination.

  When using an index There are several special circumstances, in these cases, it is possible to use the field with index query, the index does not take effect, here are several special about this case.

Use the LIKE keyword query. 
In the statement uses the LIKE keyword query, if the first character matched string is "%", the index will not work. Only "%" not in the first position, the index will work.

Multi-column index query. 
MySQL can create indexes on multiple fields. 16 may include an index field. For multi-column index, only the query used in these fields in the first field, the index will work.

OR use the keyword query. 
When the query only query keyword OR, OR and two conditions before and after the columns are the index, only the query using the index. Otherwise, the query will not use the index.

Optimization Subqueries

  Subqueries can do a lot of one-time logically require multiple steps to complete SQL operations. Although sub-query can query very flexible, but the efficiency is not high. The implementation of sub-queries, MySQL needs to create a temporary table to the inner query results statement. Then the outer query query records from the temporary table. After the query is complete, and then withdraw these temporary tables. Therefore, the speed of the sub-queries will be affected to some degree. 
  In MySQL, you can use the connection (JOIN) query instead of a subquery. Join query does not need to create a temporary table, its speed is faster than the subquery.

Optimized database structure

  Reasonable database structure not only allows databases take up less disk space, and can make queries faster. Design of the database structure, you need to consider the data redundancy, speed query and update the data type of the field is reasonable and many other content.

Many of the fields into a plurality of tables Table

For many fields of the table, frequency of use is low some fields, these fields can be separated out to form a new table. Because when the data table is large, due to the presence of low frequency of use field becomes slower.

Increase in the middle of the table

For tables require frequent union query, you can create an intermediate table to improve query performance. By establishing middle of the table, inserting data requires frequent joint inquiry into the middle of the table, and then the original query to a query on the joint middle of the table, in order to improve query performance.

Increased redundancy field

Design database tables should try to follow the statute paradigm theory, minimize redundant field, so that the database design looks sophisticated and elegant. However, reasonably adding redundancy field can improve query speed.

Speed ​​optimization insert records

When recording is inserted, the insertion speed mainly affect the index, the only check, insert a number of records. This can be separately targeted optimization.

Analysis tables, checklists and optimization table

  MySQL provides statement analysis tables, checklists and optimization of the table. The main analysis table is distributed analysis of keywords; checklist mainly checklist for errors; optimization is to eliminate the main table to delete or update space caused by waste.

  1. Analysis table: ANALYZE TABLE table name;
  2. Checklist: CHECK TABLE table name;
  3. Optimization table: OPTIMIZE TABLE table name; (only optimization table VARCHAR, BLOB or TEXT type field)

  The three methods described above, the database system will automatically add a read lock on the table. During the analysis table, the table can only read the record, the record is not updated and inserted. In most scenarios, without the use of optimized table, even for variable-length done a lot of updating, you do not need to run often, once a week or once a month to, and only needs to run on a specific table.

Optimizing the MySQL Server

Optimized server hardware

Hardware performance server directly determines the performance of the MySQL database. Hardware performance bottlenecks, but also determines the speed and efficiency of the database. Commonly used to optimize the server hardware as follows: 
1. large memory configuration. 
2. With the high speed disk system, read the disk to reduce latency, and improve the response speed. 
3. rational distribution of disk I / O, the disk I / O spread across multiple devices, in order to reduce competition for resources, improve the ability to operate in parallel. 
4. The multi-processor configuration, MySQL database is multi-threaded, multi-processor can execute multiple threads simultaneously.

MySQL optimization of the parameters

By optimizing MySQL parameters can improve resource utilization, so as to improve the performance of the MySQL server. 
MySQL server configuration parameters are in my.cnf or my.ini file [MySQLd] group. among them:

  • key_buffer_size: represents the size of the index buffer. All threads share index buffer. Reasonable buffer size can better handle index.

  • table_cache: indicates the number of tables open simultaneously.

  • query_cache_size: indicates that the query size of the buffer.

  • innodb_buffer_pool_size: indicates the maximum cache table and index InnoDB type. The higher the value, the faster the speed of the query will be.

  • max_connections: indicates the maximum number of connections, which is not the bigger the better, excessive number of connections may cause the MySQL server dead. 
    ...... other slightly.

supplement

1. The index is not better? 
Reasonable index can improve query speed, but the index is not better. Before executing the insert statement, MySQL to index the newly inserted record, so the index will lead to excessive insertion slow. 
2. How to use the query buffer? 
Query buffer can speed up the search, but only for more queries, update statements relatively few cases, specific ways to optimize MySQL reference parameters.

Guess you like

Origin www.cnblogs.com/liboware/p/12542028.html