[MySQL] sql optimization

Disclaimer: This article is a blogger original article, shall not be reproduced without the bloggers allowed. https://blog.csdn.net/wrs120/article/details/80716574

1. optimized processes

But usually we would not be up to Explain, usually for one of the following procedures:

  1. Detected a problem with mysql
  2. Turn on slow query log, set thresholds, such as more than two seconds is slow SQL, and grab it out .explain + slow sql, analyzed
  3. show profile sql query execution details and statement cycles where mysql server inside
  4. SQL database server parameter tuning

The following steps are done for each presentation

2. The slow query log settings

The default mysql slow query log is turned off:

  • View status: SHOW VARIABLES LIKE '%slow_query_log%';OFF represent the slow query log is disabled
- temporary turn: `set global slow_query_log = 1;` (only the effect on the current database, mysql restart failure) - permanently open: my.cnf file modify, add or modify the parameters [mysqld] node:
show_query_log=1
slow_query_log_file=var/lib/mysql/xxx-slow.log
(指定慢查询日志文件的存放路径,系统默认给一个缺省的文件hostname-slow.log)
  • View threshold: the slow count how many seconds SHOW VARIABLES LIKE'long_query_time';
  • Set thresholds
set global long_query_time=3;

或在my.cnf的[mysqld]节点下配置:

long_query_time=2;
log_output=FILE

  If the running time is equal long_query_time nice, and will not be recorded
  may see no change after setting, you can be reconnected or open a session, orSHOW GLOBAL VARIABLES LIKE '%slow_query_log%';

3. optimization principle

3.1 never driven a large table small table

Existing two tables A and B:

If A is greater than B the table data set table data set, in use than exists:

select * from A where id in (select id from B)
等价于:
select id from B
select * from A where A.id = B.id

If A is less than B the table data set table data sets, with superior exists in:

select * from A where exists (select 1 from B where B.id = A.id)
等价于
select * from A
select * from B where B.id = A.id

select …from mytable exists(subquery)

  • exists: the main data query into subqueries some conditions are verified, to determine whether the data is preserved main query results based on the verification result (true / false)
  • exists (subquery) returns only TRUE or FALSE, and therefore the sub-query select * but also select 1 or select
    'any value', the official said that the list will be ignored select the actual implementation, there is no difference
  • The actual implementation process exists subquery may been optimized rather than one by one our understanding of contrast

3.2 order by Keyword Optimization

  • order by clause, try to use the index sort of way, to avoid the use FileSort sort

    • mysql supports two FileSort sort and index, Index, Index high efficiency, which refers to the completion of ordering mysql scanning the index itself, less efficient way FileSort
    • Sort Order By using the case of Index: @ 1.oreder by most left the forefront of the index; @ 2 where clause and order by clause conditions combined to meet the leftmost forefront index.
    • But one thing, when there are two or more inconsistent sort field, collation, use using FileSort
      Write pictures described here
  • As far as possible to complete a sort operation on the index, follow best left prefix index

  • If not in the index column order by, filesort There are two algorithms: Dual sorting (using dual MySQL4.1 before sorting, i.e. conjunction scan the disk, the data finally obtained, and the read row pointer orderby column, sort the buffer, then take other fields from the disk. When the disk was 2 I \ O, time consuming), and single sort after (query needs to read column from the magnetic disk, in accordance with the order by sorting them in column buffer, then scan ordering column output, so that a higher efficiency and avoid the second read data. Thus the random I / O into a sequential I / O, but it uses more space, saved because each row in the memory, Therefore, there may be a total size exceeds the data extracted sort_buffer capacity, it can only lead to sort the data capacity size sort_buffer [create a tmp file, multiple combined], drained and then take sort_buffer capacity size, then the resulting exhaust ... time I / O), single unresolved issues arise, use the following solutions:

  • When the order by select * is taboo, namely to reduce unnecessary return to field

  • Sort_buffer_size improved ( this can not be decided to use mysql single sort, but to reduce the number of segment ordering mysql less the I / O )

  • Max_length_for_sort_data increase ( when all the fields of the returned length is less than the maximum value of this parameter, mysql will select single sorting, increase this parameter will increase the probability of the improved algorithm used , but if set too high, the probability exceeds the total capacity of data sort_buffer_size is increased, obvious symptoms of high disk I / O activity and low processor utilization)


3.3 group by Keyword Optimization

  • After the first group by the essence of the sort grouping, follow best left prefix of the index construction
  • When not using the index column, increase sort_buffer_size and parameter settings max_length_for_sort_data
  • where above having, can be written in defined conditions where not going to define HAVING

4. mysqldumpslow Tools

  In a production environment, the log file is great, if manual log analysis, search, analysis mysql, not realistic, MySQL provides a log analysis tool mysqldumpslow:

Common tools are as follows:

5. show profile

5.1 What
  is the mysql provide can be used to analyze the current session resource consumption in the execution of the statement can be used to tune the SQL measuring
5.2 to see if mysql support Profile Show
5.3 View Status Show variables like 'profiling';

  The default is off, and keep the results of the last 15 runs

5.4 = ON Profiling open the SET;
5.5 to view the execution of sql results: Show Profiles;
Write pictures described here

5.6 Diagnostic sql: show profile cpu, block io for query query id number (show profiles obtained Query_ID)
Write pictures described here
contents parameter that is to be viewed can be:

5.7 What sql statement indicates that the article in question?
When stauts the following statement indicates a problem:

  • converting HEAP to MyISAM: the query result is too large, not enough memory to disk moved
  • creating tmp table: create a temporary table, copy the data to a temporary table, then delete
  • copying to tmp table on disk: the copy in memory temporary tables to disk, then it is very dangerous! ! !
  • locked: locked up

6. Global Query Log

Open only in a test environment, it must not be open in a production environment

  • Use the configuration file open: Modify the my.cnf file:
# 开启
general_log=1
#记录日志文件的路径
general_log_file=/path/logfile
# 输出格式
log_output=FILE
  • Use the command to open:
# 开启
set general_log=1;
# 输出形式
set global log_output='TABLE';

  Since then write sql statement will be recorded to general_log table mysql library

  • View the log file: select * from mysql.general_log;
    Write pictures described here

  When a problem is the implementation of mysql appears certain period of time, can be viewed in a global query log, because it records the time, so that you can find problems sql statement execution time, and show prifiles query execution time sql statement, focusing there is no question of investigation


Guess you like

Origin blog.csdn.net/wrs120/article/details/80716574