php interview topics --- 18, MySQL query optimization test sites

php interview topics --- 18, MySQL query optimization test sites

A summary

Sentence summary:

Slow query: Find Cause Analysis query slow
Data Access: Data Access query optimization process
Long sentences: long hard optimize query
Specific types: a specific type of query optimization

 

1, mysql slow query in which the query how?

Slow query log: those which will record slower log, you can use pt-query-digest analysis tools
explain the statement: you can analyze the efficiency of a single query statement
show profile, show status, show processlist other statements: slow query execution as well as a variety of messages or other circumstances

 

Record slow query log: which recorded those slow query
analysis query logs: Do not directly open slow query log analysis, this is more a waste of time and effort, you can use pt-query-digest tools for analysis

using the show profile: set profiling = 1; open, all statements will detect the time spent executing on the server to store temporary tables
show profile for query temporary tables ID: each profile can query the time spent recording the temporary table

using the show status: show status will return some counter, show global status view of all server-level count
using the show processlist: see if there is a large number of threads in an abnormal state or feature
using explain: analysis of a single SQL statement

 

2, how to optimize data access mysql query process?

Try just need to take the rows and columns: rows indexed with terms, aspects of the column with the desired


Too many access data query performance degradation leads to
determine whether the data in the application retrieves a lot more than necessary, it may be too many rows or columns
to confirm whether the MySQL server analyze large amounts of unnecessary data line


whether the scan additional recording
using explain to analyze if the discovery query needs to scan large amounts of data and returns only a few rows, you can go through the following optimization techniques:

the corresponding row using an index scan coverage, with all the columns are placed in the index, so the storage engine does not need to get back to the table can return a result
to change the structure of the database and tables, modifying table data paradigm
rewrite the SQL statement, the optimizer can execute a query in a more optimal way


to avoid using the following SQL statement
1, the query does not need the record: use the limit to solve
2, multi-table returns all associated column: Specifies A.id, A.name, B.age
. 3, always remove all of the columns: SELECT * optimizer will not complete scan coverage optimization index
4, the same data is repeated queries, data can be cached, the next direct read cache

 

3, mysql SQL statement we should avoid doing?

1, the query does not need the record: Using limit to solve
2, multi-association list to return all columns: Specify A.id, A.name, B.age
3, always remove all of the columns: SELECT * optimizer will not complete coverage optimization index scan
4, do not cache: repeated query the same data, the data can be cached, the next read cache directly

 

1, the query does not need the record: Use the limit to solve
2, multi-table associated with the return of all columns: Specify A.id, A.name, B.age
3, always remove all of the columns: SELECT * optimizer will not complete index covering scanning optimization
4, repeat the same inquiry data, the data can be cached, cache the next read directly

 

4. How long is difficult to optimize mysql query?

Segmentation query: a large query into multiple queries in the same small: 10 million of one-time delete to delete data than 10,000, the program pauses for a loss of more server overhead
Decomposition related inquiries: one related statement can be broken down into multiple SQL to perform

 

Segmentation query: a large query into multiple queries in the same small: a one-time delete than 10 million data deletion 10,000, suspended for a loss programs more server overhead

decomposition associated with the query
a statement can be associated broken down into multiple SQL to perform
to make caching more efficient
to perform a single query can reduce the lock of competition
in the application layer association can make it easier to split the database

query efficiency will be dramatically
less redundancy record query

 

5, mysql, we use a more complex query or use a simple query?

Internal inquiries fast, slow and client interaction: MySQL internal memory can scan millions of rows of data per second, compared to the response data to the client will be much slower
A number of simple queries necessary to: use a query as little as possible is good, but sometimes a large query into multiple smaller queries are necessary

 

6, how to optimize a particular type of mysql query?

Optimization count (*) query: count (*) * will ignore all of the columns, all direct statistical series, so do not use count (column name)
Relational query optimization: ON or determine the index column USING clause; ensure GROUP BY and ORDER BY only one column in the table, so MySQL will it be possible to use the index
Optimization Subqueries: Using Queries related to possible alternative
Optimization GROUP BY and DISTINCT: These two queries can be used to optimize the index, is the most effective optimization methods


Optimization count (*) query
count (*) in * ignores all the columns, the direct counting all columns, so do not use count (column name)
MyISAM, there is no count WHERE conditions (*) very fast; when there WHERE condition, MyISAM count of statistics is not necessarily faster than other table engines

can use the query explain approximation with approximation alternative count (*)
increase in the summary table
using the cache


optimized relational query
determines that there is an index on the column oN or uSING clause
to ensure the GROUP bY and ORDER bY only one column in the table, so MySQL will it be possible to use the index


to optimize subquery
using the associated queries as to replace


optimize GROUP bY and DISTINCT
two queries can be used to optimize the index, is the most effective optimization methods
associated query, the efficient use of identity columns are grouped will be higher
using the ORDER BY NULL if not require ORDER BY, conducted gROUP BY, MySQL will not be sorted file
WITH ROLLUP super polymerization, the application process can be moved to


optimize the paging LIMIT
LIMIT when the offset is greater, the lower the efficiency of the query, you can record the maximum of the last query ID, the next time the query directly from the ID to query


optimization UNION query
UNION ALL efficient than UNION

 

 

 

Second, content in summary

 

 

 

 

Reproduced in: https: //www.cnblogs.com/Renyi-Fan/p/11078851.html

Guess you like

Origin blog.csdn.net/weixin_33720956/article/details/93571705