title: Handle rapid sequential get requests from the server side?
body: <p>I'm editing an open source web server to host data from the Android app Torque.  Torque connects to an ODBII adapter in the car, logs data, then uploads it to the server in the form of a get request at a desired interval (in this case, 1 per second).</p>
  
  <p>The codebase when I started stored everything in 1 mysql table, then used some mysql distinct statements to pull out "session" info (which was just repeated on every line of the logged data).  My fork intends to create a second "sessions" table which stores distinct info, leaving only the momentary info in the large table.  </p>
  
  <p>Each get request from the app appears to wait for an "Ok!" returned from the html interface.</p>
  
  <p>What I'm finding is, my logic and multiple queries take a bit longer to return, and the net result is that datapoints aren't getting logged.  Yesterday (before my commit) my commute logged ~1900 datapoints; today (after my commit) it only logged 52.  In my apache access logs, I see <Strike>52 access attempts.</strike> thousands of attempts.</p>
  
  <p>I don't have the ability to edit/modify the app, so I'm stuck trying to re-mediate this from the server side.  I'm only intermediate at coding in PHP.  What techniques can I use to speed up the return code, or what can I do from the server side to produce the return code immediately allowing for another datapoint to be logged?  Is it as simple as moving the echo statement up in the code?  Is there a best practice I'm missing?</p>
  
  <p>I'm going to try to move the response code up and possibly trigger an output buffer flush.  Also, I'm gonna decrease the logging interval to 5 seconds.  Hopefully those things will help.</p>
  
  <p>Unfortunately, the entire code piece is suspect; I'll add the statement I added (thus believe is the trouble) but I don't think it's limited to this:</p>
  
  <pre><code>if ((sizeof($sesskeys) === sizeof($sessvalues)) &amp;&amp; sizeof($sesskeys) &gt; 0) {
    // See if there is already an entry in the sessions table for this session
    $sessionqry = mysql_query("SELECT session, sessionsize FROM $db_sessions_table WHERE session LIKE '$sessuploadid'", $con) or die(mysql_error());
    if (mysql_num_rows($sessionqry) &gt; 0) {
      // If there's an entry in the session table for this session, update the session end time and the datapoint count
      while($row = mysql_fetch_assoc($sessionqry)) {
        $sesssizecount = $row["sessionsize"] + 1;
        $sessionqrystring = "UPDATE $db_sessions_table SET timeend='$sesstime', sessionsize='$sesssizecount' WHERE session LIKE '$sessuploadid'";
        mysql_query($sessionqrystring, $con) or die(mysql_error());
      }
    } else {
      // If this is a new session, insert an entry in the sessions table and then update the start time and datapoint count
      $sql = "INSERT INTO $db_sessions_table (".implode(",", $sesskeys).") VALUES (".implode(",", $sessvalues).")";
      mysql_query($sql, $con) or die(mysql_error());
      $sql = "UPDATE $db_sessions_table SET timestart='$sesstime', sessionsize='1' WHERE session LIKE '$sessuploadid'";
      mysql_query($sql, $con) or die(mysql_error());
    }
  }
  </code></pre>
  
  <p>Before my commits:
  <a href="https://github.com/econpy/torque/blob/master/web/upload_data.php" rel="nofollow">https://github.com/econpy/torque/blob/master/web/upload_data.php</a></p>
  
  <p>After my commits:
  <a href="https://github.com/surfrock66/torque/blob/master/web/upload_data.php" rel="nofollow">https://github.com/surfrock66/torque/blob/master/web/upload_data.php</a></p>
  
  <p>Server: Ubuntu Server 15.10.  Apache 2.4.12.  Php 2.6.0 through mod php.  MySQL 5.6.28.</p>