SQL: 通过getlastSQL 获取到SQL
SELECT a.id as aid,`a`.`username`,`a`.`phone`,a.college as a_college,a.school as a_school,a.major as a_major,
`a`.`area_id`,`a`.`teach_center_id`,`a`.`class_id`,a.teacher_id as ateacher_id,`a`.`year`,`a`.`product_type_id`,
`a`.`choose_status`,`a`.`equip_status`,`b`.*,`c`.`special_change_num`,`c`.`apply_status`,c.operator as c_operator,
c.create_time as c_create_time,c.id as cid,`c`.`applicant`,`c`.`special_teacher_id`,`c`.`apply_area_id`,`c`.`out_money`,
c.update_time as c_update_time,c.check_remark as c_check_remark,s.username as sp_name,s.area_id as sp_area_id,
m.name as follow_teacher,`n`.`equip_area_id`,`n`.`equip_manager`,n.operator as mark_operator,`n`.`operator_area`,
`n`.`assign_type`,n.remark as mark_remark,`n`.`equip_area_ids_list`,`n`.`is_all_area`,`n`.`sign_service`,
`n`.`sign_service_hours`,n.create_time as mark_create_time
FROM `choose_school` `b`
LEFT JOIN `student` `a` ON `a`.`crm_student_id`=`b`.`crm_student_id`
LEFT JOIN `equip_new` `c` ON `b`.`student_id`=c.student_id and a.equip_status = c.apply_status
LEFT JOIN `special_teacher_new` `s` ON `s`.`id`=`c`.`special_teacher_id`
LEFT JOIN `special_manager` `m` ON `b`.`follow_teacher`=`m`.`id`
LEFT JOIN `no_sp_record` `n` ON `n`.`crm_id`=`b`.`crm_student_id`
WHERE `b`.`status` = '1' AND `a`.`status` = '1' AND `a`.`service_status` = '1'
AND `a`.`choose_status` = '1' AND ( `a`.`area_id` in (1,36) or FIND_IN_SET(1,n.equip_area_ids_list) or n.is_all_area=1 )
AND ( `a`.`area_id` in (1,36) or FIND_IN_SET(36,n.equip_area_ids_list) or n.is_all_area=1 )
GROUP BY `b`.`crm_student_id` ORDER BY `a`.`equip_status` ASC,`b`.`write_time` DESC,`a`.`update_time` DESC
LIMIT 0,10
6个表联查数据,条件异常复杂;
主要负责点在于 where 条件的组装和拼接;不同的where 逻辑 ,and 或 or ;
因为执行速度极慢,两条数据需要大概120秒;
Explain 分析发现 两个大表均 是全表扫描;未走索引;
一个一个字段排查,发现遗漏索引的列;添加后:
速度明显提升;
分析原因:随着系统越来越 庞大后,表设计字段也越来越多, 维护起来可能就比较麻烦,系统响应速度慢,就需要具体问题,一步一步排查;
技巧:可以试着一步一步的先去掉 leftjoin 去掉where 去掉排序,一个个的添加和排查,这样更方便排查问题所在;
文章来源:刘俊涛的博客 欢迎关注公众号、留言、评论,一起学习。
__________________________________________________________________________________
若有帮助到您,欢迎点击推荐,您的支持是对我坚持最好的肯定(*^_^*)