SQL optimization steps

   When the rapid growth of production data, many SQL statements may begin to expose performance problems. When faced with a SQL database performance problems, where do you start with a systematic analysis, makes it possible to locate the problem as soon as possible to solve the problem as soon as possible and at SQL?

Step 1: Check SQL execution frequency

  After the MySQL client connection is successful, the show [session | global] status command provides server status information. show [session | global] status may session level (current connection) of global level statistics and statistical results (since the database was last started from) the need to add the parameter "session" or "global" are displayed according to. If you do not write, use the default session.

  

   Displays the current session statistics for all parameters:

  

   

  Show global-level statistics for all parameters:

  

   

  

  

 Step two: positioning a low efficiency in the implementation of SQL statements

  It can be low in two ways positioned the efficiency sql statement

  1) slow query log: with --log-slow-querles [= file_name] option to start, mysql to write a log file of all the execution time than long_query_time seconds sql statement contains.

  2) show processlist: Slow query log record after the end of the query, the query slow query log when problems reflect the efficiency of the application and can not locate the problem, you can use the show processlist mysql command to view the current thread making, including thread state whether the lock tables, etc., can be viewed in real-time implementation of sql, while some table lock to optimize the operation.

  

  

 The third step: explain analysis of the implementation plan

  After querying inefficient sql statement Through the above steps, you can obtain information on how to perform the select statement by mysql explain desc or command, including how to connect and connected sequentially select statement during execution table.

  Query execution plan sql statement:

  

  

   id: id same order from top to bottom of the table represents a load; id different id values ​​larger, higher priority, the first to be executed; id have the same or different, the same id execution order from top to bottom, different id the greater the value of the id to be executed.

   select_type: select the type of representation, have the following common values:

  

  

   table: Display data on which this line is a table

   type: the type of access is shown, it is a more important indicator, possible values:

   

  

   key:

  

   The number of scanning lines: rows

  extra: other additional execution plan information in this column show

  

 Step four: show profile analysis sql

  From the start Mysql 5.0.37 version adds support for and show profiles show profile statements, show profiles can help in doing sql optimize our understanding of the time-consuming gone.

  View through the current mysql command select @@ profiling is turned profile: (1 said it had opened, you can set profiling = 1 command to turn on, set profiling = 0 closed)

  

   Just perform a few sql statement:

  

   View consuming sql statements executed by show profiles command:

  

   Article sql view a state of each thread and consumed during execution by show profile for query query_id Command Time

  

   Note: Sending data mysql thread state is beginning to access the data lines and the results returned to the client, not just back to the client, Sending data due in the state, mysql threads tend to take a lot of disk read operations, so often the longest state of the whole query time-consuming.

   After obtaining the thread state's most time-consuming, mysql support further select all, cpu, block lo, context switch, page faults and other details of the type of view in mysql using what resources spent too much time on, for example, choose to view the cpu waste time:

  

 Step five: trace analysis optimizer execution plan

  mysql5.6 provides for tracking sql trace by trace file to learn more about why the optimizer can choose Plan A Plan B instead of choosing

  Open trace. Set json format and set the maximum memory trace Bear can be used ah, to avoid the resolution process because the memory is too small to be able to complete the show.

  

  After execution of the sql statement, check information_schema.optimizer_trace can know how to execute sql mysql is in.

   

Guess you like

Origin www.cnblogs.com/jpxjx/p/12551247.html