MySQL query optimization

1. Use Index

  When using the index, should try to avoid full table scan, you should first consider the establishment of an index on where and order by, group by column involved.

2. Optimize SQL statements

 1) Analysis query: By analyzing the query, you can understand the implementation of the query, identify bottlenecks query execution to optimize queries.

   By explain (query optimization artifact) to view the results of SQL statements, you can help choose a better index and query optimization, write better optimized statement.

   For example: explain select * from news;

 2) Do not use anywhere select * from t, with a specific list of fields instead of "*", do not return any of the fields with less than.

 3) do not index column operation or function used.

 4) limit the query to use as the number of rows returned reduce, reduce data transfer time and bandwidth wasted.

3. Optimize database objects

 1) optimization data type table

  Using the procedure analyse () function table analysis, this function can be made of the optimization to the data type of a column in the table. A table data type of the first principle is: the use of correctly represents the shortest and the type of data stored. This can reduce the use of disk space, memory, CPU cache.

  Usage: select * from table name procedure analyse ();

 2) split table

  Access efficiency can be improved by splitting table table. There are two methods of resolution:

  a. Vertical resolution (in terms of functional modules)

   The table according to its function, it is closely related to the degree of division, deployed to a different library. For example: We will create a custom database workDB, commodity database payDB, user database userDB, etc., it was used to store project data definition table, commodity definition table, user data sheets.

   The primary key and a number of columns in the table, and the primary key and additional zero columns in a table. If a table used in some columns, while the other is not more commonly used, it may be vertical split.

           

  b. horizontal split (divided according to the rules stored )

   When a data table is too large, the data in the table we can be divided according to certain rules, such as a hash userID, then stored in the same tables and a plurality of structures different libraries.

   The value of one or more rows of data it into two separate rows of data tables.

   

 3) an intermediate table used to speed up the search

  Create an intermediate table, table structure and table structure identical to the source, the statistical data to be transferred to the middle of the table, then statistics on the middle of the table, the results they want.

4. Hardware Optimization

 1) CPU optimization

  And selecting a high frequency multi-core CPU.

 2) Memory Optimization

  Use more memory. We will try to allocate more memory to caching MySQL do.

 3) disk I / O optimization

  a. Using RAID

   RAID 0 has no data redundancy, there is no data validation disk array. Implement RAID 0 requires at least two drives, two or more hard disk will be merged into one, data is divided successively on each plate.

   The RAID 1 is a RAID array of two hard disk drives, only a capacity equal to the capacity of a hard disk, just as another piece of data as "image."

   RAID-0 + 1 using the disk array. RAID 0 + 1 is a combination of RAID 0 and RAID 1 is. It is in the same RAID 1 while providing data security, but also provides storage performance with RAID 1 approximation.

  b. Adjust the disk scheduling algorithm

   Select the appropriate disk scheduling algorithm can reduce the disk seek time.

5.MySQL own optimization

  Optimization of MySQL itself is adjusted to optimize the parameters of its main configuration file my.cnf in. If you specify MySQL query buffer size, specify the maximum number of connections and other processes MySQL allowed.

6. Application Optimization

 1) use of a database connection pool

 2) practical query cache

  Its role is to select the text and the corresponding results are stored query. If you subsequently receive a same query servers directly query results from the query cache. Applicable query cache object is not frequently updated tables, when the data in the table changes, the relevant entry in the query cache will be cleared. 

Guess you like

Origin www.linuxidc.com/Linux/2020-03/162666.htm