Query 1:
select distinct email from mybigtable where account_id=345
takes 0.1s
Query 2:
Select count(*) as total from mybigtable where account_id=123 and email IN (<include all from above result>)
takes 0.2s
Query 3:
Select count(*) as total from mybigtable where account_id=123 and email IN (select distinct email from mybigtable where account_id=345)
takes 22 minutes and 90% its in the “preparing” state. Why does this take so much time.
Table is innodb with 3.2mil rows on MySQL 5.0
After much deliberation and with help from dba.se, this is the final query that worked majic.