mysql query optimization finishing

Based on the index level

1, the index covering the SELECT to query data in fact have been included in the index of (both columns to query or query's WHERE condition), so queries can no longer walk in the table, reducing query data

2, where the conditions such as the index pushdown deptno like 'A%' and empno like 'B%', and there is currently a combined index (deptno, EMPNO), then the index will be performed two ways
1) according to find deptno all matching records, according to the need to return to the secondary index mysql query table rows back to back manner corresponding table taking all the records, and then retrieves the corresponding record of the query requirements empno
2) to find all matching records in accordance deptno , not directly back to the table and then select records that meet the requirements of empno query from the list of matches, to return to the table to read out the data.
mysql default index pushdown, aims to reduce the number of back-table query records

3, leftmost prefix currently created joint index (deptno, empno)
database were recorded deptno, empno = [{10,11} , {10,12}, {11,21}, {12,22}, { 30, 31}, {30, 32}] then when the condition where written where deptno like when '1%' and empno like ' 2%' a. The most left-prefix deptno conditions will first take to the range of records to meet the conditions, and then read the query in this range corresponding record. This function points to the index and MYSQL contact B + tree structure.

B + tree structure before and after sequential association before each final leaf node, the association features help expand the leftmost prefix

4, not on a column carried out in conditions where the function calculation, such as where f (x) = y (z) +20. Because use of the index is equivalent to directly match the index value stored in compare compare. Once the function introduced in the calculation, it means to each index value are performed after the first calculations to determine eligibility

5, the index including possible group by, order by words corresponding to columns. Because the group by the order by sort operation will involve the result set. Especially when the corresponding result set is large and will trigger a massive SQL-intensive sorting operation in the temporary table ESA such as extra column in the execution plan can be seen in this file sorts prompt prompt means that there may be a sort operation, even results in the database connection is lost due to the emergence of a SQL query. Adding an index to the group by, order by the corresponding column, using the index of the natural ordering of the sorting operation to reduce or eliminate the list space. SQL reduce both time results feedback, but also to reduce the demand for temporary table space pressure

6, a large amount of data to delete or insert data tables, may be used to re-operate OPTIMIZE TABLE innodb primary key table to adjust the level of physical

SQL-based writing
1, rather than try to use UNION ALL UNION
simple UNION ALL returns the result set by adding, to the absence of re-operation. And UNION seems simple, and actually results in a set when the deduplication operation, do not need to re-query for the result set, which is equal to do the unnecessary operations, reduce the efficiency of SQL

2, do not try to use EXISTS IN, do not try to use the table associated with sub-queries.

IN IN using, for example when there are only a finite number of parameters determining ( '36', '23', '38'), and try not to over a hundred

IN EXISTS association table and, probably, it is associated with various forms of the table. With the IN and EXISTS such as SQL parser is told in the context you have to do this form of association table, then the SQL parser will not be able to play a role when tuning. That is the best implementation of the IN and EXISTS fact and the associated table when almost SQL parser to analyze the results of the general, not much better than it. As between IN and EXISTS do not explain the difference

SQL tuning based on slow query log positioning slow query execution according to plan

Before the slow SQL query must first define a reasonable threshold slow SQL queries, such as to define two seconds slower query returns the query is a query. When these first and then gradually slow down to clean up the query threshold, such as to 300ms, and then to 100ms, in stages, step by step to ease the tight

In the slow query SQL retrieved mainly look at several indicators
1, QUERY_TIME most intuitive query length

2, ROWS_EXAMINED retrieve the number of records where the condition or if the table join definition is not good, then the impact on the value will be relatively large

3, the number of ROWS_SEND result set records, if the query returns 1W bar, speed certainly better than the query returns a slower

After a slow SQL queries case will correspond to the appropriate SQL execution plan to print out to observe the implementation of the plan, targeted for adjustment

EXPLAIN select ............ obtain the corresponding implementation plan in this way

Implementation plan will include id, select_type, table, type, possible_keys, key, ken_len, ref, rows, extra columns, etc.

Id column wherein a set of numbers, which represent the order in which each unit to execute SQL statements in SQL this, when the numbers are not simultaneously a large number of the first unit are performed after a small number of units. When the number in the top row of the same number of cells before and after the execution of the execution below

For example, id returns

1,
2,
3,
3,
4,
5,
and that the order of execution is 5,4,3 (line 3), 3 (line 4), 2,1

SQL is individually observed throughout this order to perform the steps of

select_type columns represent each type of clause, it is more simple or more complex

simple is the easiest, no subqueries nor union, the most common condition where execution results

DESCRIPTION complex primary sub-portions, it will be marked as the outermost primary

subquery SQL contains a subquery (may be concerned about whether performance tuning)

I understand DERIVED derived as a result of a large section of SQL me to use it as a table such as
the SELECT from A, (the SELECT from b) c Here edge (select * from b) will trigger DERIVED,

There is the use of the UNION or UNION ALL UNION large SQL, performance tuning may be concerned about whether there are points

Range type column are all, index, range, ref, eq_ref, const, system null

all full table scan generally in need of tune complex SQL, all must be out of adjustment, if necessary, increase the corresponding query, to avoid such a full table scan

index is also a full table scan, but it is to traverse the entire index, instead of the entire table

range: scan range index, such deptno> 10 and deptno <30

ref: non-uniqueness of the index scan such as writing SQL select on emp emp table from the WHERE depnto = * '2'
deptno here is the only type of non-indexed columns

eq_ref: The only type of index columns such as writing SQL select on emp table * from emp where empno = '2'

const, system table only when there is such a line when prompted, to the extent generally does not require optimization

null: no access nor access the index table

By tuning the implementation plan is to look at the two main columns, the worst appears to minimize execution

In addition rows columns need to focus

But the great value of rows of columns executed quickly SQL, we tend to be a test but no pressure locally on the server becomes full of business has led to the root causes of

Local quickly because time is running this SQL locally due to hardware resources permit so fast. However, when a large number of servers on the same statement might be concurrent system IO, BUFFER and other resources caused contention, leading to a sharp decline in performance

extra column major concern if there are files sort using temporary or if the cue. That description of the SQL will do in a temporary table space for sorting, SQL query speed of a great impact, but also a lot of pressure on the temporary table space. If there are issues to consider increasing temporary table space, and see if you need to increase the index to sort or group by column

There are also adjusted based on DB parameter, after finishing

Guess you like

Origin blog.51cto.com/4890631/2484203