Skip to content

Instantly share code, notes, and snippets.

@waleedsamy
Last active August 29, 2015 14:15
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save waleedsamy/b6f289be47627c776d56 to your computer and use it in GitHub Desktop.
Save waleedsamy/b6f289be47627c776d56 to your computer and use it in GitHub Desktop.
MySql subquery performance enhancment
-- 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