High-performance MySQL- Chapter VI query performance optimization (1)

Generally speaking, the life cycle of a query can be roughly in the order of view: from the client to the server, and then parse on the server, execution plan, execute, and return the results to the client.

Three indicators for MySQL, the simplest measure of the cost of a query as follows:
Response time
The number of lines scanned
Number of rows returned
The underlying query execution:
The client sends a query to the server.
The server first checks the query cache if a cache hit, then immediately return the results stored in the cache. Otherwise, proceed to the next stage.
Parsing SQL server side, pretreatment, and then the corresponding execution plan generated by the optimizer.
According to MySQL implementation plan generated by the optimizer, the storage engine API calls to execute the query.
The results are returned to the client.
Check status:
Sleep: the thread is waiting for the client to send a new request.
Query: thread is executing a query or is sending the results to the client.
Locked: In the MySQL server layer, the thread is waiting for a table lock.
Analyzing and statistics: the thread is to collect statistical information storage engine, and generating an execution plan for the query.
Copying to tmp table [on disk]: the thread is executing a query, and the result set are copied to a temporary table, this state is generally either do GROUP BY operations, either file sorting operations, or UNION operations. If this state back there "on disk" mark, it means that MySQL is a temporary table into memory on the disk.
Sorting result: the thread is to sort the result set.
Sending data: This represents a variety of situations: thread may transfer data between a plurality of states, or generate a result set, or the data returned to the client.
MySQL There are two sorting algorithms:
Two transmissions ordering (old version):
Read row pointer field and need to sort, sort them, and then reading the data row according to the desired sorting result.
Single transfer Sort (new version):
First read all the columns needed for the query, and then sorted according to the given column, and finally returned directly to sort the results.
Tip query optimizer (hint):
HIGH_PRIORITY和LOW_PRIORITY
This hint tells MySQL, when multiple statements simultaneously access a particular table, the priority of which statements are comparatively high priority which the statement is relatively low.
When HIGH_PRIORITY for SELECT statements, MySQL will be rescheduled to the table until all are waiting for the lock to modify data in this statement SELECT statement. HIGH_PRIORITY can also be used for the INSERT statement, the effect is simply to offset the impact of the global settings LOW_PRIORITY statement.
LOW_PRIORITY is just the opposite: it makes the statement has been in a wait state, there are long queues need access to the same statement a table ---- even those that statement than the statement also later submitted to the server.
Both ideas are only valid for the use of table lock storage engine.
DELAYED
This tip is valid for INSERT and REPLACE. MySQL will use the prompt statement returns immediately to the client, and placed in rows inserted into the buffer, and then writes the data in the batch table is idle.
STRAIGHT_JOIN
After this tip can be placed in the SELECT keyword SELECT statement, it can also be placed between any two names associated table. The first is to make use of all of the tables in the query associated with the order they appear in the statement. The second rule is associated with a fixed sequence before and after the two tables.
SQL_SMALL_RESULT和SQL_BIG_RESULT
These two ideas are only valid for SELECT statements. They tell the optimizer for GROUP BY or DISTINCT query how to use a temporary table and sort. SQL_SMALL_RESULT tell the optimizer that the result is very small rally, the result set can be placed in the temporary memory in the index table to avoid sorting operation. If SQL_BIG_RESULT, then tell the optimizer that the result set can be very large, it is recommended to use a temporary table to do disk sorting operations.
SQL_BUFFER_RESULT
This hint tells the optimizer puts the query results into a temporary table, and then unlock the tables as fast as possible.
SQL_CACHE sum SQL_NO_CACHE
This hint tells MySQL result set whether this should be cached in the query cache.
SQL_CALC_FOUND_ROWS
It will make MySQL returns the result set contains more information. MySQL query plus the prompt removal of the total will be calculated after the LIMIT clause of the query result set to be returned, but in fact only return a result set LIMIT requirements. This value can be obtained by a function FOUND_ROW ().
FOR UPDATE和LOCK IN SHARE MODE
This prompted two major lock control mechanism of the SELECT statement, but only effective for achieving the row-level locking storage engine. Use the tips will match the query lock rows.
USE INDEX、IGNORE INDEX和FORCE INDEX
These tips will tell the optimizer to use or which does not use an index to query record.
In MySQL5.0 and later, add some of the parameters used to control the behavior of the optimizer:
optimizer_search_depth
This parameter controls limit the optimizer in the exhaustive implementation plan.
optimizer_prune_level
This parameter is enabled by default, which makes the optimizer will decide whether to skip some of the implementation plan based on the number of lines to be scanned.
optimizer_switch
This variable contains a number of on / off flag optimizer characteristics.

Guess you like

Origin www.cnblogs.com/zhishuiyushi/p/12444482.html