title: mysql UNION vs IN clause
body: <p>I have been trying to optimize one of my queries which looks like:</p>
  
  <pre><code>select toc.* from
      (select sender, max(convid) as maxconvid
       from MetaTable 
       where sender in ('arjunchow','aamir_alam')
       group by sender) as tmp1
      inner join MetaTable as toc on 
          toc.sender = tmp1.sender 
          and toc.convid = tmp1.maxconvid;
  </code></pre>
  
  <p>When the mysql server is under stress, this query normally responds within 0.2 sec, but when the number of "sender" ids in the IN clause increase (>50), the query slows down terribly (~5-6 sec). </p>
  
  <p>Is it advisable to use multiple union clauses instead of an IN clause given that my query may become a union of 50 queries. So my query will look like : </p>
  
  <pre><code>(SELECT 
      convId, 
      UNIX_TIMESTAMP(timeStamp) as timeStamp, 
      UNIX_TIMESTAMP(createTime) as createTime, 
      numMessages, 
      rootMsg, 
      sender, 
      ipormobile, 
      modIpOrMobile, 
      UNIX_TIMESTAMP(modTimeStamp) as modTimeStamp 
   from 
      MetaTable 
   where 
      sender='arjunchow' 
   ORDER BY convId DESC limit 1) 
  UNION ALL 
  (SELECT 
      convId, 
      UNIX_TIMESTAMP(timeStamp) as timeStamp, 
      UNIX_TIMESTAMP(createTime) as createTime, 
      numMessages, 
      rootMsg, 
      sender, 
      ipormobile, 
      modIpOrMobile, 
      UNIX_TIMESTAMP(modTimeStamp) as modTimeStamp 
   from 
      MetaTable 
   where 
      sender='aamir_alam' 
   ORDER BY convId DESC limit 1)
  </code></pre>