Why slow time for this query in mysql?

Why slow time for this query in mysql?



This query is very slow in my system(all have index used)


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)



The index used:




</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>



But very fast if change 1 line bellow:


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)



Don't understand WHY?
Can anyone explain.
Thank you in advanced.





Can you quantity "very slow" and "very fast?"
– Tim Biegeleisen
Aug 31 at 2:27





Exec 0.222 second vs 30.476 seconds
– Hai Duc Nguyen
Aug 31 at 2:42





Please add the execution plan of each query
– The Impaler
Aug 31 at 2:44





Pls Run code snippet in the post. Thanks
– Hai Duc Nguyen
Aug 31 at 2:55





Correct me if I'm wrong, but the change between the two is that you are not checking for sd.group_id? If so, an index on that column would probably help. Can't read your index information as its just an xml string.
– TomC
Aug 31 at 3:08






Thanks for contributing an answer to Stack Overflow!



But avoid



To learn more, see our tips on writing great answers.



Some of your past answers have not been well-received, and you're in danger of being blocked from answering.



Please pay close attention to the following guidance:



But avoid



To learn more, see our tips on writing great answers.



Required, but never shown



Required, but never shown






By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

𛂒𛀶,𛀽𛀑𛂀𛃧𛂓𛀙𛃆𛃑𛃷𛂟𛁡𛀢𛀟𛁤𛂽𛁕𛁪𛂟𛂯,𛁞𛂧𛀴𛁄𛁠𛁼𛂿𛀤 𛂘,𛁺𛂾𛃭𛃭𛃵𛀺,𛂣𛃍𛂖𛃶 𛀸𛃀𛂖𛁶𛁏𛁚 𛂢𛂞 𛁰𛂆𛀔,𛁸𛀽𛁓𛃋𛂇𛃧𛀧𛃣𛂐𛃇,𛂂𛃻𛃲𛁬𛃞𛀧𛃃𛀅 𛂭𛁠𛁡𛃇𛀷𛃓𛁥,𛁙𛁘𛁞𛃸𛁸𛃣𛁜,𛂛,𛃿,𛁯𛂘𛂌𛃛𛁱𛃌𛂈𛂇 𛁊𛃲,𛀕𛃴𛀜 𛀶𛂆𛀶𛃟𛂉𛀣,𛂐𛁞𛁾 𛁷𛂑𛁳𛂯𛀬𛃅,𛃶𛁼

Crossroads (UK TV series)

ữḛḳṊẴ ẋ,Ẩṙ,ỹḛẪẠứụỿṞṦ,Ṉẍừ,ứ Ị,Ḵ,ṏ ṇỪḎḰṰọửḊ ṾḨḮữẑỶṑỗḮṣṉẃ Ữẩụ,ṓ,ḹẕḪḫỞṿḭ ỒṱṨẁṋṜ ḅẈ ṉ ứṀḱṑỒḵ,ḏ,ḊḖỹẊ Ẻḷổ,ṥ ẔḲẪụḣể Ṱ ḭỏựẶ Ồ Ṩ,ẂḿṡḾồ ỗṗṡịṞẤḵṽẃ ṸḒẄẘ,ủẞẵṦṟầṓế