optimization operations common mysql

1. explain to view SQL execution plan

Before the query plus explain to view the SQL execution plan, in which five of the more important:

  • type column, the type of connection. At least one good sql statement to achieve the range level.
  • Prevent the emergence of all-level key columns, use the name of the index.
  • If you do not select an index, the value is NULL. It may take enforcement indexing key_len column,
  • Rows column index length, the number of scanning lines.
  • This value is an estimate extra columns detail. Note that common values ​​are not very friendly: Using filesort, Using temporary

IN 2.SQL statement contains a value should not be too much

For MySQL made the corresponding optimized IN, IN upcoming constants in all stored in an array inside, and the array is sorted. However, if the value is more, consume generated is relatively large. E.g:

select id from t where num in(1,2,3) 

For continuous values, you can not use in the between; used to replace or re-connected.

3.SELECT statement must identify the name of the field

SELECT * increase the number of unnecessary consumption (cpu, io, memory, network bandwidth); increases the possibility of using a covering index; and when the table structure changes, need to be updated before breaking

4. When the data requires only one time, the use limit 1

5. avoid a null value fields in the where clause determines

For null judgment will cause the engine to give up using the index and full table scan, this cliché, and I remember just started writing null judgment was ridiculed his colleagues, ha ha! ! ! And null values ​​in the table do not appear, or other place with 0, because a null value will cause failure of the entire field index

6% is not recommended prefix fuzzy queries

For example LIKE "% name" or LIKE "% name%", such a query can lead to failure while the index for full table scan. But you can use LIKE "name%".
How that query% name%? Can be considered global index

7. For the joint index, the most left-prefix to comply with the law

For column contains fields for index id, name, school, id field can be directly used, may be id, name in this order, but the name; school can not use this index. Therefore, when creating the joint index must pay attention to the order of the index fields, commonly used in the query field on the front

8. make use of inner join, avoid left join

Table participation in joint inquiry of at least two tables, there are generally the size of the points. If the connection is inner join, MySQL will automatically choose a small table as the driving table in the absence of other filters situation (small table-driven large table principle), but left join followed in the choice of driving table is the principle of the left driving on the right that left join table called left drive table.

9. The index table is driven on a field as a field limitation

10. Note that the scope of the query

For the joint index, if there is range queries, such as between,>, <when ​​other conditions, can cause failure of the back of the index field.

11. efficient use reasonable methods to increase pagination pagination

select id,name from product limit 1000001, 10
When using the sql statement to do pagination, some people may find that, with the increasing amount of table data, use direct query page limit will become slower. Optimization method is as follows: id may be taken before a maximum number of rows, and the next start point is limited based on this maximum id. So the column than the largest previous id is 1,000,000. sql can be written as follows:select id,name from product where id> 1000000 limit 10

12. If no constraint index in other fields, or minimize the use of

or both sides of the field, if there is not a field index, while other conditions are not indexed field, the situation will cause the query does not go indexes. Many times using union all (try to use the union all instead of the union, union and union all major difference is that the former requires the collection of the results and then uniqueness of the filtering operation, which will involve sorting, increase the number of CPU operations, increase resource consumption and delay. of course, with the proviso that two union all result set not duplicate data) or a union (when necessary) can be substituted for the "or" get better results

13. If you do not use the index sort field, as little as possible to sort

Published 169 original articles · won praise 224 · views 260 000 +

Guess you like

Origin blog.csdn.net/sureSand/article/details/86555174
Recommended