title: Performance implications filter/order/limit array in php (for+if) vs SQL clauses
body: <p>I'm working with <a href="http://wordpress.org/" rel="nofollow">wordpress</a>, and it has a nice <a href="http://codex.wordpress.org/Class_Reference/WP_Query" rel="nofollow">database</a> <a href="http://codex.wordpress.org/Function_Reference/query_posts" rel="nofollow">interface</a>, it is very useful to do a lot of somewhat simple queries.</p>
  
  <p>I wanted to use this interface but it doesn't allow me to do some special filtering. Then I thought I could return a resultset from the database, and then make a filter using a php for with a nested if, taking out the results I did not want.</p>
  
  <p>I was thinking on doing the same for ordering.</p>
  
  <p>I know that if I have indices (in PHP I can't have them for arrays, but in mysql I have) operations like filter (WHERE) and ordering are super fast, comparing to sequential. </p>
  
  <p>I was thinking on the performance that the php_mysql adapter has, I don't know if it uses something like lazy loading data, cursors, and the memory implications in passing data from the database to php.</p>
  
  <p>Another use is in limiting/counting, in the common case of generating pagination links, where one needs the limited data, but also the total count. There are the two options to do that:</p>
  
  <ul>
  <li>using two queries: one for the count and one with the limit and actual data.</li>
  <li>using one query no limit, and counting the resulset in PHP</li>
  </ul>
  
  <p>Does anyone knows what are the performance implication of doing this work in PHP vs. MySQL?</p>