当前位置:网站首页>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
边栏推荐
- MySQL-01.工作中数据库优化和explain字段知识的总结
- NOI OJ 1.3 14:大象喝水 C语言
- Copilot免费时代结束!正式版67元/月,学生党和热门开源项目维护者可白嫖
- 漫画 | Code Review快把我逼疯了!
- Tencent tangdaosheng: practice "science and technology for the good" and promote sustainable social value innovation
- 个人博客系统毕业设计开题报告
- Personal blog system graduation project opening report
- Several practical software sharing
- Golang 快速上手 (1)
- 2021-04-16数组
猜你喜欢

Several practical software sharing

Dr. Sun Jian was commemorated at the CVPR conference. The best student thesis was awarded to Tongji Ali. Lifeifei won the huangxutao Memorial Award

Cloud native database Amazon RDS

JVM简单入门-02

Numerical calculation method

搭建一个QQ机器人叫女友起床

Pet Feeder Based on stm32

Successful experience of postgraduate entrance examination in materials and Chemical Engineering (metal) of Beijing University of Aeronautics and Astronautics in 2023

2021-04-15

Set up a QQ robot for ordering songs, and watch beautiful women
随机推荐
Distributed common interview questions
Nuxt.js spa与ssr的区别
oracle中遇到的bug
JVM简单入门-02
Tencent tangdaosheng: practice "science and technology for the good" and promote sustainable social value innovation
2021-05-07封装 继承 super this
Personal blog system graduation project opening report
Build a security video monitoring platform using Huawei cloud ECS server
2021-04-15
2021-04-16 array
线程池在项目中使用的心得体会
春招面试经验汇总(技术岗)
Is there anyone who plans to open source an industrial "seckill" system architecture?
RT thread add MSH command
On shore experience of Chang'an University majoring in transportation in 2023
炫酷相册代码,祝对象生日快乐!
验证码redis实践总结
NOI OJ 1.4 03:奇偶数判断 C语言
NOI OJ 1.3 17:计算三角形面积 C语言
2021-05-11 abstract class