Sql语句优化
我所在的公司数据库是mysql, 表多数据多,经常mysql占用内存高出100%,开始使用了,show null processlist 来查找运行慢的sql,但是有时候好像没有发现非常慢和耗资源的sql,
然后我们通过设置 mysql的慢查询的日志文件。找到了几条sql.并找到了原因和优化:
EXPLAIN select ( SELECT GROUP_CONCAT(productsearchid, '##') FROM vtiger_servicecontracts WHERE sc_related_to = related_to LIMIT 2 ORDER BY servicecontractid DESC ) AS 'productlist', vtiger_servicecomments.assigntype, IFNULL((select vtiger_modcomments.addtime from vtiger_modcomments where vtiger_modcomments.modulename='Accounts' and vtiger_modcomments.moduleid=vtiger_servicecomments.related_to and vtiger_modcomments.creatorid=vtiger_servicecomments.serviceid ORDER BY vtiger_modcomments.addtime desc LIMIT 1),vtiger_servicecomments.addtime) as lastfollowtime, vtiger_servicecomments.allocatetime, vtiger_servicecomments.servicecommentsid, vtiger_servicecomments.salesorderproductsrelid, vtiger_servicecomments.related_to as accountid, (vtiger_account.accountname) as related_to, vtiger_servicecomments.addtime, (select leadsource from vtiger_account where vtiger_account.accountid=vtiger_servicecomments.related_to ) as leadsource, '--' as productid, vtiger_servicecomments.starttime, vtiger_servicecomments.endtime, '--' as serviceamount, IFNULL((select CONCAT(last_name,'[',IFNULL((select departmentname from vtiger_departments where departmentid = (select departmentid FROM vtiger_user2department where userid=vtiger_users.id LIMIT 1)),''),']',(if(`status`='Active','','[离职]'))) as last_name from vtiger_users where id=vtiger_servicecomments.serviceid),'--') as serviceid, (select last_name from vtiger_users where id=vtiger_servicecomments.assignerid) as assignerid, (select accountrank from vtiger_account where vtiger_account.accountid=vtiger_servicecomments.related_to) as accountrank, (select IFNULL((select CONCAT(last_name,'[',IFNULL((select departmentname from vtiger_departments where departmentid = (select departmentid FROM vtiger_user2department where userid=vtiger_users.id LIMIT 1)),''),']',(if(`status`='Active','','[离职]'))) as last_name from vtiger_users where vtiger_crmentity.smownerid=vtiger_users.id),'--') as smownerid from vtiger_crmentity where vtiger_crmentity.crmid=vtiger_servicecomments.related_to and vtiger_crmentity.deleted=0) as ownerid, (select departmentname from vtiger_departments where vtiger_departments.departmentid=(select departmentid from vtiger_user2department where vtiger_user2department.userid=(select vtiger_crmentity.smownerid from vtiger_crmentity where vtiger_crmentity.crmid=vtiger_servicecomments.related_to and vtiger_crmentity.deleted=0))) as departmentid, '--' as schedule, vtiger_servicecomments.nofollowday, vtiger_servicecomments.remark from vtiger_servicecomments LEFT join vtiger_account ON vtiger_account.accountid = vtiger_servicecomments.related_to LEFT JOIN vtiger_crmentity ON vtiger_crmentity.crmid = vtiger_servicecomments.related_to LEFT JOIN vtiger_servicecomments_returnplan ON vtiger_servicecomments_returnplan.commentsid = vtiger_servicecomments.servicecommentsid where 1=1 and vtiger_servicecomments.assigntype='accountby' GROUP BY vtiger_servicecomments.servicecommentsid order by vtiger_servicecomments.nofollowday LIMIT 0,20;
红色的部分:表示在关联查询时,mysql的进行了文件排序。并且取出的字段不全是第一个表的字段。这种排序是由于数量比较多mysql需要使用到磁盘排序,所以叫做文件排序。如果出现这样的标示,可以去掉排序看看sql是否快了很多。