当前位置:网站首页>什么是慢查询?如何优化?
什么是慢查询?如何优化?
2022-07-13 17:47:00 【Kuo-Teng】
什么是慢查询?如何优化?
1. 什么是慢查询?
慢查询,顾名思义,就是比较慢的查询,那什么是算是慢查询呢?不同的公司对于慢查询的定义不尽相同。
一般情况下,我们把查询时间超过1s的查询称为慢查询。
2. 优化SQL来减少慢查询
2.1. 索引优化
慢查询非常多的情况是由于SQL没有走适当的索引导致的。但是,即使加上索引,如果使用不正确,SQL语句在执行时依然不会走索引。哪些情况下SQL语句不走索引呢?
WHERE条件中的1=1,查询条件永远为真,可能导致WHERE条件失效,进行全表查询;- 使用函数或者隐式转化会导致不走索引;
LIKE匹配通配符号在前面的时候,不走索引;- 使用了否定条件;
OR其中一个有索引,另一个没有的情况;- 多列索引需要满足最左匹配原则;
- 两张表字符集不一样或者编码不一样,联表查询时;
IN的内容过多,会不走索引;
2.2 拆解分批
如果数据量比较大,则可以尝试拆解分批的方式来减少慢查询:
- 多层嵌套查询改为多次查询;
IN子查询影响查询性能,用JOIN方式代替;- 一次查询数量过于庞大,拆成多次查询、拼装。
- 用了反向查询(比如
NOT IN)或者IN语句参数集太多,可能会导致全表扫描,这种情况尽量拆分语句,或者内存中过滤解决; - 将⼤字段、访问频率低的字段拆分到单独的表中存储,分离冷热数据;
- 分库分表;
- 归档;
边栏推荐
- Buuctf webshell back door
- North tour project notes
- Intranet penetration notes - Sticky Keys and system command information collection
- sniffer Pro對ARP協議的分析、捕獲與模擬攻擊
- [wustctf2020] plain
- 内网渗透笔记——三层发现and四层发现
- PHP docking Alipay web payment -tp5.1 framework
- FTP upload file script description
- [Huang ah code] redis realizes fuzzy query and deletes | redis obtains the key according to the prefix
- Convert list data to tree data
猜你喜欢

Es6--string (string)

ES6 -- class

Intranet penetration notes --:) a smiling face

ES6 -- arrow function

Garbage collection mechanism
图形图像编程实践 课程报告
![[BJDCTF2020]Cookie is so stable](/img/d8/fb9fc2496ee8b23ec6fd6195051030.png)
[BJDCTF2020]Cookie is so stable

Network security emergency response - common tools

About the installation and use of visual studio 2022
![[NCTF2019]Fake XML cookbook](/img/d4/e5ff896108c86179526a06911abd38.png)
[NCTF2019]Fake XML cookbook
随机推荐
MySQL DQL conditional query / aggregate function / grouping query / sorting query / paging query
Intranet penetration notes - Sticky Keys and system command information collection
Log blacklist can really save you money!
NFTScan 开发者平台推出多链 NFT 数据 Pro API 服务
Intranet penetration notes - layer 2 Discovery
Operating system notes - Windows emergency response and security hardening
扶朕起来,朕还能接着编!
MSF利用永恒之蓝渗透win2003
[Vue] keep alive the simplest way to clear the cache
File upload parsing vulnerability
PHP对接微信支付--Native tp5框架
暑期沉淀web学习——php基础
内网渗透笔记——二层发现
40.js -- the same name identifier promotion problem
Dense Digital Economy: using passwords to "search for pearls in the deep sea"
Find a value of tree data
Summer precipitation Web Learning -- PHP Foundation
Buuctf webshell back door
如何解决数据流转「不可能三角」难题?
unity实验-重力撞墙