Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

MySQL statement Analysis and sql process View

2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Database >

Share

Shulou(Shulou.com)06/01 Report--

Configure it in the my.cnf file:

Slow_query_log=1

Slow_query_log_file=mysql.slow

Long_query_time=10

SQL statements that exceed the default 10s execution time can be recorded.

If you want to change the default settings, you can add:

Long_query_time = 5

Set to 5s.

MySQL comes with slow log's analysis tool mysqldumpslow.

Slow log is a file written by MySQL according to the execution time of SQL statements, which is used to analyze slow statements.

Explain to understand the status of SQL execution.

Explain select * from wp_posts\ G

Explain shows how mysql uses indexes to process select statements and join tables. It can help select better indexes and write more optimized query statements.

To use the method, add explain before the select statement:

Such as: explain select surname,first_name form aforme b where a.id=b.id

Interpretation of the EXPLAIN column:

Table: shows which table the data in 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 empty, there is no possible index. You can select an appropriate statement from the WHERE statement for the relevant domain

Key: the index actually used. If NULL, the index is not used. In rare cases, MYSQL chooses indexes that are not sufficiently optimized. In this case, you can use USE INDEX (indexname) in the SELECT statement to force the use of an index or 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 losing accuracy.

Ref: shows which column of the index is used and, if possible, a constant

The number of rows that rows:MYSQL believes must be checked to return the request data

Extra: additional information about how MYSQL parses queries. It will be discussed in Table 4.3.But the bad examples you can see here are Using temporary and Using filesort, meaning that MYSQL cannot use indexes at all, and the result is that retrieval will be slow

The meaning of the description returned by the extra column

Distinct: once MYSQL finds a row that matches the row union, it no longer searches

Not exists: MYSQL optimizes LEFT JOIN so that once it finds rows that match the LEFT JOIN standard, it no longer searches

Range checked for each Record (index map:#): no ideal index was found, so for each combination of rows from the previous table, MYSQL checks which index to use and uses it to return rows from the table. This is one of the slowest connections to use an index

Using filesort: when you see this, the query needs to be optimized. MYSQL needs to take additional steps to discover how to sort the returned rows. It sorts all rows according to the connection type and the row pointer to all rows that store the sort key values and matching criteria.

Using index: column data is returned from a table that only uses the information in the index and does not read the actual action, which occurs when all request columns on the table are part of the same index

When Using temporary sees this, the query needs to be optimized. Here, MYSQL needs to create a temporary table to store the results, which usually occurs on the ORDER BY of different sets of columns, not on the GROUP BY

Where used uses the WHERE clause to restrict which rows will match the next table or be returned to the user. This can happen if you do not want to return all the rows in the table and the join type is ALL or index, or the query has a problem with the interpretation of different join types (sorted 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 unique index). Because there is only one line, this value is actually a constant, because MYSQL reads the value first and then treats it as a constant.

Eq_ref: in a join, when MYSQL queries, the union of each record from the previous table reads a record from the table, which is used when the query uses the index as the primary key or all of the unique key.

Ref: this join type occurs only when the query uses keys that are not unique or primary keys or parts of these types (for example, using the leftmost prefix). For each row join of the previous table, all records are read out from the table. This type depends heavily on how many records are matched by the index-the fewer the better

Range: this connection type uses an index to return rows in a range, such as using > or

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Database

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report