0 Preface
This article introduces MySQL, the related slow query optimization.
Mysql slow query log is a log, mysql statement can be used to record the execution time exceeds the specified long_query_time SQL statement, the default value is 10s long_query_time
slow query log under the circumstances is not turned on by default, because the data is saved to a log have some impact on performance, the test environment can be opened manually, but then pay attention to manually open only works on this start, restart restore the default state after mysql closed, in order to take effect lasting change my.ini configuration file (under Window system), other system variables also do so. By '% slow_query_log%' show varaibles like to view the log open case
1 Query Optimization
1.1 explain + SQL statement : execute this statement allows developers to see the details of the implementation of the select statement, developers can step on slowly query SQL query statements captured in the analysis, the low efficiency of inquiry to determine the possible causes
id: Number select query
id are the same, perform a top-down order id is not the same, the greater the value of id higher priority, the first to be executed id have the same of different, the same id as a group, according to different sets of values id rules two priority execution, the same group id is sequentially executed in accordance with the rules of a
select_type: select the type of query
simple: it indicates that the query is not connected subqueries and UNION queries outermost query has a subquery: Primary subquery: inner layer when subqueries nested query derived: select included in the called derived from (derived) , mysql will recursively these sub-queries, the results in a temporary table union: union of the second or the last union result: the result of the union
table: the table with the implementation of the current SQL statement
partitions: the representative of the current partition table used
type: show how to use the query, according to several common query order of best to worst system> const> eq_ref> ref> range> index> all
system, const: mysql can be part of the query optimization enables these convert it to a constant (system returns only one, const multiple rows), as the primary key of a row manner into the WHERE clause to select this line primary key, MySQL will be able to convert this query into a constant. Then the table can be efficiently removed from the coupling execution eq_ref: using the lookup index, mysql return up to a known data can be used when using the master key or a unique index lookup ref: non-unique index lookup index range: scanning range, for example between or with>, <, in other index: index scanning all lines all: scanning all rows of data
The actual use of the index and the index may be used on behalf of: possible_keys / kesy
key_len: number of bytes in the index
ref: it shows a lookup table prior to the value recorded in the index key column used in columns or constants
rows: mysq to find the estimated number of rows meet the conditions required for scanning of line
Show Profile 1.2 : mysql Show Profile is used to analyze resource usage of SQL query tool
We perform some tests run SQL statements after the statements show profiles
We can choose to specify items specified SQL statement analysis
Our view is that the general properties and cpu block io two modules
note:
If any of the following situations arise, we have said it was a bad SQL statement that needs to be optimized
- convering heap to MyIsam query result is too large, not enough memory, you need to record to disk
- creating tmp table to create a temporary table to store data, delete exhausted after
- copying to tmp table on disk to store data in the temporary table to disk
- locked
2 Index Tuning
What circumstances need to build the index?
- Primary key, a unique index
- Often used as the query conditions index
- Often we need sorting, grouping, indexing statistics
- Query associated with other tables, foreign key relationships indexes
What case do not build the index?
- Table records too, you do not need to build the index about one million
- Frequent additions and deletions to the list
- Data is repeatedly and uniformly distributed data: such as true, false
- Frequent Updates field
- where conditions are less than in the field