当前位置:网站首页>慢 SQL 问题经验总结
慢 SQL 问题经验总结
2022-07-21 15:53:00 【全栈程序员站长】
大家好,又见面了,我是你们的朋友全栈君。
1、 导致慢 SQL 的原因
在遇到慢 SQL 情况时,不能简单的把原因归结为 SQL 编写问题(虽然这是最常见的因素),实际上导致慢 SQL 有很多因素,甚至包括硬件和 mysql 本身的 bug。根据出现的概率从大到小,罗列如下:
- SQL编写问题
- 锁
- 业务实例相互干绕对 IO/CPU 资源争用
- 服务器硬件
- MYSQL BUG
2、 由 SQL 编写导致的慢 SQL 优化
针对SQL编写导致的慢 SQL,优化起来还是相对比较方便的。正如上一节提到的正确的使用索引能加快查询速度,那么我们在编写 SQL 时就需要注意与索引相关的规则:
- 字段类型转换导致不用索引,如字符串类型的不用引号,数字类型的用引号等,这有可能会用不到索引导致全表扫描;
- mysql 不支持函数转换,所以字段前面不能加函数,否则这将用不到索引;
- 不要在字段前面加减运算;
- 字符串比较长的可以考虑索引一部份减少索引文件大小,提高写入效率;
- like % 在前面用不到索引;
- 根据联合索引的第二个及以后的字段单独查询用不到索引;
- 不要使用 select *;
- 排序请尽量使用升序 ;
- or 的查询尽量用 union 代替 (Innodb);
- 复合索引高选择性的字段排在前面;
- order by / group by 字段包括在索引当中减少排序,效率会更高。
除了上述索引使用规则外,SQL 编写时还需要特别注意一下几点:
- 尽量规避大事务的 SQL,大事务的 SQL 会影响数据库的并发性能及主从同步;
- 分页语句 limit 的问题;
- 删除表所有记录请用 truncate,不要用 delete;
- 不让 mysql 干多余的事情,如计算;
- 输写 SQL 带字段,以防止后面表变更带来的问题,性能也是比较优的 ( 涉及到数据字典解析,请自行查询资料);
- 在 Innodb上用 select count(*),因为 Innodb 会存储统计信息;
- 慎用 Oder by rand()。
3、分析诊断工具
在日常开发工作中,我们可以做一些工作达到预防慢 SQL 问题,比如在上线前预先用诊断工具对 SQL 进行分析。常用的工具有:
- mysqldumpslow
- mysql profile
- mysql explain
具体使用及分析方法在此就不赘述,网上有丰富的资源可以参考。
4、误操作、程序 bug 时怎么办,解决方案
提出这个问题显然主要是针对刚开始工作的年轻同行们……实际上误操作和程序 bug 导致数据误删或者混乱的问题并非少见,但是刚入行的开发工作者会比较紧张。一个成熟的企业往往会有完善的数据管理规范和较丰富的数据恢复方案(初创公司除外),会进行数据备份和数据容灾。
当你发现误操作或程序 bug 导致线上数据被误删或误改动时,一定不能慌乱,应及时与 DBA 联系,第一时间进行数据恢复(严重时直接停止服务),尽可能减少影响和损失。对于重要数据(如资金)的操作,在开发时一定要反复进行测试,确保没有问题后再上线。
发布者:全栈程序员栈长,转载请注明出处:https://javaforall.cn/124757.html原文链接:https://javaforall.cn
边栏推荐
- Keras' deep learning practice -- gender classification based on vgg19 model
- Dynamics CRM: 使用setFilterXml来过滤需要在表单的Subgrid控件中进行展示的记录
- Dynamics crm: how to use query string parameters
- Azure Key Vault(1)介绍
- Error 1053: The service did not respond to the start or control request in a timely fashion.问题排查
- Scala函数及其高阶应用
- Dynamics crm: use setfilterxml to filter the records that need to be displayed in the subgrid control of the form
- Dynamics 365: 如何在Azure AD中创建用户并添加到Dynamics 365 Online中
- Application between azure service bus and dynamics 365 service endpoint
- Special analysis of China's third-party payment market in 2022
猜你喜欢
《通信软件开发与应用》课程结业报告
Dynamics CRM: 关系Relationships(2) - N:N 多对多
Detailed explanation of redis [data operation, persistence, jedis, cache processing] of NoSQL database
Azure Service Bus与Dynamics 365 Service Endpoint的之间的应用
Let me show you eight fallacies in software design
dedecms编辑器支持Word图片自动粘贴
Dynamics 365: how to create users in azure AD and add them to dynamics 365 Online
加密崩盘,Web3已死?
電腦上如何開啟多個微信,微信多開
Dynamics CRM: several situations in which the display fields in a form become read-only
随机推荐
Dynamics 365: how to remove the restriction that an entity can only create two access team templates
Dynamics CRM: Form中的Non-Event Dependencies的作用
Keras deep learning practice (10) -- transfer learning
Spirng之注解使用
Keras' deep learning practice -- gender classification based on inception V3
How to use document tools for API management?
数据可视化图表之面积折线图
Special analysis of China's third-party payment market in 2022
Application between azure service bus and dynamics 365 service endpoint
Dynamics CRM: 关系Relationships(1) - 1:N, N:1
How to write SQL statements?
公网官网建站笔记(五):域名工信部备案完整流程并解析公网访问
Dynamics 365: 查找哪些记录使用Access Team这种方式分享给了某一个User
Dynamics CRM:表单中移除一个控件时,提示“The field you are trying to remove is required by the system or business“
Desai wisdom number - other charts (Sangji chart): summer vacation consumption
#yyds干货盘点# 解决名企真题:小A最多会新认识的多少人
感觉的定义
Oracle about date field index usage test
Dynamics CRM: the entity set in dashboard cannot be displayed
加密崩盘,Web3已死?