此查询在我的系统中非常慢(都使用索引)
SELECT
`sd`.`group_id` AS `department_id`,
`sd`.`group_name` AS `department_name`,
`cltc`.`first_source_type`,
cl.`id`,sd.`group_id`
FROM
`crm_leads` cl
INNER JOIN `crm_lead_to_contracts` cltc ON cltc.`lead_id` = cl.`id`
INNER JOIN `crm_staff_roles` AS `csr` ON `cl`.`id` = `csr`.`lead_id` AND `csr`.`role` = 'SALES'
INNER JOIN `users` AS `u` ON `u`.`id` = `csr`.`user_id`
INNER JOIN `sale_departments` `sd` ON `u`.`department_id` = `sd`.`group_id`
WHERE
cl.created BETWEEN '2018-08-01 00:00:00' AND '2018-08-30 23:59:59'
AND `u`.`department_id` IN (1526,1608,1527,1627,1632,1661,1685,1725)
AND `sd`.`group_id` IN (1526,1527,1608,1627,1632,1661,1685,1725)
使用的索引:
</b><br><br><div align="left"><div><div class = "resultcaptionstyle"><b>EXPLAIN Result</b></div><br><table class = "tablestyle"><tbody><tr class = "captionfontstyle"><td class = "colcaptionstyleleft">id</td><td class = "colcaptionstyleleft">select_type</td><td class = "colcaptionstyleleft">table</td><td class = "colcaptionstyleleft">type</td><td class = "colcaptionstyleleft">possible_keys</td><td class = "colcaptionstyleleft">key</td><td class = "colcaptionstyleleft">key_len</td><td class = "colcaptionstyleleft">ref</td><td class = "colcaptionstyleleft">rows</td><td class = "colcaptionstyleleft">Extra</td></tr><tr class = "datafontstyleroweven"><td class = "cellstyleleft">1</td><td class = "cellstyleleft">SIMPLE</td><td class = "cellstyleleft">sd</td><td class = "cellstyleleft">range</td><td class = "cellstyleleft">IDX_sale_departments_group_id</td><td class = "cellstyleleft">IDX_sale_departments_group_id</td><td class = "cellstyleleft">4</td><td class = "cellstyleleft">(NULL)</td><td class = "cellstyleleft">8</td><td class = "cellstyleleft">Using index condition</td></tr><tr class = "datafontstylerowodd"><td class = "cellstyleleft">1</td><td class = "cellstyleleft">SIMPLE</td><td class = "cellstyleleft">u</td><td class = "cellstyleleft">ref</td><td class = "cellstyleleft">PRIMARY,DepartmentIdIndex</td><td class = "cellstyleleft">DepartmentIdIndex</td><td class = "cellstyleleft">9</td><td class = "cellstyleleft">dms.sd.group_id</td><td class = "cellstyleleft">4</td><td class = "cellstyleleft">Using where; Using index</td></tr><tr class = "datafontstyleroweven"><td class = "cellstyleleft">1</td><td class = "cellstyleleft">SIMPLE</td><td class = "cellstyleleft">csr</td><td class = "cellstyleleft">ref</td><td class = "cellstyleleft">unique_id,lead_id,user_id</td><td class = "cellstyleleft">user_id</td><td class = "cellstyleleft">4</td><td class = "cellstyleleft">dms.u.ID</td><td class = "cellstyleleft">510</td><td class = "cellstyleleft">Using index condition; Using where</td></tr><tr class = "datafontstylerowodd"><td class = "cellstyleleft">1</td><td class = "cellstyleleft">SIMPLE</td><td class = "cellstyleleft">cl</td><td class = "cellstyleleft">eq_ref</td><td class = "cellstyleleft">PRIMARY,created</td><td class = "cellstyleleft">PRIMARY</td><td class = "cellstyleleft">4</td><td class = "cellstyleleft">dms.csr.lead_id</td><td class = "cellstyleleft">1</td><td class = "cellstyleleft">Using where</td></tr><tr class = "datafontstyleroweven"><td class = "cellstyleleft">1</td><td class = "cellstyleleft">SIMPLE</td><td class = "cellstyleleft">cltc</td><td class = "cellstyleleft">ref</td><td class = "cellstyleleft">lead_id</td><td class = "cellstyleleft">lead_id</td><td class = "cellstyleleft">4</td><td class = "cellstyleleft">dms.csr.lead_id</td><td class = "cellstyleleft">1</td><td class = "cellstyleleft"></td></tr></tbody></table></div></div></div>
但是如果更改以下1行,速度会非常快:
SELECT
`sd`.`group_id` AS `department_id`,
`sd`.`group_name` AS `department_name`,
`cltc`.`first_source_type`,
cl.`id`,sd.`group_id`
FROM
`crm_leads` cl
INNER JOIN `crm_lead_to_contracts` cltc ON cltc.`lead_id` = cl.`id`
INNER JOIN `crm_staff_roles` AS `csr` ON `cl`.`id` = `csr`.`lead_id` AND `csr`.`role` = 'SALES'
INNER JOIN `users` AS `u` ON `u`.`id` = `csr`.`user_id`
INNER JOIN `sale_departments` `sd` ON `u`.`department_id` = `sd`.`group_id`
WHERE
cl.created BETWEEN '2018-08-01 00:00:00' AND '2018-08-30 23:59:59'
AND `u`.`department_id` IN (1526,1608,1527,1627,1632,1661,1685,1725)
不明白为什么? 谁能解释。 谢谢你。