MySQL optimize queries related

[Relevant] Query Optimization

1. How to locate the relevant slow queries:

   a. can turn slow query log can also be used to record the show profiles related queries to a temporary table reanalysis.  

   b.show processlist see if there is a lot of waiting for the process of the class.

   c. desc or explain detailed look at the use of a strip sql index, the impact of lines, etc..

 

2. How to optimize:

  a. query, use the index and reduce the number of lines scanned a lot of useless. And the investigation that is necessary, try not to use select *, develop a good use of the column, to avoid a lot of redundant data scans. As well as a large number of repeated queries, do some caching at the application layer.

 b. decompose complex queries become part of a small number of relevant, you can improve cache efficiency, reduce competition inquiry singled lock and so on.

 C. a particular type of optimization related far as possible without subqueries, associated with the split in place or, COUNT (*), with * will not scan row, only the scanning lines.

 

 

DETAILED Index Tuning]

1.insert when selecting multiple insertions bulk inserts.

Like 2.% at the beginning of the index can not be used.

3. Data type implicit conversion can not use the index. int turn this string

4. slower than a full table scan is not used if mysql index using an index, the number of scanning lines typically will not exceed 30% of the index. (Also to avoid frequent switching of the general index and the clustered index)

5. Large or long text fields do not build the index.

Table 6. Vertical, horizontal sub-table-related, this time can generally be considered artificially generate the primary key id, to maintain single order.

 

 

[Clustered index and other indexes]

1.Mysql primary key index and other indexes difference:

a. inherently unique primary key index, and can not have a null value, although the only unique index, but can be null.

B. primary key index belongs to a clustered index memory structure saved id and rows primary key index (B + leaf nodes of the tree leaf node is no child nodes of the node.), while the storage structure other indexed, stored is content, and the primary key values ​​of the index aggregation index, search through the index when the position of the recombination points of the primary key index data acquisition.

c. (again incidentally why the primary key general increment, because the increment, then will not have to change the structure of the nodes, just on the line in the last append, or you'll constantly adjust the physical address of the page of data, increasing io loss).

 

Guess you like

Origin www.cnblogs.com/camouflage/p/11111075.html