title: MySQL performance using IN predicate
body: <p>If I run the following queries each one returns quickly (0.01 sec) and gives me my desired result.</p>
  
  <p><code>SELECT tagId FROM tag WHERE name='programming'</code></p>
  
  <p><code>SELECT COUNT(DISTINCT workcode) FROM worktag WHERE tagId=123 OR tagId=124</code></p>
  
  <p>(assume the two tagId numbers were the results from the first query)</p>
  
  <p>I would like to combine these queries so I only have to run it once:</p>
  
  <p><code>SELECT COUNT(DISTINCT workcode) FROM worktag WHERE tagId IN (SELECT tagId FROM tag WHERE name='programming')</code></p>
  
  <p>However this query completes in about 1 min and 20 sec. I have indexes on <code>worktag.workcode</code>, <code>worktag.tagId</code>, <code>tag.tagId</code>, and <code>tag.name</code>.</p>
  
  <p>If I run <code>DESCRIBE</code> on the queries the first two use the indexes and the second one uses the index for the subquery (on the <code>tag</code> table) but doesn't use any indexes on the <code>worktag</code> table.</p>
  
  <p><strong>Does anyone know why this might be?</strong></p>
  
  <p><em>NOTE: the <code>worktag</code> table has over 18 million records in it.</em> </p>