Turn on Slow query
First we check if the MySQL server's slow query status is turned on. Execute the following command:
650) this.width=650; "Src=" http://images.cnitblog.com/blog/44702/201301/ 23175454-351c6f142b774ac9bbe923faf9482c85.jpg "style=" border:0px; "/>
We can see that the current log_slow_queries status is off, indicating that no slow query is currently turned on.
It is very simple to turn on slow queries, as follows:
Find MySQL configuration file under Linux My.ini, add the configuration statement of slow query under MYSQLD (Note: Be sure to add below [mysqld])
650) this.width=650; "Src=" http://images.cnitblog.com/blog/44702/201301/ 23175654-3442bef4aa424618a8c157d4407b6eef.jpg "style=" border:0px; "/>
Log-slow-queries: On behalf of the MySQL slow query log storage directory, this directory file must have write permission;
MySQL Explain usage
Explain shows how MySQL uses indexes to process SELECT statements and join tables. Can help select better indexes and write more optimized query statements.
Using the method, add explain to the SELECT statement, such as:
Explain select * from Statuses_status where id=11;
650) this.width=650; "src=" Http://images.cnitblog.com/blog/270324/201411/271101207779336.png "style=" border:0px; " />
Explanation of the Explain column
Table: Shows which table the data for this row is about
Type: This is an important column that shows what type of connection is used. the best to worst connection types are const, EQ_REG, ref, range, Indexhe, and all
Possible_keys: Displays the indexes that may be applied to this table. if it is empty, there is no possible index. you can select an appropriate statement from the where statement for the related domain
Key: The actual index used. If NULL, the index is not used. In rare cases, MySQL chooses an index that is poorly optimized. In this case, use Index (indexname) can be used in the SELECT statement to force an index or use ignore index (indexname) to force MySQL to ignore the index
Key_len: The length of the index used. The shorter the length the better , without loss of accuracy
Ref: Shows which column of the index is being used and, if possible, a constant
Rows:mysql the number of rows that must be checked to return the requested data
Extra: Additional information on how MySQL resolves queries. It will be discussed in Table 4.3, But the bad examples you can see here are the using temporary and using filesort, meaning that MySQL simply cannot use the index, and the result is that the retrieval will be slow
The meaning of the description returned by the extra column
Distinct: Once MySQL finds a row that matches a row, it no longer searches for
Not Exists:mysql optimizes the left join, and once it finds a row that matches the left join standard, it no longer searches for
Range checked for each record (index map:#): No ideal index was found, so for every combination of rows from the preceding table, MySQL examines which index to use and uses it to return rows from the table. This is one of the slowest connections to use the index
using Filesort: When you see this, the query needs to be optimized. MySQL requires additional steps to find out how to sort the rows that are returned. It sorts all rows based on the connection type and the row pointers for all rows that store the sort key values and matching criteria.
Using index: Column data is returned from a table that uses only the information in the index and does not read the actual action, which occurs when all the request columns of the table are part of the same index
using temporary when you see this, the query needs to be optimized. here, MySQL needs to create a temporary table to store the results, which usually occurs on an order by on a different set of columns, rather than on the group by
Where used uses a WHERE clause to restrict which rows will match the next table or return to the user. If you do not want to return all rows in the table, and the connection type all or index, this occurs, or the query has a problem different connection types of interpretation (in order of efficiency)
The system table has only one row: the system table. This is a special case of the const connection type
Const: The maximum value of a record in a table can match this query (the index can be a primary key or a unique index). Because there is only one row, this value is actually a constant, because MySQL reads the value first and treats it as a constant.
Eq_ref: In a connection, when MySQL queries, from the previous table, the union of each record reads a record from the table, which is used when the query uses the index as the primary key or the unique key.
Ref: This connection type occurs only if the query uses a key that is not a unique or primary key or is part of these types (for example, using the leftmost prefix). For each row union of the previous table, all records are read from the table. This type is heavily dependent on how many records are matched against the index-the less the better
Range: This connection type uses an index to return rows in a range, such as what happens when you use > or < to find something
Index: This connection type is fully scanned for each record in the previous table (better than all because the index is generally less than the table data)
All: This connection type is fully scanned for each previous record, which is generally bad and should be avoided as much as possible.
MYSQL optimization (i)-----slow query