The basic steps of slow query optimization, slow log management

11. The basic steps of slow query optimization

  • Run first to see if it is really slow, pay attention to setting SQL_NO_CACHE
  • Where condition single table check, lock the minimum return record table. The meaning of this sentence is to apply the where of the query statement to the table with the smallest number of records returned in the table and start to look up. Each field of the single table is queried separately to see which field has the highest degree of discrimination.
  • explain to check whether the execution plan is consistent with the expectation of 1 (start querying from the table with fewer locked records)
  • The order by limit form of the SQL statement allows the sorted table to be checked first
  • Understand the business side usage scenarios
  • When adding an index, refer to the principles of indexing
  • Observe the results, do not meet expectations, continue to analyze from the first step

12. Slow log management

Slow log

  • Execution time> 10
  • Miss index
  • Log file path

Configuration

  • RAM
show variables like '%queries%';
set global [变量名] = []
  • Configuration file
mysqld --defaults-file='E:\wupeiqi\mysql-5.7.16-winx64\mysql-5.7.16-winx64\my-default.ini'
  • my.cnf file content
slow_query_log = ON
slow_query_log_file = [日志文件路径]

ps: The service needs to be restarted after modifying the configuration file

Mysql log management

  • Error log: Record MySQL server startup, shutdown and operation errors and other information
  • Binary log: also known as binlog log, which records operations other than SELECT in the database in the form of a binary file
  • Query log: record the query information
  • Slow query log: Record operations that take longer than the specified time
  • Relay log: The standby library copies the binary log of the main library to its own relay log, thereby replaying locally
  • General log: which account to audit, during which time period, and what events were done
  • Transaction log or redo log: record Innodb transaction related such as transaction execution time, checkpoints, etc.

use

1.bin-log

1. 启用
# vim /etc/my.cnf
[mysqld]
log-bin[=dir\[filename]]
# service mysqld restart
2. 暂停
//仅当前会话
SET SQL_LOG_BIN=0;
SET SQL_LOG_BIN=1;
3. 查看
查看全部:
# mysqlbinlog mysql.000002
按时间:
# mysqlbinlog mysql.000002 --start-datetime="2012-12-05 10:02:56"
# mysqlbinlog mysql.000002 --stop-datetime="2012-12-05 11:02:54"
# mysqlbinlog mysql.000002 --start-datetime="2012-12-05 10:02:56" --stop-datetime="2012-12-05 11:02:54" 

按字节数:
# mysqlbinlog mysql.000002 --start-position=260
# mysqlbinlog mysql.000002 --stop-position=260
# mysqlbinlog mysql.000002 --start-position=260 --stop-position=930
4. 截断bin-log(产生新的bin-log文件)
a. 重启mysql服务器
b. # mysql -uroot -p123 -e 'flush logs'
5. 删除bin-log文件
# mysql -uroot -p123 -e 'reset master' 

2. Query log

启用通用查询日志
# vim /etc/my.cnf
[mysqld]
log[=dir\[filename]]
# service mysqld restart

3. Slow query log

启用慢查询日志
# vim /etc/my.cnf
[mysqld]
log-slow-queries[=dir\[filename]]
long_query_time=n
# service mysqld restart
MySQL 5.6:
slow-query-log=1
slow-query-log-file=slow.log
long_query_time=3
查看慢查询日志
测试:BENCHMARK(count,expr)
SELECT BENCHMARK(50000000,2*3);

Guess you like

Origin blog.csdn.net/songhaixing2/article/details/114102850