Introduction By enabling the slow query log, MySQL can record query statements that exceed the specified time. By locating and analyzing performance bottlenecks, the performance of the database system can be better optimized. 1. Configure slow query 1. Parameter Description
2. View slow query related parameters show variables like 'slow_query%'; +---------------------------+----------------------------------+ | Variable_name | Value | +---------------------------+----------------------------------+ | slow_query_log | OFF | | slow_query_log_file | /mysql/data/localhost-slow.log | +---------------------------+----------------------------------+ show variables like 'long_query_time'; +-----------------+-----------+ | Variable_name | Value | +-----------------+-----------+ | long_query_time | 10.000000 | +-----------------+-----------+ 3. Configure slow queries It has two configuration methods, one is global variable configuration and the other is configuration file configuration (1) Global variable configuration --Set the slow_query_log global variable to "ON" state set global slow_query_log='ON'; --Set the location where the slow query log is stored set global slow_query_log_file='/usr/local/mysql/data/slow.log'; --Record if the query exceeds 1 second set global long_query_time=1; (2) Modify the configuration file my.cnf (under Linux environment) slow_query_log = ON slow_query_log_file = /usr/local/mysql/data/slow.log long_query_time = 1 Summarize: 2. mysqldumpslow tool mysqldumpslow is a tool that comes with MySQL. With it, we don't have to look for it in the log file above. If the data is ordinary, then checking the log is also a tedious task. 1. Main commands --s: indicates the sorting method--c: number of visits--l: lock time--r: returned records--t: query time--al: average lock time--ar: average number of returned records--at: average query time--t: how many records are returned in front--g: followed by a regular matching pattern, case-insensitive 2. Examples --1. Get the 10 SQL statements that return the most records mysqldumpslow -s -r -t 10 /logs/mysql-slow.log --2. Get the top 10 SQLs with the highest number of visits mysqldumpslow -s -c -t 10 /logs/mysql-slow.log --3. Get the first 10 query statements containing links sorted by time mysqldumpslow -st -t 10 -g "left join" /logs/mysql-slow.log --4. In addition, it is recommended to use | and more when using these commands, otherwise the screen may explode. mysqldumpslow -sr -t 10 /logs/mysql-slow.log | more 3. show profile Show profiles was added after 5.0.37. To use this feature, make sure the version is after 5.0.37. 1. Enable show profile show variables like "profiling";--The default is closed+---------------+-------+ | Variable_name | Value | +---------------+-------+ | profiling | OFF | +---------------+-------+ --enable set profiling=1; 2. Run the specified SQL I run a few SQL statements here, and then run show profiles;--will list all SQL statements executed during this opening period, with the QUERY ID attached +----------+------------+----------------------------+ | Query_ID | Duration | Query | +----------+------------+----------------------------+ | 1 | 0.00168025 | select * from vhr.employee | | 2 | 0.06573200 | select * from vhr.hr | +----------+------------+----------------------------+ --We can see two statements showing the most recent operations. The list size is controlled by the profiling_history_size session variable. The default value is 15 and the maximum value is 100. 3. Diagnose specific SQL show profile cpu,block io for queryid -- corresponds to query_id in 2 SHOW PROFILE CPU FOR QUERY 1;--Query the specific information of query_id 1+----------------------+----------+----------+------------+ | Status | Duration | CPU_user | CPU_system | +----------------------+----------+----------+------------+ | starting | 0.000194 | 0.000000 | 0.000000 | | checking permissions | 0.000012 | 0.000000 | 0.000000 | | Opening tables | 0.000030 | 0.000000 | 0.000000 | | init | 0.000053 | 0.000000 | 0.000000 | | System lock | 0.000011 | 0.000000 | 0.000000 | | optimizing | 0.000003 | 0.000000 | 0.000000 | | statistics | 0.000014 | 0.000000 | 0.000000 | | preparing | 0.000010 | 0.000000 | 0.000000 | | executing | 0.000001 | 0.000000 | 0.000000 | | Sending data | 0.001213 | 0.000000 | 0.000000 | | end | 0.000014 | 0.000000 | 0.000000 | | query end | 0.000012 | 0.000000 | 0.000000 | | closing tables | 0.000019 | 0.000000 | 0.000000 | | freeing items | 0.000070 | 0.000000 | 0.000000 | | cleaning up | 0.000025 | 0.000000 | 0.000000 | +----------------------+----------+----------+------------+ Conclusions that need to be noted in daily development: 1 converting HEAP to MyISAM: The query results are too large to fit in memory, so they are moved to disk; Note: If one or more of the above four appear, it means that the SQL statement must be optimized. The above is an example to explain the details of MySQL slow query. For more information about MySQL slow query, please pay attention to other related articles on 123WORDPRESS.COM! You may also be interested in:
|
<<: js to realize simple shopping cart function
>>: Introduction to several ways to introduce CSS in HTML
Table of contents First Look Index The concept of...
Table of contents Preface Browser compiled versio...
How can you improve web page performance? Most de...
Harbor Harbor is an open source solution for buil...
Why? The simplest way to put it is that pixels are...
Variables defined in SASS, the value set later wi...
Table of contents 1.1. Enable MySQL binlog 1.2. C...
When Mysql associates two tables, an error messag...
Using the knowledge of CSS variables, I will dire...
1 Background Recently, some performance issues ha...
How to uninstall MySQL database under Linux? The ...
First: First, confirm whether the server hardware ...
This article example shares the specific code of ...
A few days ago, a colleague asked me a question a...