当前位置:网站首页>Mysql-03. Experience of SQL optimization in work

Mysql-03. Experience of SQL optimization in work

2022-06-23 10:24:00 cfcoolya

1. Use explain analysis sql

explain+sql You can start to analyze sql La , Here are some important fields

1.1 type
  • const It's found by index once ( best )
  • eq_ref Is used for unique indexing (primary key perhaps unique index)
  • ref Indicates the join matching condition of the above table , That is, which columns or constants are used to find values on index columns
  • range Range query in single table index
  • index Than all Better
  • all Full table scan ( The worst )

SQL Objectives of performance optimization : At the very least range Level , The requirement is ref Level , If it could be consts best .

1.2 possible_keys

Possible indexes , Note that... May not be used . When this column is NULL when , It is necessary to consider the current SQL Whether to optimize

1.3 extra
  • Using index Use overlay index
  • Using where Used where Clause to filter the result set
  • Using filesort Use file sorting , When sorting with non indexed columns , Very performance intensive , Try to optimize

2. How to deal with large paging ?

In Alibaba 《Java Development Manual 》 in , The solution to oversized paging

  • First, quickly locate what needs to be acquired id paragraph , And then relate
  • SELECT a.* FROM surface 1 a, (select id from surface 1 where Conditions LIMIT 100000,20 ) b where a.id=b.id
scene :
  • Be similar to select * from table where age > 20 limit 1000000,10, This statement requires load1000000 The data is then basically discarded , Take only 10 Of course it's slow .

  • We can also change it to select * from table where id in (select id from table where age > 20 limit 1000000,10). Although this is also load A million dollars of data , But because of index coverage , All fields to query are in the index , So it's going to be fast .

  • just so so select * from table where id > 1000000 limit 10, The efficiency is also good

3. When the amount of data is large , The primary key uses auto increment ID, Don't use UUID.

4. Store the password hash of the user char Not with varchar, Reduce space and improve retrieval efficiency .

5. Optimize where Clause

Optimize the query , Try to avoid full scan , The first thing to consider is where And order by Index the columns involved , The following is an example that causes the engine to abandon the use of indexes and perform a full table scan .

  • Try to avoid where Field in the clause null Value judgement
select id from t where num is null
--  Can be in num Set the default value on 0, Make sure the table num No column null value , And then query like this :
select id from t where num=
  • Try to avoid where Used in clauses != or <> The operator
  • Try to avoid where Used in clauses or To join the conditions
select id from t where num=10 or num=20
--  You can query like this :
select id from t where num=10 union all select id from t where num=20
  • in and not in Be careful with , Otherwise, it will result in a full table scan
select id from t where num in(1,2,3) 
--  For continuous values , It works  between  Don't use  in  了 :
select id from t where num between 1 and 3
  • Try to avoid where The clause performs an expression operation on the field
select id from t where num/2=100
--  Should be changed to :
select id from t where num=100*2
  • Should try to avoid in where Clause to function fields
select id from t where substring(name,1,3)=’abc’
-- name With abc At the beginning id Should be changed to :
select id from t where name like ‘abc%

Here we recommend a blog with a large number of examples to avoid index invalidation

  • https://www.cnblogs.com/cyhbyw/p/8898977.html
原网站

版权声明
本文为[cfcoolya]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/174/202206231006570873.html