Last active
August 29, 2015 14:15
-
-
Save waleedsamy/b6f289be47627c776d56 to your computer and use it in GitHub Desktop.
MySql subquery performance enhancment
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
-- enviroment : Mysql 5.5 | |
-- worst peerformance frist | |
--explain query indicate it not depend on any index on partent query ( so it will full scan on table with 11M ) | |
SELECT co_id FROM argo.v_r_p_master WHERE mn_code | |
IN ( | |
SELECT pk_data FROM argo.s_r_d_l WHERE insert_date > (NOW() - INTERVAL 1 DAY) | |
AND table_name IN ('argo.mn_code') AND pk_data IS NOT NULL | |
); | |
-- CONVERTED TO NEXT QUERY | |
-- best performance | |
-- explain query indicate it not depend on any index on partent query | |
SELECT co_id | |
FROM ar.p_master pm | |
JOIN ar.s_r_d_l srdl | |
ON ((pm.mn_code = srdl.pk_data)) | |
WHERE srdl.table_name IN ('ar.m_master', | |
'ar.m_master_new') | |
AND insert_date > (NOW() - INTERVAL 1 DAY) | |
AND srdl.pk_data IS NOT NULL; | |
-- found link that take about this issue http://bugs.mysql.com/bug.php?id=9021 | |
-- some enhancment in mysql 5.6 approve subqueries but i am not give it a try |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment