网站首页 > 教程分享 正文
概述
新项目业务人员反馈说最近订单发放模块经常很卡,导致总是有锁的情况发生,在用慢查询和开启锁监控观察后发现实际上只是单条查询慢造成的阻塞锁,这里考虑先对单条查询做一下优化。
1、优化前的表结构、数据量、SQL、执行计划、执行时间
1.1、表结构
A表有90个字段,B表有140个字段。
1.2、数据量
select count(*) from A; --166713 select count(*) from B; --220810
1.3、sql
开启慢查询观察到慢sql如下,单条执行只取200条记录是在23秒左右。
select ob.id, ob.customer, ob.order_no1, ob.accountingitems_code, ob.insert_date, ob.weight, ob.volume, ob.qty, ob.project_code,ob.order_no2,ob.order_type1 from A as ob where ifnull(ob.project_code,'')<>'' and ifnull(ob.accountingitems_code,'')<>'' and ob.if_cost_proof='N' and EXISTS (select 1 from B ol where ob.id=ol.order_base) limit 200;
1.4、执行计划
思路
这两张表都是订单表,全国各地的每天大概会产生十万行左右,这里又是全扫,等后期达到上千万的数据就GG了。目前只是看到这个sql上的问题,先考虑exists部分做一下改写。
2、exists部分改写
select ob.id, ob.customer, ob.order_no1, ob.accountingitems_code, ob.insert_date, ob.weight, ob.volume, ob.qty, ob.project_code,ob.order_no2,ob.order_type1 from fsl_order_base as ob,fsl_order_base_line ol where ob.id=ol.order_base and ob.if_cost_proof='N' and ifnull(ob.project_code,'')<>'' and ifnull(ob.accountingitems_code,'')<>'' limit 200;
执行时间:耗时1.8秒
对应的执行计划:
可以看到ob表走了主键索引
业务确认结果符合需求,那就在这基础上建一下索引吧!
3、ol表建索引
create index idx_obl_id on fsl_order_base_line(order_base); create index idx_ob_id_cost on fsl_order_base(id,if_cost_proof); --加上去但实际上用不到这个索引,选择去掉
4、查看执行时间和执行计划
耗时1.1秒,可惜执行计划还是走了全扫,在对ob表建了索引实际上也用不到,最终只在ol表建了索引。
5、考虑用join改写
把ob结果集缩小,然后再做关联查,并测试是否可以用上索引。
SELECT obc.id, obc.customer, obc.order_no1, obc.accountingitems_code, obc.insert_date, obc.weight, obc.volume, obc.qty, obc.project_code, obc.order_no2, obc.order_type1 FROM (select * from fsl_order_base AS ob where ob.if_cost_proof = 'N' and ifnull( ob.project_code, '' ) <> '' and ifnull( ob.accountingitems_code, '' ) <> '' ) obc join fsl_order_base_line ol on obc.id = ol.order_base limit 200;
时间快了一点,但不是很明显,先凑合吧
执行计划保持不变。
总结
建索引前因为走了主键索引,所以时间在1.6秒这样,建索引后不走主键索引了,走ol表的索引,所以在1.5秒,然后缩小结果集去查的话就在1s这样。
更重要的是这两个表一个90个字段,一个150个字段,所以这两个表的关联查后期结果集应该还是会很大,建议是弄成分区表的形式,表能拆分的话是最好的。这些长度不要直接给那么大,这么宽对性能都是有影响的。
觉得有用的朋友多帮忙转发哦!后面会分享更多devops和DBA方面的内容,感兴趣的朋友可以关注下~
猜你喜欢
- 2024-09-09 SQL Server优化50法(sql server 优化)
- 2024-09-09 SQLServer-高级篇(sqlserver ag)
- 2024-09-09 2022-12-17:订单最多的客户。以下数据,结果输出3。请问sql语句
- 2024-09-09 springboot整合mybatis使用xml实现sql语句的查询配置
- 2024-09-09 Qt的数据库(Driver类、Query类、Model类、View类)
- 2024-09-09 VBA+ADO+SQL语句,小试牛刀。(vba的sql)
- 2024-09-09 MS SQL Server——SQL语句导入导出大全
- 2024-09-09 mysql根据条件执行sql(mysql根据条件查询)
- 2024-09-09 MyBatis3-动态SQL语句(navicat怎么写sql语句)
- 2024-09-09 SQL优化——IN和EXISTS谁的效率更高
你 发表评论:
欢迎- 最近发表
- 标签列表
-
- css导航条 (66)
- sqlinsert (63)
- js提交表单 (60)
- param (62)
- parentelement (65)
- jquery分享 (62)
- check约束 (64)
- curl_init (68)
- sql if语句 (69)
- import (66)
- chmod文件夹 (71)
- clearinterval (71)
- pythonrange (62)
- 数组长度 (61)
- javafx (59)
- 全局消息钩子 (64)
- sort排序 (62)
- jdbc (69)
- php网页源码 (59)
- assert h (69)
- httpclientjar (60)
- postgresql conf (59)
- winform开发 (59)
- mysql数字类型 (71)
- drawimage (61)
本文暂时没有评论,来添加一个吧(●'◡'●)