First check whether it is enabled:
show variables like '%slow_query_log%';
Enable slow query log:
set global slow_query_log=1;
View parameter settings:
show variables like 'long_query_time'; //The query here is how long it takes to record a slow query
By default , sql statements longer than 10 seconds are recorded in the log
Change parameters:
set global long_query_time=0.1; the sql statement that satisfies the query time of 0.1 seconds will be recorded
After modification, the modified value cannot be directly seen, as shown in the following figure
So, we need to open a new window to view
Turn on the system variable log-queries-not-using-indexes : Queries that do not use indexes are also recorded in the slow query log (optional). If tuning, it is recommended to enable this option.
show variables like 'log_queries_not_using_indexes';
set global log_queries_not_using_indexes=1;
When we turn everything on, it will record all sql statements that exceed the threshold we just set in the slow query we set
Next, we will analyze the current sql statement
The reason for the slow analysis statement:
We turned on slow query to record some SQL statements that have been executed for a long time. Finding these SQL statements does not mean that it is over.
Then analyze the statement through explain or pro file s
0: Check if the profile is enabled
show variables like "profiling";
set profiling=on;
then pass
1)Show profiles;
This command is to display the working status of all current connections .
2 ) View the execution process of a single statement
show profile for query 5;
The 5 here refers to Query_ID, which means which sql statement you want to analyze
When is a temporary table created ?
1: When the column of group by is different from the column of order by, 2 When checking the table side by side, take the content of table A, and group/order by the columns of other tables
2: When distinct and order by are used together
3: The SQL_SMALL_RESULT option is enabled
When is a temporary table written to disk ?
Answer :
1: When the retrieved column contains the text/blob type---the memory table cannot store the text/blob type
2: There is a string column > 512 bytes in the group by or distinct column
3: The select contains a string column > 512 bytes, and the union or union all statement is used at the same time
Less use of subqueries and less use of group by
In daily work, sometimes we often use the explain command to view the execution plan of an SQL statement, check whether the SQL statement uses an index, and does a full table scan, which can all be done through the explain command. Check.
The explain shows how MySQL uses indexes to process select statements and join tables. Can help choose better indexes and write more optimized query statements.
column analysis of explain
id: represents the number of the select statement. If it is a join query, the tables are in an equal relationship. The select number is all 1, starting from 1. If there is a subquery in a select, the number is incremented.
explain select count(user) from lz_admin where user='zll';
Type column: It refers to the query method, which is very important and an important basis for analyzing the " data query process " ( the performance is getting better and better from top to bottom )
possible values
all: means to do a full table scan row by row from the first row of the table, after that, scan to the last row if you are unlucky.
index: slightly better performance than all,
In layman 's terms: all scans all data rows, equivalent to data_all index scans all index nodes, equivalent to index_all
range: means that when querying, it can scan the range based on the index
ref means that through the index column, you can directly refer to some data rows
eq_ref refers to a row of data that is directly referenced through the index column, which is commonly used in join queries
const, system, null These three respectively refer to the query optimization to the constant level, even without the need for lookup time.
Generally, when querying according to the primary key , it is easy to appear const, system
Or query an expression directly , without going through the table, NULL appears
max, min are optimized in the table, no real search is needed, they are NULL, so the type is null
The ref column refers to the field reference relationship between the tables when connecting the query. Shows which column or constant is used to select a row from the table together with the key.
rows : refers to the estimated number of rows to scan.
extra:
using index: refers to the use of index coverage, which is very efficient
using where means that the index can not be used to locate, you have to judge where
Using temporary refers to using a temporary table, when group by and order by are different columns, or group by, order by columns of other tables.
using filesort : file sorting (files may be on disk or in memory),
Select tables optimized away This value means that by using the index alone, the optimizer may return only one row from the aggregate function result
select sum(shop_price) from goods group by cat_id (this sentence uses temporary tables and file sorting)
Type column: refers to the query method, which is very important and is an important basis for analyzing the "data query process"
possible values
all: means to do a full table scan row by row from the first row of the table, after that, scan to the last row if you are unlucky.
expand
http://blog.csdn.net/kk185800961/article/details/49179619
https://www.cnblogs.com/xiaoboluo768/p/5400990.html
https://www.cnblogs.com/lpfuture/p/5756543.html