title: Should I use a for loop for an SQL query or use a WHERE clause with multiple ORs and process the results
body: <p>I have one MySQL table that I am using PHP to query data entries from multiple machines.  I need to summarize/analyze thousands of rows of data for each machine to output to a front-end results table.</p>
  
  <p>I know that it is bad practice to include an sql query in a for loop, so currently I am using one query with 50+ OR statements in it.<br>
  i.e. </p>
  
  <pre><code>SELECT 'x','xx','xxx' 
    from table 
   WHERE machine_id = 'id1' 
      OR machine_id='id2' 
      OR...etc
  </code></pre>
  
  <p>Processing and sorting these result rows puts a lot of load on the server and takes ~ 10 seconds. Using a for loop also takes a long time, but is cleaner and easier to understand when other coders are reading it.</p>
  
  <p>1)Is there a better way to query one table for multiple machines that each have thousands of rows?</p>
  
  <p>2)Should I use the for loop because its easier to read/understand what is going on with the queries?</p>
  
  <p>3)Another One: I've thought about running a cron and preprocessing these results every hour so the front end can just grab the pre-processed results, so I may go with that, but I would still like to know of a more efficient way to render this data to the front-end in real time.</p>