title: MySQL substring/in array performance
body: <p>I am selecting rows from a MySQL table like this:</p>
  
  <pre><code>SELECT id, name, code FROM table
  </code></pre>
  
  <p>What I want to do now is to select rows where code starts with a sequence of characters specified in an array. What is best practice - a) to change the MySQL query and use SUBSTRING and IN, or b) use PHP to filter the rows later?</p>
  
  <p>a)</p>
  
  <pre><code>$qsql = mysql_query("SELECT id, name, code FROM table WHERE SUBSTRING(code, 1, 2) IN ('AB', 'BA', 'ZK', ...)");
  while($sql = mysql_fetch_assoc($qsql)) {
      // Do something
  }
  </code></pre>
  
  <p>or b)</p>
  
  <pre><code>$codes = array('AB', 'BA', 'ZK', ...);
  $qsql = mysql_query("SELECT id, name, code FROM table");
  while($sql = mysql_fetch_assoc($qsql)) {
      if(!in_array($sql['code'], $codes))
          continue;
      // Do something
  }
  </code></pre>
  
  <p>The code array will probably have ~30 elements.</p>