结果中有 ORDER BY 列时避免文件排序

Avoid filesort when having ORDER BY column in result

对于医疗日历,我有以下查询:

SELECT
   `events`.`StartDate`,
   `events`.`ID`,
   `events`.`EndDate`,
   `events`.`Title`,
   `insurances`.`Title`,
   `personsPatients`.`PrimaryPhone`,
   `personsDocs`.`Name`

FROM `events`

LEFT JOIN `events++persons` ON `events`.`ID` = `events++persons`.`FirstEntityID` AND `events++persons`.`Type` = 'Patient'
LEFT JOIN `persons` AS `personsPatients` ON `events++persons`.`SecondEntityID` = `personsPatients`.`ID`
LEFT JOIN `insurances` ON `persons`.`Provider` = `insurances`.`ID`
LEFT JOIN `events++persons` AS `events++persons1` ON `events`.`ID` = `events++persons1`.`FirstEntityID` AND `events++persons1`.`Type` = 'Doctor'
LEFT JOIN `persons` AS `personsDoctors` ON `events++persons`.`SecondEntityID` = `personsDoctors`.`ID`
LEFT JOIN `companies++events` ON `events`.`ID` = `companies++events`.`SecondEntityID` 

WHERE
   ((`events`.`Type` = 'Annotation' and `companies++events`.`FirstEntityID` IS NULL) or 
    (`events`.`Type` = 'Annotation' and `companies++events`.`FirstEntityID` = 1) or 
    (`events`.`Type` = 'Consultation' and `companies++events`.`FirstEntityID` = 1)) and 

    `events`.`StartDate` >= '2015-03-02 00:00:00' AND 
    `events`.`StartDate` <= '2015-03-07 23:59:59'

ORDER BY `events`.`StartDate` ASC

事件通过 events++persons 链接到两个人:医生和病人。

这个查询非常慢。

当我删除 ORDER BY(这是必不可少的)时,文件排序和临时文件 table 消失了。非常感谢任何帮助!

问题在于 companies++events - 世界上所有的一对一 LEFT JOIN 当标准只出现在最后时都无济于事。我设法将该信息移动到主要实体(有点像指针)并创建覆盖索引,现在查询快如闪电。