title: Is it faster to use php array count() than SQL row count?
body: <p>In a nutshell: Is it faster to use PHPs array count() on a number of arrays vs. using SQL row count multiple times?</p>
  
  <p>I'm having an issue with a slow query that I attribute to the COUNT(*) function. I will explain what I am doing and then what I'm anticipating <em>might</em> be significantly faster.</p>
  
  <p>Currently I'm looping a function that does a count of about 20,000 rows each iteration. It returns the number of rows for each month in a year:</p>
  
  <pre><code>// CREATE MONTHLY LINKS
  public function monthly_links() {
  
      $months = array('','January','February','March','April','May','June','July','August', 'September','October','November','December');
      for ($i=1 ; $i &lt;= 12 ; $i++) {
          $array[] = "&lt;a href='monthly.php?month=" . $i . "&amp;status=3'&gt;" . $months[$i] . " " . $this-&gt;chosen_year() . " (" . $this-&gt;number_positions_month_year($i, $this-&gt;chosen_year()) . ")&lt;/a&gt;";
      }
      return $array;
  
  }
  
  
  // SHOW NUMBER OF POSITIONS FOR EACH MONTH/YEAR
  public function number_positions_month_year($month, $year) {
  
      $sql = $this-&gt;client_positions_sql() . " AND MONTH(`exp_date`) = " . $month . " AND YEAR(`exp_date`) = " . $year;
      $res = $this-&gt;conn-&gt;query($sql);
      $count = $res-&gt;num_rows;
  
      return $count;
  }
  </code></pre>
  
  <p>The code is not that important in the example above because essentially what I am asking is: Is it faster to do the following...?</p>
  
  <ul>
  <li>Query the table once while dumping each months corresponding ids to an array (there will be 12 arrays)</li>
  <li>Using PHP count() on each array to get the number of entries for each month</li>
  </ul>