Commonly used 19 mysql optimization

Link Source: https://zhuanlan.zhihu.com/p/49888088

A, EXPLAIN
do MySQL optimization, we want to make good use of EXPLAIN view SQL execution plan.
Let's a simple example, labeling (1,2,3,4,5) we want to focus on data


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. Indexing can take enforcement
key_len column index length
rows columns, 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
Second, the value of the IN SQL statement should not contain too many
MySQL for IN made the corresponding optimized, coming IN the constants are all stored in an array inside, and this array is sorted. However, if the value is more, consume generated is relatively large. Another example: select id from t where num in (1,2,3) for successive values, can not use in the between; used to replace or re-connected.
Three, SELECT statement is sure to specify the name of the field
SELECT * add a lot of unnecessary consumption (cpu, io, memory, network bandwidth); increases the likelihood of the use of covering indexes; when the table structure changes, also need to be updated before the break. So I requested directly connected to the back of the field names in the select.
Fourth, when you need only piece of data, use limit 1
This is to achieve the type column in EXPLAIN const type
Fifth, if the field is not used to sort the index, as little as possible to sort
Sixth, if restrictions in other fields are not indexed, as far as possible less or
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. In many cases the use of a union or union all (when necessary) way to replace "or" will get better results
Seven, try to use union all instead of union
union and union all differences are mainly 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 latency. Of course, with the proviso that two union all the result data set is not repeated.
Eight, the RAND BY without using the ORDER ()
SELECT from `dynamic` ID Order by RAND () limit 1000;
above sql statement, can be optimized to
select id from` dynamic` t1 join ( select rand () * (select max (id ) from `dynamic`) as nid) t2 on t1.id> t2.nidlimit 1000;
nine, and in distinction exists, not in EXISTS and Not
SELECT * from table A where id in (select id from table B)
above sql statement corresponds to
select * from table a where exists (select * from table B where table B.id = table A.id)
distinguish between in and exists mainly caused a change in the drive sequence (which is the key property change), if exists then layer other than table-driven table is accessed first, if it is IN, it runs the subquery. Therefore IN adapted to large and small outer inner case; EXISTS adapted to the outer case of the small and large table.
About not in and not exists, we recommended not exists, not only efficiency, not in logic there may be a problem. How to write a highly efficient alternative not exists sql statement?
Original sql statement
select colname ... from A table where a.id not in (select b.id from B tables)
efficiently sql statement
select colname ... from A Table Left join B table on where a.id = b.id where b. id is null
removed showing the result set as shown below, a table is not the data in table B


Ten, the use of reasonable methods to increase the efficiency of paging paging
select id, name from product limit 866613 , 20
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 getting slower and 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 866612. sql can be written as follows:
SELECT ID, Product name WHERE ID from> 866 612 limit 20 is
eleven, segmented query
the user selects a number of pages, some users may select a time range is too large, resulting in slow query. The main reason is the excessive number of scanning lines. This time can be programmed, segmentation query, loop through the results consolidation process on display.
The figure below sql statement, more than the number of scanning lines to one million when you can use segmentation query


XII avoid null value judgment on the fields in the where clause
to null the judge will cause the engine to give up using the index and full table scan.
Java Architecture / Distributed / high concurrency: 468,897,908
thirteen, does not recommend the use of the prefix% fuzzy queries
such as 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%?
As shown below, although the index is added to the secret field, but did not explain the results in fruit


那么如何解决这个问题呢,答案:使用全文索引
在我们查询中经常会用到select id,fnum,fdst from dynamic_201606 where user_name like '%zhangsan%'; 。这样的语句,普通索引是无法满足查询需求的。庆幸的是在MySQL中,有全文索引来帮助我们。
创建全文索引的sql语法是:
ALTER TABLE `dynamic_201606` ADD FULLTEXT INDEX `idx_user_name` (`user_name`);
使用全文索引的sql语句是:
select id,fnum,fdst from dynamic_201606 where match(user_name) against('zhangsan' in boolean mode);
注意:在需要创建全文索引之前,请联系DBA确定能否创建。同时需要注意的是查询语句的写法与普通索引的区别
十四、避免在where子句中对字段进行表达式操作
比如
select user_id,user_project from user_base where age*2=36;
中对字段就行了算术运算,这会造成引擎放弃使用索引,建议改成
select user_id,user_project from user_base where age=36/2;
十五、避免隐式类型转换
where 子句中出现 column 字段的类型和传入的参数类型不一致的时候发生的类型转换,建议先确定where中的参数类型
十六、对于联合索引来说,要遵守最左前缀法则
举列来说索引含有字段id,name,school,可以直接用id字段,也可以id,name这样的顺序,但是name;school都无法使用这个索引。所以在创建联合索引的时候一定要注意索引字段顺序,常用的查询字段放在最前面
十七、必要时可以使用force index来强制查询走某个索引
有的时候MySQL优化器采取它认为合适的索引来检索sql语句,但是可能它所采用的索引并不是我们想要的。这时就可以采用forceindex来强制优化器使用我们制定的索引。
十八、注意范围查询语句
对于联合索引来说,如果存在范围查询,比如between,>,<等条件时,会造成后面的索引字段失效。
十九、关于JOIN优化


LEFT JOIN A表为驱动表
INNER JOIN MySQL会自动找出那个数据少的表作用驱动表
RIGHT JOIN B表为驱动表
注意:MySQL中没有full join,可以用以下方式来解决
select * from A left join B on B.name = A.namewhere B.name is nullunion allselect * from B;
尽量使用inner join,避免left join
参与联合查询的表至少为2张表,一般都存在大小之分。如果连接方式是inner join,在没有其他过滤条件的情况下MySQL会自动选择小表作为驱动表,但是left join在驱动表的选择上遵循的是左边驱动右边的原则,即left join左边的表名为驱动表。
合理利用索引
被驱动表的索引字段作为on的限制字段。
利用小表去驱动大表


从原理图能够直观的看出如果能够减少驱动表的话,减少嵌套循环中的循环次数,以减少 IO总量及CPU运算的次数。
巧用STRAIGHT_JOIN
inner join是由mysql选择驱动表,但是有些特殊情况需要选择另个表作为驱动表,比如有group by、order by等「Using filesort」、「Using temporary」时。STRAIGHT_JOIN来强制连接顺序,在STRAIGHT_JOIN左边的表名就是驱动表,右边则是被驱动表。在使用STRAIGHT_JOIN有个前提条件是该查询是内连接,也就是inner join。其他链接不推荐使用STRAIGHT_JOIN,否则可能造成查询结果不准确。


这个方式有时可能减少3倍的时间。

Guess you like

Origin www.cnblogs.com/itzlg/p/10981421.html